From 82b2b13e5a303d9412aae25137c45e79e7a71163 Mon Sep 17 00:00:00 2001 From: Markus Freitag Date: Tue, 1 Nov 2022 18:29:05 +0100 Subject: [PATCH] object: add generic ObjectToAny method --- object/object.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/object/object.go b/object/object.go index 18492ea8..82df34bd 100644 --- a/object/object.go +++ b/object/object.go @@ -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 +}