Skip to content

Commit 6170308

Browse files
committed
- Fix: Flatten any instances of {type: { type: ... } }
1 parent 5714441 commit 6170308

33 files changed

+818
-1005
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# UNRELEASED
44

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

78
# 0.6.1 - 2024-02-27
89

avro/field.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ func (t Field) ToJSON(types *TypeRepo) (any, error) {
1818
return nil, fmt.Errorf("error parsing field type %s %w", t.Name, err)
1919
}
2020
jsonMap := orderedmap.New()
21+
mapType, ok := typeJson.(*orderedmap.OrderedMap)
22+
if ok {
23+
_, isType := mapType.Get("type")
24+
if isType { // we merge the type with the current type
25+
for _, k := range mapType.Keys() {
26+
val, _ := mapType.Get(k)
27+
jsonMap.Set(k, val)
28+
}
29+
}
30+
} else {
31+
jsonMap.Set("type", typeJson)
32+
}
2133
jsonMap.Set("name", t.Name)
22-
jsonMap.Set("type", typeJson)
2334
if t.Default != "" {
2435
jsonMap.Set("default", t.Default)
2536
} else {

testdata/base/AOneOf.avsc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
"namespace": "testdata",
55
"fields": [
66
{
7-
"name": "oneof_types",
87
"type": [
98
{
109
"type": "record",
1110
"name": "TypeA",
1211
"namespace": "testdata",
1312
"fields": [
1413
{
15-
"name": "foo",
1614
"type": "string",
15+
"name": "foo",
1716
"default": ""
1817
}
1918
]
@@ -24,13 +23,14 @@
2423
"namespace": "testdata",
2524
"fields": [
2625
{
27-
"name": "bar",
2826
"type": "string",
27+
"name": "bar",
2928
"default": ""
3029
}
3130
]
3231
}
3332
],
33+
"name": "oneof_types",
3434
"default": null
3535
}
3636
]

testdata/base/Foobar.avsc

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,115 +4,96 @@
44
"namespace": "testdata",
55
"fields": [
66
{
7-
"name": "name",
87
"type": "string",
8+
"name": "name",
99
"default": ""
1010
},
1111
{
12+
"type": "enum",
1213
"name": "blarp",
13-
"type": {
14-
"type": "enum",
15-
"name": "Blarp",
16-
"symbols": [
17-
"BLARP_UNSPECIFIED",
18-
"BLARP_ME",
19-
"BLARP_YOU"
20-
],
21-
"default": "BLARP_UNSPECIFIED"
22-
},
14+
"symbols": [
15+
"BLARP_UNSPECIFIED",
16+
"BLARP_ME",
17+
"BLARP_YOU"
18+
],
2319
"default": "BLARP_UNSPECIFIED"
2420
},
2521
{
22+
"type": "record",
2623
"name": "yowza",
27-
"type": {
28-
"type": "record",
29-
"name": "Yowza",
30-
"namespace": "testdata",
31-
"fields": [
32-
{
33-
"name": "hoo_boy",
34-
"type": "float",
35-
"default": 0
36-
}
37-
]
38-
},
24+
"namespace": "testdata",
25+
"fields": [
26+
{
27+
"type": "float",
28+
"name": "hoo_boy",
29+
"default": 0
30+
}
31+
],
3932
"default": {}
4033
},
4134
{
35+
"type": "array",
36+
"items": "testdata.Blarp",
4237
"name": "blarps",
43-
"type": {
44-
"type": "array",
45-
"items": "testdata.Blarp"
46-
},
4738
"default": []
4839
},
4940
{
41+
"type": "array",
42+
"items": "testdata.Yowza",
5043
"name": "yowzas",
51-
"type": {
52-
"type": "array",
53-
"items": "testdata.Yowza"
54-
},
5544
"default": []
5645
},
5746
{
47+
"type": "array",
48+
"items": "string",
5849
"name": "names",
59-
"type": {
60-
"type": "array",
61-
"items": "string"
62-
},
6350
"default": []
6451
},
6552
{
66-
"name": "optional_name",
6753
"type": [
6854
"null",
6955
"string"
7056
],
57+
"name": "optional_name",
7158
"default": null
7259
},
7360
{
74-
"name": "optional_blarp",
7561
"type": [
7662
"null",
7763
"testdata.Blarp"
7864
],
65+
"name": "optional_blarp",
7966
"default": null
8067
},
8168
{
82-
"name": "optional_yowza",
8369
"type": [
8470
"null",
8571
"testdata.Yowza"
8672
],
73+
"name": "optional_yowza",
8774
"default": null
8875
},
8976
{
90-
"name": "a_num",
9177
"type": "int",
78+
"name": "a_num",
9279
"default": 0
9380
},
9481
{
82+
"type": "map",
83+
"values": "string",
9584
"name": "a_string_map",
96-
"type": {
97-
"type": "map",
98-
"values": "string"
99-
},
10085
"default": {}
10186
},
10287
{
88+
"type": "map",
89+
"values": "testdata.Blarp",
10390
"name": "a_blarp_map",
104-
"type": {
105-
"type": "map",
106-
"values": "testdata.Blarp"
107-
},
10891
"default": {}
10992
},
11093
{
94+
"type": "map",
95+
"values": "testdata.Yowza",
11196
"name": "a_yowza_map",
112-
"type": {
113-
"type": "map",
114-
"values": "testdata.Yowza"
115-
},
11697
"default": {}
11798
}
11899
]

testdata/base/StringList.avsc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
"namespace": "testdata",
55
"fields": [
66
{
7+
"type": "array",
8+
"items": "string",
79
"name": "data",
8-
"type": {
9-
"type": "array",
10-
"items": "string"
11-
},
1210
"default": []
1311
}
1412
]

testdata/base/TypeA.avsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"namespace": "testdata",
55
"fields": [
66
{
7-
"name": "foo",
87
"type": "string",
8+
"name": "foo",
99
"default": ""
1010
}
1111
]

testdata/base/TypeB.avsc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"namespace": "testdata",
55
"fields": [
66
{
7-
"name": "bar",
87
"type": "string",
8+
"name": "bar",
99
"default": ""
1010
}
1111
]

0 commit comments

Comments
 (0)