Skip to content

Commit

Permalink
support map
Browse files Browse the repository at this point in the history
  • Loading branch information
czpmango committed Oct 5, 2021
1 parent 3ff394b commit c45f831
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit c45f831

Please sign in to comment.