Skip to content

Commit

Permalink
Generic case to handle both pointer type and raw type
Browse files Browse the repository at this point in the history
  • Loading branch information
kidambisrinivas committed Sep 20, 2024
1 parent 14d26c0 commit cfcb0d8
Showing 1 changed file with 0 additions and 23 deletions.
23 changes: 0 additions & 23 deletions pkg/values/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,6 @@ func Wrap(v any) (Value, error) {
return NewBytes(slice), nil
}

if isOracleIDsArray(v) {
arrayValue := reflect.ValueOf(v)
length := arrayValue.Len()

// Create a slice with the same length as the array
slice := make([]byte, length)

// Copy elements from the array to the slice
for i := 0; i < length; i++ {
thang := arrayValue.Index(i).Interface()
slice[i] = byte(thang.(commontypes.OracleID))
}

return NewBytes(slice), nil
}

if isByteArray(v) {
arrayValue := reflect.ValueOf(v)
length := arrayValue.Len()
Expand Down Expand Up @@ -181,7 +165,6 @@ func Wrap(v any) (Value, error) {
if reflect.Indirect(reflect.ValueOf(v)).Kind() == reflect.Slice {
return createListFromSlice(reflect.Indirect(reflect.ValueOf(v)))
}

case reflect.String:
return Wrap(val.Convert(reflect.TypeOf("")).Interface())
case reflect.Bool:
Expand Down Expand Up @@ -399,16 +382,10 @@ func unwrapTo[T any](underlying T, to any) error {
// eg: type FeedId string allows verification of FeedId's shape while unmarshalling
rTo := reflect.ValueOf(to)
rUnderlying := reflect.ValueOf(underlying)
underlyingPtr := reflect.PointerTo(rUnderlying.Type())
if rTo.Kind() != reflect.Pointer {
return fmt.Errorf("cannot unwrap to value of type: %T", to)
}

if rTo.CanConvert(underlyingPtr) {
reflect.Indirect(rTo.Convert(underlyingPtr)).Set(rUnderlying)
return nil
}

if rUnderlying.CanConvert(reflect.Indirect(rTo).Type()) {
conv := rUnderlying.Convert(reflect.Indirect(rTo).Type())
reflect.Indirect(rTo).Set(conv)
Expand Down

0 comments on commit cfcb0d8

Please sign in to comment.