Skip to content

Commit

Permalink
Re fix nested type (#6)
Browse files Browse the repository at this point in the history
* Revert "- Fix: Flatten any instances of `{type: { type: ... } }`"

This reverts commit 6170308.

* - Revert and re-fix the issue with flattened `type` declarations. - Stop emitting a default of `{}` for records as Avro can't actually handle this.
  • Loading branch information
dorner authored Feb 27, 2024
1 parent 3cd6051 commit 8828554
Show file tree
Hide file tree
Showing 36 changed files with 1,132 additions and 849 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

# UNRELEASED

- Revert and re-fix the issue with flattened `type` declarations.
- Stop emitting a default of `{}` for records as Avro can't actually handle this.

# 0.7 - 2024-02-27

- Fix: Union types are now flattened (i.e. if a union has a type which is another union, they will be combined).
- 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
11 changes: 11 additions & 0 deletions avro/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func (t Array) ToJSON(types *TypeRepo) (any, error) {
if returnedType == "map" {
return itemJson, nil
}
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)
mapType.Set(k, val)
}
}
}
}
jsonMap := orderedmap.New()
jsonMap.Set("type", "array")
Expand Down
24 changes: 10 additions & 14 deletions avro/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/iancoleman/orderedmap"
"google.golang.org/protobuf/types/descriptorpb"
"reflect"
)

type Field struct {
Expand All @@ -18,24 +19,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)
var defaultValue any
if t.Default != "" {
jsonMap.Set("default", t.Default)
defaultValue = t.Default
} else {
jsonMap.Set("default", DefaultValue(typeJson))
defaultValue = DefaultValue(typeJson)
}
// Avro can't actually handle defaults for records
if reflect.ValueOf(defaultValue).Kind() != reflect.Map {
jsonMap.Set("default", defaultValue)
}

return jsonMap, nil
}

Expand Down
1 change: 0 additions & 1 deletion avro/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func flatten(slice []any) []any {
var flattened []any
for _, jsonType := range slice {
jsonMap, ok := jsonType.(*orderedmap.OrderedMap)
LogMsg("%v", reflect.TypeOf(jsonType))
if ok {
typeArr, ok := jsonMap.Get("type")
if ok && reflect.TypeOf(typeArr).Kind() == reflect.Slice {
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,15 +4,16 @@
"namespace": "testdata",
"fields": [
{
"name": "oneof_types",
"type": [
{
"type": "record",
"name": "TypeA",
"namespace": "testdata",
"fields": [
{
"type": "string",
"name": "foo",
"type": "string",
"default": ""
}
]
Expand All @@ -23,14 +24,13 @@
"namespace": "testdata",
"fields": [
{
"type": "string",
"name": "bar",
"type": "string",
"default": ""
}
]
}
],
"name": "oneof_types",
"default": null
}
]
Expand Down
109 changes: 73 additions & 36 deletions testdata/base/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,134 @@
"namespace": "testdata",
"fields": [
{
"type": "string",
"name": "name",
"type": "string",
"default": ""
},
{
"type": "enum",
"name": "blarp",
"symbols": [
"BLARP_UNSPECIFIED",
"BLARP_ME",
"BLARP_YOU"
],
"type": {
"type": "enum",
"name": "Blarp",
"symbols": [
"BLARP_UNSPECIFIED",
"BLARP_ME",
"BLARP_YOU"
],
"default": "BLARP_UNSPECIFIED"
},
"default": "BLARP_UNSPECIFIED"
},
{
"type": "record",
"name": "yowza",
"namespace": "testdata",
"fields": [
{
"type": "float",
"name": "hoo_boy",
"default": 0
}
],
"default": {}
"type": {
"type": "record",
"name": "Yowza",
"namespace": "testdata",
"fields": [
{
"name": "hoo_boy",
"type": "float",
"default": 0
}
]
}
},
{
"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
},
{
"type": "int",
"name": "a_num",
"type": "int",
"default": 0
},
{
"type": "map",
"values": "string",
"name": "a_string_map",
"default": {}
"type": {
"type": "map",
"values": "string"
}
},
{
"type": "map",
"values": "testdata.Blarp",
"name": "a_blarp_map",
"default": {}
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
{
"type": "map",
"values": "testdata.Yowza",
"name": "a_yowza_map",
"default": {}
"type": {
"type": "map",
"values": "testdata.Yowza"
}
},
{
"name": "string_lists",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "StringList",
"namespace": "testdata",
"fields": [
{
"name": "data",
"type": {
"type": "array",
"items": "string"
},
"default": []
}
]
}
},
"default": []
}
]
}
6 changes: 4 additions & 2 deletions testdata/base/StringList.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"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": [
{
"type": "string",
"name": "foo",
"type": "string",
"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": [
{
"type": "string",
"name": "bar",
"type": "string",
"default": ""
}
]
Expand Down
Loading

0 comments on commit 8828554

Please sign in to comment.