Skip to content

Commit

Permalink
- Fix: Flatten any instances of {type: { type: ... } }
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Feb 27, 2024
1 parent 5714441 commit 6170308
Show file tree
Hide file tree
Showing 33 changed files with 818 additions and 1,005 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# UNRELEASED

- Feature / breaking change: add `preserve_non_string_maps` and change default behavior to turn all maps into string-keyed ones.
- Fix: Flatten any instances of `{type: { type: ... } }`

# 0.6.1 - 2024-02-27

Expand Down
13 changes: 12 additions & 1 deletion avro/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ func (t Field) ToJSON(types *TypeRepo) (any, error) {
return nil, fmt.Errorf("error parsing field type %s %w", t.Name, err)
}
jsonMap := orderedmap.New()
mapType, ok := typeJson.(*orderedmap.OrderedMap)
if ok {
_, isType := mapType.Get("type")
if isType { // we merge the type with the current type
for _, k := range mapType.Keys() {
val, _ := mapType.Get(k)
jsonMap.Set(k, val)
}
}
} else {
jsonMap.Set("type", typeJson)
}
jsonMap.Set("name", t.Name)
jsonMap.Set("type", typeJson)
if t.Default != "" {
jsonMap.Set("default", t.Default)
} else {
Expand Down
6 changes: 3 additions & 3 deletions testdata/base/AOneOf.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
"namespace": "testdata",
"fields": [
{
"name": "oneof_types",
"type": [
{
"type": "record",
"name": "TypeA",
"namespace": "testdata",
"fields": [
{
"name": "foo",
"type": "string",
"name": "foo",
"default": ""
}
]
Expand All @@ -24,13 +23,14 @@
"namespace": "testdata",
"fields": [
{
"name": "bar",
"type": "string",
"name": "bar",
"default": ""
}
]
}
],
"name": "oneof_types",
"default": null
}
]
Expand Down
83 changes: 32 additions & 51 deletions testdata/base/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,96 @@
"namespace": "testdata",
"fields": [
{
"name": "name",
"type": "string",
"name": "name",
"default": ""
},
{
"type": "enum",
"name": "blarp",
"type": {
"type": "enum",
"name": "Blarp",
"symbols": [
"BLARP_UNSPECIFIED",
"BLARP_ME",
"BLARP_YOU"
],
"default": "BLARP_UNSPECIFIED"
},
"symbols": [
"BLARP_UNSPECIFIED",
"BLARP_ME",
"BLARP_YOU"
],
"default": "BLARP_UNSPECIFIED"
},
{
"type": "record",
"name": "yowza",
"type": {
"type": "record",
"name": "Yowza",
"namespace": "testdata",
"fields": [
{
"name": "hoo_boy",
"type": "float",
"default": 0
}
]
},
"namespace": "testdata",
"fields": [
{
"type": "float",
"name": "hoo_boy",
"default": 0
}
],
"default": {}
},
{
"type": "array",
"items": "testdata.Blarp",
"name": "blarps",
"type": {
"type": "array",
"items": "testdata.Blarp"
},
"default": []
},
{
"type": "array",
"items": "testdata.Yowza",
"name": "yowzas",
"type": {
"type": "array",
"items": "testdata.Yowza"
},
"default": []
},
{
"type": "array",
"items": "string",
"name": "names",
"type": {
"type": "array",
"items": "string"
},
"default": []
},
{
"name": "optional_name",
"type": [
"null",
"string"
],
"name": "optional_name",
"default": null
},
{
"name": "optional_blarp",
"type": [
"null",
"testdata.Blarp"
],
"name": "optional_blarp",
"default": null
},
{
"name": "optional_yowza",
"type": [
"null",
"testdata.Yowza"
],
"name": "optional_yowza",
"default": null
},
{
"name": "a_num",
"type": "int",
"name": "a_num",
"default": 0
},
{
"type": "map",
"values": "string",
"name": "a_string_map",
"type": {
"type": "map",
"values": "string"
},
"default": {}
},
{
"type": "map",
"values": "testdata.Blarp",
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
},
"default": {}
},
{
"type": "map",
"values": "testdata.Yowza",
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "testdata.Yowza"
},
"default": {}
}
]
Expand Down
6 changes: 2 additions & 4 deletions testdata/base/StringList.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
"namespace": "testdata",
"fields": [
{
"type": "array",
"items": "string",
"name": "data",
"type": {
"type": "array",
"items": "string"
},
"default": []
}
]
Expand Down
2 changes: 1 addition & 1 deletion testdata/base/TypeA.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"namespace": "testdata",
"fields": [
{
"name": "foo",
"type": "string",
"name": "foo",
"default": ""
}
]
Expand Down
2 changes: 1 addition & 1 deletion testdata/base/TypeB.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"namespace": "testdata",
"fields": [
{
"name": "bar",
"type": "string",
"name": "bar",
"default": ""
}
]
Expand Down
Loading

0 comments on commit 6170308

Please sign in to comment.