Skip to content

Commit

Permalink
merge map into slice conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Weng committed Feb 23, 2022
1 parent b7db14d commit d2db312
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions kusto/data/value/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,17 @@ func (d Dynamic) Convert(v reflect.Value) error {
} else {
valueToSet = reflect.ValueOf(d.Value)
}
case t.Kind() == reflect.Slice:
case t.Kind() == reflect.Slice || t.Kind() == reflect.Map:
if !d.Valid {
return nil
}

ptr := reflect.New(t)
if err := json.Unmarshal([]byte(d.Value), ptr.Interface()); err != nil {
return fmt.Errorf("Error occurred while trying to unmarshal Dynamic into a slice: %s", err)
return fmt.Errorf("Error occurred while trying to unmarshal Dynamic into a %s: %s", t.Kind(), err)
}

valueToSet = ptr.Elem()
case t.Kind() == reflect.Map:
if !d.Valid {
return nil
}
if t.Key().Kind() != reflect.String {
return fmt.Errorf("Type dymanic and can only be stored in a string, *string, map[string]interface{}, *map[string]interface{}, struct or *struct")
}
if t.Elem().Kind() != reflect.Interface {
return fmt.Errorf("Type dymanic and can only be stored in a string, *string, map[string]interface{}, *map[string]interface{}, struct or *struct")
}

m := map[string]interface{}{}
if err := json.Unmarshal([]byte(d.Value), &m); err != nil {
return fmt.Errorf("Error occurred while trying to marshal type dynamic into a map[string]interface{}: %s", err)
}

valueToSet = reflect.ValueOf(m)
case t.Kind() == reflect.Struct:
structPtr := reflect.New(t)

Expand Down

0 comments on commit d2db312

Please # to comment.