Skip to content

Commit fb4bbb7

Browse files
committed
chore: functional converter with spdx 3
Signed-off-by: Keith Zantow <[email protected]>
1 parent ad4a1aa commit fb4bbb7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

convert_func.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"reflect"
7+
"slices"
78
)
89

910
type FuncChain interface {
@@ -238,13 +239,16 @@ type reflectConvertStep struct {
238239
convertFunc reflectConvertFunc
239240
}
240241

241-
func (c funcChain) shortestChain(fromType reflect.Type, targetType reflect.Type) []reflectConvertStep {
242+
func (c funcChain) shortestChain(fromType reflect.Type, targetType reflect.Type, visited ...reflect.Type) []reflectConvertStep {
242243
var shortest []reflectConvertStep
243244
for toType, convertFunc := range c.funcs[fromType] {
245+
if slices.Contains(visited, toType) {
246+
continue
247+
}
244248
if toType == targetType {
245249
return []reflectConvertStep{{toType, convertFunc}}
246250
}
247-
chain := c.shortestChain(toType, targetType)
251+
chain := c.shortestChain(toType, targetType, append(visited, fromType)...)
248252
if chain != nil {
249253
chain = append([]reflectConvertStep{{toType, convertFunc}}, chain...)
250254
}

converter.go

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (c *conversion) getValue(fromValue reflect.Value, targetType reflect.Type)
142142
toValue = fromValue
143143
}
144144

145+
if !toValue.IsValid() {
146+
return nilValue
147+
}
148+
145149
// handle non-pointer returns -- the reflect.New earlier always creates a pointer
146150
if !isPtr(baseTargetType) {
147151
toValue = fromPtr(toValue)
@@ -235,6 +239,9 @@ func (c *conversion) convertValueTypes(value reflect.Value, targetType reflect.T
235239

236240
func (c *conversion) findConvertableType(fromType reflect.Type, targetType reflect.Type) reflect.Type {
237241
converters := c.chain.funcs[fromType]
242+
if converters == nil {
243+
return nil
244+
}
238245
if v, ok := converters[targetType]; ok {
239246
if v == nil {
240247
return nil

0 commit comments

Comments
 (0)