From c45f831e64e9034e5a8e89f3ade00121c51ff639 Mon Sep 17 00:00:00 2001 From: "kyle.cao" Date: Tue, 5 Oct 2021 09:13:09 +0800 Subject: [PATCH] support map --- main.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index b56c6fc..c7e06c2 100644 --- a/main.go +++ b/main.go @@ -379,7 +379,7 @@ func validateFlags() { } } -// construct go-type to nebula.Value +// construct Slice to nebula.NList func Slice2Nlist(list []interface{}) (*nebula.NList, error) { sv := []*nebula.Value{} var ret nebula.NList @@ -394,6 +394,21 @@ func Slice2Nlist(list []interface{}) (*nebula.NList, error) { return &ret, nil } +// construct map to nebula.NMap +func Map2Nmap(m map[string]interface{}) (*nebula.NMap, error) { + var ret nebula.NMap + kvs := map[string]*nebula.Value{} + for k, v := range m { + nv, err := Base2Value(v) + if err != nil { + return nil, err + } + kvs[k] = nv + } + ret.Kvs = kvs + return &ret, nil +} + // construct go-type to nebula.Value func Base2Value(any interface{}) (value *nebula.Value, err error) { value = nebula.NewValue() @@ -415,6 +430,12 @@ func Base2Value(any interface{}) (value *nebula.Value, err error) { err = er } value.LVal = nv + } else if v, ok := any.(map[string]interface{}); ok { + nv, er := Map2Nmap(map[string]interface{}(v)) + if er != nil { + err = er + } + value.MVal = nv } else { // unsupport other Value type, use this function carefully err = fmt.Errorf("Do not support convert %T to nebula.Value", any)