Skip to content

Commit

Permalink
object: add generic ObjectToAny method
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusFreitag committed Nov 1, 2022
1 parent a027137 commit 82b2b13
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,32 @@ func AnyToObject(a any) Object {
}
return NIL
}

func ObjectToAny(o Object) any {
switch v := o.(type) {
case *Boolean:
return v.Value
case *String:
return v.Value
case *Integer:
return v.Value
case *Float:
return v.Value
case *Array:
array := make([]any, len(v.Elements))
for idx, element := range v.Elements {
array[idx] = ObjectToAny(element)
}
return array
case *Hash:
hash := make(map[any]any)
for _, pair := range v.Pairs {
key := ObjectToAny(pair.Key)
if key != nil {
hash[key] = ObjectToAny(pair.Value)
}
}
return hash
}
return nil
}

0 comments on commit 82b2b13

Please sign in to comment.