| 1 |
// Copyright (c) 2020-2022, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package types |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"reflect" |
| 13 |
"strings" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
Bool = reflect.TypeOf(false) |
| 19 |
Interface = reflect.TypeOf((*any)(nil)).Elem() |
| 20 |
SliceInterface = reflect.TypeOf(([]any)(nil)) |
| 21 |
FmtStringer = reflect.TypeOf((*fmt.Stringer)(nil)).Elem() |
| 22 |
Error = reflect.TypeOf((*error)(nil)).Elem() |
| 23 |
JsonUnmarshaler = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem() //nolint: revive |
| 24 |
Time = reflect.TypeOf(time.Time{}) |
| 25 |
Int = reflect.TypeOf(int(0)) |
| 26 |
Uint8 = reflect.TypeOf(uint8(0)) |
| 27 |
Rune = reflect.TypeOf(rune(0)) |
| 28 |
String = reflect.TypeOf("") |
| 29 |
) |
| 30 |
|
| 31 |
// IsStruct returns true if t is a struct or a pointer on a struct |
| 32 |
// (whatever the number of chained pointers), false otherwise. |
| 33 |
func IsStruct(t reflect.Type) bool { |
| 34 |
for { |
| 35 |
switch t.Kind() { |
| 36 |
case reflect.Struct: |
| 37 |
return true |
| 38 |
case reflect.Ptr: |
| 39 |
t = t.Elem() |
| 40 |
default: |
| 41 |
return false |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// IsTypeOrConvertible returns (true, false) if v type == target, |
| 47 |
// (true, true) if v if convertible to target type, (false, false) |
| 48 |
// otherwise. |
| 49 |
// |
| 50 |
// It handles go 1.17 slice to array pointer convertibility. |
| 51 |
func IsTypeOrConvertible(v reflect.Value, target reflect.Type) (bool, bool) { |
| 52 |
if v.Type() == target { |
| 53 |
return true, false |
| 54 |
} |
| 55 |
if IsConvertible(v, target) { |
| 56 |
return true, true |
| 57 |
} |
| 58 |
return false, false |
| 59 |
} |
| 60 |
|
| 61 |
// IsConvertible returns true if v is convertible to target type, |
| 62 |
// false otherwise. |
| 63 |
// |
| 64 |
// It handles go 1.17 slice to array pointer convertibility. |
| 65 |
// It handles go 1.20 slice to array convertibility. |
| 66 |
func IsConvertible(v reflect.Value, target reflect.Type) bool { |
| 67 |
if v.Type().ConvertibleTo(target) { |
| 68 |
tk := target.Kind() |
| 69 |
if v.Kind() != reflect.Slice || |
| 70 |
(tk != reflect.Ptr && tk != reflect.Array) || |
| 71 |
// Since go 1.17, a slice can be convertible to a pointer to an |
| 72 |
// array, but Convert() may still panic if the slice length is lesser |
| 73 |
// than array pointed one |
| 74 |
(tk == reflect.Ptr && (target.Elem().Kind() != reflect.Array || |
| 75 |
v.Len() >= target.Elem().Len())) || |
| 76 |
// Since go 1.20, a slice can also be convertible to an array, but |
| 77 |
// Convert() may still panic if the slice length is lesser than |
| 78 |
// array one |
| 79 |
(tk == reflect.Array && v.Len() >= target.Len()) { |
| 80 |
return true |
| 81 |
} |
| 82 |
} |
| 83 |
return false |
| 84 |
} |
| 85 |
|
| 86 |
// KindType returns the kind of val as a string. If the kind is |
| 87 |
// [reflect.Ptr], a "*" is used as prefix of kind of |
| 88 |
// val.Type().Elem(), and so on. If the final kind differs from |
| 89 |
// val.Type(), the type is appended inside parenthesis. |
| 90 |
func KindType(val reflect.Value) string { |
| 91 |
if !val.IsValid() { |
| 92 |
return "nil" |
| 93 |
} |
| 94 |
|
| 95 |
nptr := 0 |
| 96 |
typ := val.Type() |
| 97 |
for typ.Kind() == reflect.Ptr { |
| 98 |
nptr++ |
| 99 |
typ = typ.Elem() |
| 100 |
} |
| 101 |
kind := strings.Repeat("*", nptr) + typ.Kind().String() |
| 102 |
if typ := val.Type().String(); kind != typ { |
| 103 |
kind += " (" + typ + " type)" |
| 104 |
} |
| 105 |
return kind |
| 106 |
} |