Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle maps in zson.MarshalZNGContext, UnmarshalZNGContext #3070

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions zson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ func (m *MarshalZNGContext) encodeAny(v reflect.Value) (zng.Type, error) {
return m.encodeArrayBytes(v)
}
return m.encodeArray(v)
case reflect.Map:
if v.IsNil() {
return m.encodeNil(v.Type())
}
return m.encodeMap(v)
case reflect.Slice:
if v.IsNil() {
return m.encodeNil(v.Type())
Expand Down Expand Up @@ -401,6 +406,44 @@ func (m *MarshalZNGContext) encodeAny(v reflect.Value) (zng.Type, error) {
}
}

func (m *MarshalZNGContext) encodeMap(v reflect.Value) (zng.Type, error) {
var lastKeyType, lastValType zng.Type
m.Builder.BeginContainer()
for iter := v.MapRange(); iter.Next(); {
keyType, err := m.encodeValue(iter.Key())
if err != nil {
return nil, err
}
if keyType != lastKeyType && lastKeyType != nil {
return nil, errors.New("map has mixed key types")
}
lastKeyType = keyType
valType, err := m.encodeValue(iter.Value())
if err != nil {
return nil, err
}
if valType != lastValType && lastValType != nil {
return nil, errors.New("map has mixed values types")
}
lastValType = valType
}
m.Builder.TransformContainer(zng.NormalizeMap)
m.Builder.EndContainer()
if lastKeyType == nil {
// Map is empty so look up types.
var err error
lastKeyType, err = m.lookupType(v.Type().Key())
if err != nil {
return nil, err
}
lastValType, err = m.lookupType(v.Type().Elem())
if err != nil {
return nil, err
}
}
return m.Context.LookupTypeMap(lastKeyType, lastValType), nil
}

func (m *MarshalZNGContext) encodeNil(t reflect.Type) (zng.Type, error) {
var typ zng.Type
if t.Kind() == reflect.Interface {
Expand Down Expand Up @@ -507,6 +550,16 @@ func (m *MarshalZNGContext) lookupType(typ reflect.Type) (zng.Type, error) {
return nil, err
}
return m.Context.LookupTypeArray(typ), nil
case reflect.Map:
key, err := m.lookupType(typ.Key())
if err != nil {
return nil, err
}
val, err := m.lookupType(typ.Elem())
if err != nil {
return nil, err
}
return m.Context.LookupTypeMap(key, val), nil
case reflect.Struct:
return m.lookupTypeRecord(typ)
case reflect.Ptr:
Expand Down Expand Up @@ -643,6 +696,8 @@ func (u *UnmarshalZNGContext) decodeAny(zv zng.Value, v reflect.Value) error {
switch v.Kind() {
case reflect.Array:
return u.decodeArray(zv, v)
case reflect.Map:
return u.decodeMap(zv, v)
case reflect.Slice:
if isIP(v.Type()) {
return u.decodeIP(zv, v)
Expand Down Expand Up @@ -778,6 +833,41 @@ func (u *UnmarshalZNGContext) decodeIP(zv zng.Value, v reflect.Value) error {
return err
}

func (u *UnmarshalZNGContext) decodeMap(zv zng.Value, mapVal reflect.Value) error {
typ, ok := zng.AliasOf(zv.Type).(*zng.TypeMap)
if !ok {
return errors.New("not a map")
}
if zv.Bytes == nil {
return nil
}
if mapVal.IsNil() {
mapVal.Set(reflect.MakeMap(mapVal.Type()))
}
keyType := mapVal.Type().Key()
valType := mapVal.Type().Elem()
for it := zv.Iter(); !it.Done(); {
keyzb, _, err := it.Next()
if err != nil {
return err
}
key := reflect.New(keyType).Elem()
if err := u.decodeAny(zng.Value{typ.KeyType, keyzb}, key); err != nil {
return err
}
valzb, _, err := it.Next()
if err != nil {
return err
}
val := reflect.New(valType).Elem()
if err := u.decodeAny(zng.Value{typ.ValType, valzb}, val); err != nil {
return err
}
mapVal.SetMapIndex(key, val)
}
return nil
}

func (u *UnmarshalZNGContext) decodeRecord(zv zng.Value, sval reflect.Value) error {
recType, ok := zng.AliasOf(zv.Type).(*zng.TypeRecord)
if !ok {
Expand Down
20 changes: 20 additions & 0 deletions zson/marshal_zng_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ func TestMarshalZNG(t *testing.T) {
assert.Equal(t, `{Field1:"value1",Sub1:{f2:"value2",Field3:-1},PField1:null(bool)}`, toZSON(t, rec))
}

func TestMarshalMap(t *testing.T) {
type s struct {
Name string
Map map[string]int
}
cases := []s{
{Name: "nil", Map: nil},
{Name: "empty", Map: map[string]int{}},
{Name: "nonempty", Map: map[string]int{"a": 1, "b": 2}},
}
for _, c := range cases {
c := c
var v s
t.Run(c.Name, func(t *testing.T) {
boomerang(t, c, &v)
assert.Equal(t, c, v)
})
}
}

type ZNGThing struct {
A string `zng:"a"`
B int
Expand Down