| 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 |
"math" |
| 20 |
"reflect" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/common/hashing" |
| 23 |
"github.com/gohugoio/hugo/common/hreflect" |
| 24 |
"github.com/gohugoio/hugo/common/types" |
| 25 |
"github.com/gohugoio/hugo/resources/resource" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
zero reflect.Value |
| 30 |
errorType = reflect.TypeFor[error]() |
| 31 |
) |
| 32 |
|
| 33 |
// normalizes different numeric types if isNumber |
| 34 |
// or get the hash values if not Comparable (such as map or struct) |
| 35 |
// to make them comparable |
| 36 |
func normalize(v reflect.Value) any { |
| 37 |
if v.Kind() == reflect.Interface { |
| 38 |
if v.IsNil() { |
| 39 |
return nil |
| 40 |
} |
| 41 |
v = v.Elem() |
| 42 |
} |
| 43 |
k := v.Kind() |
| 44 |
switch { |
| 45 |
case !v.Type().Comparable(): |
| 46 |
return hashing.HashUint64(v.Interface()) |
| 47 |
case hreflect.IsNumber(k): |
| 48 |
return numericKey(v) |
| 49 |
} |
| 50 |
|
| 51 |
vv := types.Unwrapv(v.Interface()) |
| 52 |
if ip, ok := vv.(resource.TransientIdentifier); ok { |
| 53 |
return ip.TransientKey() |
| 54 |
} |
| 55 |
|
| 56 |
return vv |
| 57 |
} |
| 58 |
|
| 59 |
// numericKey returns a key that is equal for numerically equal values |
| 60 |
// regardless of their Go type, without losing precision. |
| 61 |
func numericKey(v reflect.Value) any { |
| 62 |
switch k := v.Kind(); { |
| 63 |
case hreflect.IsInt(k): |
| 64 |
return v.Int() |
| 65 |
case hreflect.IsUint(k): |
| 66 |
u := v.Uint() |
| 67 |
if u > math.MaxInt64 { |
| 68 |
return u |
| 69 |
} |
| 70 |
return int64(u) |
| 71 |
default: |
| 72 |
f := v.Float() |
| 73 |
if f == math.Trunc(f) { |
| 74 |
if f >= -(1<<63) && f < 1<<63 { |
| 75 |
return int64(f) |
| 76 |
} |
| 77 |
if f >= 1<<63 && f < 1<<64 { |
| 78 |
return uint64(f) |
| 79 |
} |
| 80 |
} |
| 81 |
return f |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
// collects identities from the slices in seqs into a set. Numeric values are normalized, |
| 86 |
// pointers unwrapped. |
| 87 |
func collectIdentities(seqs ...any) (map[any]bool, error) { |
| 88 |
seen := make(map[any]bool) |
| 89 |
for _, seq := range seqs { |
| 90 |
v := reflect.ValueOf(seq) |
| 91 |
switch v.Kind() { |
| 92 |
case reflect.Array, reflect.Slice: |
| 93 |
for i := range v.Len() { |
| 94 |
ev, _ := hreflect.Indirect(v.Index(i)) |
| 95 |
|
| 96 |
if !ev.Type().Comparable() { |
| 97 |
return nil, errors.New("elements must be comparable") |
| 98 |
} |
| 99 |
|
| 100 |
seen[normalize(ev)] = true |
| 101 |
} |
| 102 |
default: |
| 103 |
return nil, fmt.Errorf("arguments must be slices or arrays") |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return seen, nil |
| 108 |
} |
| 109 |
|
| 110 |
// We have some different numeric and string types that we try to behave like |
| 111 |
// they were the same. |
| 112 |
func convertValue(v reflect.Value, to reflect.Type) (reflect.Value, error) { |
| 113 |
if v.Type().AssignableTo(to) { |
| 114 |
return v, nil |
| 115 |
} |
| 116 |
switch kind := to.Kind(); { |
| 117 |
case kind == reflect.String: |
| 118 |
return hreflect.ToStringValueE(v) |
| 119 |
case hreflect.IsNumber(kind): |
| 120 |
return convertNumber(v, to) |
| 121 |
default: |
| 122 |
return reflect.Value{}, fmt.Errorf("%s is not assignable to %s", v.Type(), to) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func convertNumber(v reflect.Value, typ reflect.Type) (reflect.Value, error) { |
| 127 |
if cv, ok := hreflect.ConvertIfPossible(v, typ); ok { |
| 128 |
return cv, nil |
| 129 |
} |
| 130 |
if v.Kind() == reflect.Interface { |
| 131 |
v = v.Elem() |
| 132 |
} |
| 133 |
if hreflect.IsFloat(v.Kind()) && hreflect.IsFloat(typ.Kind()) { |
| 134 |
// Narrowing float64 to float32 loses precision, but that is expected. |
| 135 |
return v.Convert(typ), nil |
| 136 |
} |
| 137 |
return reflect.Value{}, fmt.Errorf("unable to convert value of type %q to %q", v.Type().String(), typ.String()) |
| 138 |
} |
| 139 |
|
| 140 |
func newSliceElement(items any) any { |
| 141 |
tp := reflect.TypeOf(items) |
| 142 |
if tp == nil { |
| 143 |
return nil |
| 144 |
} |
| 145 |
switch tp.Kind() { |
| 146 |
case reflect.Array, reflect.Slice: |
| 147 |
tp = tp.Elem() |
| 148 |
if tp.Kind() == reflect.Pointer { |
| 149 |
tp = tp.Elem() |
| 150 |
} |
| 151 |
|
| 152 |
return reflect.New(tp).Interface() |
| 153 |
} |
| 154 |
return nil |
| 155 |
} |