Debian Codegraph @

tpl/collections/reflect_helpers.go

raw
1 // Copyright 2017 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package collections
15
16 import (
17 "errors"
18 "fmt"
19 "reflect"
20
21 "github.com/gohugoio/hugo/common/hashing"
22 "github.com/gohugoio/hugo/common/hreflect"
23 "github.com/gohugoio/hugo/common/types"
24 "github.com/gohugoio/hugo/resources/resource"
25 )
26
27 var (
28 zero reflect.Value
29 errorType = reflect.TypeFor[error]()
30 )
31
32 // normalizes different numeric types if isNumber
33 // or get the hash values if not Comparable (such as map or struct)
34 // to make them comparable
35 func normalize(v reflect.Value) any {
36 k := v.Kind()
37 switch {
38 case !v.Type().Comparable():
39 return hashing.HashUint64(v.Interface())
40 case hreflect.IsNumber(k):
41 f, err := hreflect.ToFloat64E(v)
42 if err == nil {
43 return f
44 }
45 }
46
47 vv := types.Unwrapv(v.Interface())
48 if ip, ok := vv.(resource.TransientIdentifier); ok {
49 return ip.TransientKey()
50 }
51
52 return vv
53 }
54
55 // collects identities from the slices in seqs into a set. Numeric values are normalized,
56 // pointers unwrapped.
57 func collectIdentities(seqs ...any) (map[any]bool, error) {
58 seen := make(map[any]bool)
59 for _, seq := range seqs {
60 v := reflect.ValueOf(seq)
61 switch v.Kind() {
62 case reflect.Array, reflect.Slice:
63 for i := range v.Len() {
64 ev, _ := hreflect.Indirect(v.Index(i))
65
66 if !ev.Type().Comparable() {
67 return nil, errors.New("elements must be comparable")
68 }
69
70 seen[normalize(ev)] = true
71 }
72 default:
73 return nil, fmt.Errorf("arguments must be slices or arrays")
74 }
75 }
76
77 return seen, nil
78 }
79
80 // We have some different numeric and string types that we try to behave like
81 // they were the same.
82 func convertValue(v reflect.Value, to reflect.Type) (reflect.Value, error) {
83 if v.Type().AssignableTo(to) {
84 return v, nil
85 }
86 switch kind := to.Kind(); {
87 case kind == reflect.String:
88 return hreflect.ToStringValueE(v)
89 case hreflect.IsNumber(kind):
90 return convertNumber(v, to)
91 default:
92 return reflect.Value{}, fmt.Errorf("%s is not assignable to %s", v.Type(), to)
93 }
94 }
95
96 func convertNumber(v reflect.Value, typ reflect.Type) (reflect.Value, error) {
97 if v, ok := hreflect.ConvertIfPossible(v, typ); ok {
98 return v, nil
99 }
100 return reflect.Value{}, fmt.Errorf("unable to convert value of type %q to %q", v.Type().String(), typ.String())
101 }
102
103 func newSliceElement(items any) any {
104 tp := reflect.TypeOf(items)
105 if tp == nil {
106 return nil
107 }
108 switch tp.Kind() {
109 case reflect.Array, reflect.Slice:
110 tp = tp.Elem()
111 if tp.Kind() == reflect.Pointer {
112 tp = tp.Elem()
113 }
114
115 return reflect.New(tp).Interface()
116 }
117 return nil
118 }