Debian Codegraph @

util/sprig/reflect.go

raw
1 package sprig
2
3 import (
4 "fmt"
5 "reflect"
6 )
7
8 // typeIs returns true if the src is the type named in target.
9 // It compares the type name of src with the target string.
10 //
11 // Parameters:
12 // - target: The type name to check against
13 // - src: The value whose type will be checked
14 //
15 // Returns:
16 // - bool: True if the type name of src matches target, false otherwise
17 func typeIs(target string, src any) bool {
18 return target == typeOf(src)
19 }
20
21 // typeIsLike returns true if the src is the type named in target or a pointer to that type.
22 // This is useful when you need to check for both a type and a pointer to that type.
23 //
24 // Parameters:
25 // - target: The type name to check against
26 // - src: The value whose type will be checked
27 //
28 // Returns:
29 // - bool: True if the type of src matches target or "*"+target, false otherwise
30 func typeIsLike(target string, src any) bool {
31 t := typeOf(src)
32 return target == t || "*"+target == t
33 }
34
35 // typeOf returns the type of a value as a string.
36 // It uses fmt.Sprintf with the %T format verb to get the type name.
37 //
38 // Parameters:
39 // - src: The value whose type name will be returned
40 //
41 // Returns:
42 // - string: The type name of src
43 func typeOf(src any) string {
44 return fmt.Sprintf("%T", src)
45 }
46
47 // kindIs returns true if the kind of src matches the target kind.
48 // This checks the underlying kind (e.g., "string", "int", "map") rather than the specific type.
49 //
50 // Parameters:
51 // - target: The kind name to check against
52 // - src: The value whose kind will be checked
53 //
54 // Returns:
55 // - bool: True if the kind of src matches target, false otherwise
56 func kindIs(target string, src any) bool {
57 return target == kindOf(src)
58 }
59
60 // kindOf returns the kind of a value as a string.
61 // The kind represents the specific Go type category (e.g., "string", "int", "map", "slice").
62 //
63 // Parameters:
64 // - src: The value whose kind will be returned
65 //
66 // Returns:
67 // - string: The kind of src as a string
68 func kindOf(src any) string {
69 return reflect.ValueOf(src).Kind().String()
70 }