Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/cast/indirect.go
2875 views
1
// Copyright © 2014 Steve Francia <[email protected]>.
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file.
5
6
package cast
7
8
import (
9
"reflect"
10
)
11
12
// From html/template/content.go
13
// Copyright 2011 The Go Authors. All rights reserved.
14
// indirect returns the value, after dereferencing as many times
15
// as necessary to reach the base type (or nil).
16
func indirect(i any) (any, bool) {
17
if i == nil {
18
return nil, false
19
}
20
21
if t := reflect.TypeOf(i); t.Kind() != reflect.Ptr {
22
// Avoid creating a reflect.Value if it's not a pointer.
23
return i, false
24
}
25
26
v := reflect.ValueOf(i)
27
28
for v.Kind() == reflect.Ptr || (v.Kind() == reflect.Interface && v.Elem().Kind() == reflect.Ptr) {
29
if v.IsNil() {
30
return nil, true
31
}
32
33
v = v.Elem()
34
}
35
36
return v.Interface(), true
37
}
38
39