Skip to content

Commit

Permalink
Fix: Still seeing nested type: { type: {} } declarations for maps w…
Browse files Browse the repository at this point in the history
…here the values are collapsed records (only for first invocation).
  • Loading branch information
dorner committed Mar 11, 2024
1 parent 17147e3 commit 88864bb
Show file tree
Hide file tree
Showing 13 changed files with 465 additions and 368 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# UNRELEASED

- Fix: Still seeing nested `type: { type: {} }` declarations for maps where the values are collapsed records (only for first invocation).

# 0.7.2 - 2024-02-28

- Fix: Accidentally removed map defaults, oops!
Expand Down
16 changes: 16 additions & 0 deletions avro/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ func (t Map) ToJSON(types *TypeRepo) (any, error) {
if ok && mapType[0] == "null"{
valueJson = mapType[1]
}
valueJsonMap, ok := valueJson.(*orderedmap.OrderedMap)
if ok {
returnedType, _ := valueJsonMap.Get("type")
returnedMap, ok := returnedType.(*orderedmap.OrderedMap)
if ok {
_, hasType := returnedMap.Get("type")
if hasType {
// it's adding an extra nesting of type: { type: ... } } - we have to flatten it
for _, k := range returnedMap.Keys() {
val, _ := returnedMap.Get(k)
valueJsonMap.Set(k, val)
}
}
}

}
jsonMap := orderedmap.New()
jsonMap.Set("type", "map")
jsonMap.Set("values", valueJson)
Expand Down
14 changes: 11 additions & 3 deletions testdata/base/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@
"default": {}
},
{
"name": "string_lists",
"name": "string_list_map",
"type": {
"type": "array",
"items": {
"type": "map",
"values": {
"type": "record",
"name": "StringList",
"namespace": "testdata",
Expand All @@ -134,6 +134,14 @@
]
}
},
"default": {}
},
{
"name": "string_lists",
"type": {
"type": "array",
"items": "testdata.StringList"
},
"default": []
}
]
Expand Down
152 changes: 80 additions & 72 deletions testdata/base/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,77 @@
"type": "long",
"default": 0
},
{
"name": "string_map",
"type": {
"type": "map",
"values": {
"type": "record",
"name": "StringList",
"namespace": "testdata",
"fields": [
{
"name": "data",
"type": {
"type": "array",
"items": "string"
},
"default": []
}
]
}
},
"default": {}
},
{
"name": "strings",
"type": "testdata.StringList",
"default": ""
},
{
"name": "a_one_of",
"type": [
"null",
{
"type": "record",
"name": "AOneOf",
"namespace": "testdata",
"fields": [
{
"name": "oneof_types",
"type": [
{
"type": "record",
"name": "TypeA",
"namespace": "testdata",
"fields": [
{
"name": "foo",
"type": "string",
"default": ""
}
]
},
{
"type": "record",
"name": "TypeB",
"namespace": "testdata",
"fields": [
{
"name": "bar",
"type": "string",
"default": ""
}
]
}
],
"default": null
}
]
}
],
"default": null
},
{
"name": "foobar",
"type": {
Expand Down Expand Up @@ -134,87 +205,24 @@
},
"default": {}
},
{
"name": "string_list_map",
"type": {
"type": "map",
"values": "testdata.StringList"
},
"default": {}
},
{
"name": "string_lists",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "StringList",
"namespace": "testdata",
"fields": [
{
"name": "data",
"type": {
"type": "array",
"items": "string"
},
"default": []
}
]
}
"items": "testdata.StringList"
},
"default": []
}
]
}
},
{
"name": "strings",
"type": "testdata.StringList",
"default": ""
},
{
"name": "string_map",
"type": {
"type": "map",
"values": "testdata.StringList"
},
"default": {}
},
{
"name": "a_one_of",
"type": [
"null",
{
"type": "record",
"name": "AOneOf",
"namespace": "testdata",
"fields": [
{
"name": "oneof_types",
"type": [
{
"type": "record",
"name": "TypeA",
"namespace": "testdata",
"fields": [
{
"name": "foo",
"type": "string",
"default": ""
}
]
},
{
"type": "record",
"name": "TypeB",
"namespace": "testdata",
"fields": [
{
"name": "bar",
"type": "string",
"default": ""
}
]
}
],
"default": null
}
]
}
],
"default": null
}
]
}
15 changes: 13 additions & 2 deletions testdata/collapse_fields/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,25 @@
},
"default": {}
},
{
"name": "string_list_map",
"type": {
"type": "map",
"values": {
"name": "data",
"type": "array",
"default": [],
"items": "string"
}
},
"default": {}
},
{
"name": "string_lists",
"type": {
"type": "array",
"items": {
"name": "data",
"type": "array",
"default": [],
"items": "string"
}
},
Expand Down
Loading

0 comments on commit 88864bb

Please sign in to comment.