Debian Codegraph @

common/hreflect/convert.go

raw
1 // Copyright 2025 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 hreflect
15
16 import (
17 "fmt"
18 "math"
19 "reflect"
20 )
21
22 var (
23 typeInt64 = reflect.TypeFor[int64]()
24 typeFloat64 = reflect.TypeFor[float64]()
25 typeString = reflect.TypeFor[string]()
26 )
27
28 // ToInt64 converts v to int64 if possible, returning an error if not.
29 func ToInt64E(v reflect.Value) (int64, error) {
30 if v, ok := ConvertIfPossible(v, typeInt64); ok {
31 return v.Int(), nil
32 }
33 return 0, errConvert(v, "int64")
34 }
35
36 // ToInt64 converts v to int64 if possible. It panics if the conversion is not possible.
37 func ToInt64(v reflect.Value) int64 {
38 vv, err := ToInt64E(v)
39 if err != nil {
40 panic(err)
41 }
42 return vv
43 }
44
45 // ToFloat64E converts v to float64 if possible, returning an error if not.
46 func ToFloat64E(v reflect.Value) (float64, error) {
47 if v, ok := ConvertIfPossible(v, typeFloat64); ok {
48 return v.Float(), nil
49 }
50 return 0, errConvert(v, "float64")
51 }
52
53 // ToFloat64 converts v to float64 if possible, panicking if not.
54 func ToFloat64(v reflect.Value) float64 {
55 vv, err := ToFloat64E(v)
56 if err != nil {
57 panic(err)
58 }
59 return vv
60 }
61
62 // ToStringE converts v to string if possible, returning an error if not.
63 func ToStringE(v reflect.Value) (string, error) {
64 vv, err := ToStringValueE(v)
65 if err != nil {
66 return "", err
67 }
68 return vv.String(), nil
69 }
70
71 func ToStringValueE(v reflect.Value) (reflect.Value, error) {
72 if v, ok := ConvertIfPossible(v, typeString); ok {
73 return v, nil
74 }
75 return reflect.Value{}, errConvert(v, "string")
76 }
77
78 // ToString converts v to string if possible, panicking if not.
79 func ToString(v reflect.Value) string {
80 vv, err := ToStringE(v)
81 if err != nil {
82 panic(err)
83 }
84 return vv
85 }
86
87 func errConvert(v reflect.Value, s string) error {
88 return fmt.Errorf("unable to convert value of type %q to %q", v.Type().String(), s)
89 }
90
91 // ConvertIfPossible tries to convert val to typ if possible.
92 // This is implemented for numeric and string kinds. The conversion is lossless;
93 // if the value cannot be represented exactly in typ, it returns false.
94 // It was added to handle the move to a new YAML library which produces uint64 for unsigned integers.
95 // See Issue 14079.
96 func ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {
97 if val.Kind() == reflect.Interface {
98 if val.IsNil() {
99 return reflect.Zero(typ), true
100 }
101 val = val.Elem()
102 }
103
104 if val.Type().AssignableTo(typ) {
105 // No conversion needed.
106 return val, true
107 }
108
109 if IsInt(typ.Kind()) {
110 return convertToIntIfPossible(val, typ)
111 }
112 if IsFloat(typ.Kind()) {
113 return convertToFloatIfPossible(val, typ)
114 }
115 if IsUint(typ.Kind()) {
116 return convertToUintIfPossible(val, typ)
117 }
118 if IsString(typ.Kind()) && IsString(val.Kind()) {
119 return val.Convert(typ), true
120 }
121
122 return reflect.Value{}, false
123 }
124
125 func convertToUintIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {
126 if IsInt(val.Kind()) {
127 i := val.Int()
128 if i < 0 {
129 return reflect.Value{}, false
130 }
131 u := uint64(i)
132 if typ.OverflowUint(u) {
133 return reflect.Value{}, false
134 }
135 return reflect.ValueOf(u).Convert(typ), true
136 }
137 if IsUint(val.Kind()) {
138 if typ.OverflowUint(val.Uint()) {
139 return reflect.Value{}, false
140 }
141 return val.Convert(typ), true
142 }
143 if IsFloat(val.Kind()) {
144 f := val.Float()
145 if f < 0 || f >= float64(math.MaxUint64) {
146 return reflect.Value{}, false
147 }
148 if f != math.Trunc(f) {
149 return reflect.Value{}, false
150 }
151 u := uint64(f)
152 if typ.OverflowUint(u) {
153 return reflect.Value{}, false
154 }
155 return reflect.ValueOf(u).Convert(typ), true
156 }
157 return reflect.Value{}, false
158 }
159
160 func convertToFloatIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {
161 if IsInt(val.Kind()) {
162 i := val.Int()
163 if typ.Kind() == reflect.Float32 {
164 f := float32(i)
165 if float64(f) >= 1<<63 || int64(f) != i {
166 return reflect.Value{}, false
167 }
168 return reflect.ValueOf(f).Convert(typ), true
169 }
170 f := float64(i)
171 if f >= 1<<63 || int64(f) != i {
172 return reflect.Value{}, false
173 }
174 return reflect.ValueOf(f).Convert(typ), true
175 }
176 if IsUint(val.Kind()) {
177 u := val.Uint()
178 if typ.Kind() == reflect.Float32 {
179 f := float32(u)
180 if float64(f) >= 1<<64 || uint64(f) != u {
181 return reflect.Value{}, false
182 }
183 return reflect.ValueOf(f).Convert(typ), true
184 }
185 f := float64(u)
186 if f >= 1<<64 || uint64(f) != u {
187 return reflect.Value{}, false
188 }
189 return reflect.ValueOf(f).Convert(typ), true
190 }
191 if IsFloat(val.Kind()) {
192 f := val.Float()
193 if typ.Kind() == reflect.Float32 && float64(float32(f)) != f {
194 return reflect.Value{}, false
195 }
196 return val.Convert(typ), true
197 }
198
199 return reflect.Value{}, false
200 }
201
202 func convertToIntIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {
203 if IsInt(val.Kind()) {
204 if typ.OverflowInt(val.Int()) {
205 return reflect.Value{}, false
206 }
207 return val.Convert(typ), true
208 }
209 if IsUint(val.Kind()) {
210 if val.Uint() > uint64(math.MaxInt64) {
211 return reflect.Value{}, false
212 }
213 if typ.OverflowInt(int64(val.Uint())) {
214 return reflect.Value{}, false
215 }
216 return val.Convert(typ), true
217 }
218 if IsFloat(val.Kind()) {
219 f := val.Float()
220 if f < float64(math.MinInt64) || f >= float64(math.MaxInt64) {
221 return reflect.Value{}, false
222 }
223 if f != math.Trunc(f) {
224 return reflect.Value{}, false
225 }
226 if typ.OverflowInt(int64(f)) {
227 return reflect.Value{}, false
228 }
229 return reflect.ValueOf(int64(f)).Convert(typ), true
230
231 }
232
233 return reflect.Value{}, false
234 }