Skip to content

Commit

Permalink
- Fix: Accidentally removed map defaults, oops!
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Feb 28, 2024
1 parent 31e9631 commit 6f89c9c
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 33 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: Accidentally removed map defaults, oops!

# 0.7.1 - 2024-02-27

- Revert and re-fix the issue with flattened `type` declarations.
Expand Down
8 changes: 5 additions & 3 deletions avro/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"github.com/iancoleman/orderedmap"
"google.golang.org/protobuf/types/descriptorpb"
"reflect"
)

type noDefaultType struct{}
var noDefault = noDefaultType{}

type Field struct {
Name string
Type Type
Expand All @@ -28,7 +30,7 @@ func (t Field) ToJSON(types *TypeRepo) (any, error) {
defaultValue = DefaultValue(typeJson)
}
// Avro can't actually handle defaults for records
if reflect.ValueOf(defaultValue).Kind() != reflect.Map {
if defaultValue != noDefault {
jsonMap.Set("default", defaultValue)
}

Expand Down Expand Up @@ -111,7 +113,7 @@ func DefaultValue(t any) any {
case "map":
return map[string]any{}
case "record":
return map[string]any{}
return noDefault
case "array":
return []any{}
}
Expand Down
9 changes: 6 additions & 3 deletions testdata/base/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "testdata.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down
12 changes: 8 additions & 4 deletions testdata/base/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "testdata.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down Expand Up @@ -166,7 +169,8 @@
"type": {
"type": "map",
"values": "testdata.StringList"
}
},
"default": {}
},
{
"name": "a_one_of",
Expand Down
9 changes: 6 additions & 3 deletions testdata/collapse_fields/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "testdata.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down
12 changes: 8 additions & 4 deletions testdata/collapse_fields/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "testdata.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down Expand Up @@ -163,7 +166,8 @@
"type": "array",
"items": "string"
}
}
},
"default": {}
},
{
"name": "a_one_of",
Expand Down
12 changes: 8 additions & 4 deletions testdata/emit_only/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "testdata.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down Expand Up @@ -166,7 +169,8 @@
"type": {
"type": "map",
"values": "testdata.StringList"
}
},
"default": {}
},
{
"name": "a_one_of",
Expand Down
9 changes: 6 additions & 3 deletions testdata/namespace_map/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "mynamespace.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "mynamespace.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down
12 changes: 8 additions & 4 deletions testdata/namespace_map/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,24 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "mynamespace.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
"type": {
"type": "map",
"values": "mynamespace.Yowza"
}
},
"default": {}
},
{
"name": "string_lists",
Expand Down Expand Up @@ -166,7 +169,8 @@
"type": {
"type": "map",
"values": "mynamespace.StringList"
}
},
"default": {}
},
{
"name": "a_one_of",
Expand Down
6 changes: 4 additions & 2 deletions testdata/preserve_non_string_maps/Foobar.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
Expand Down
9 changes: 6 additions & 3 deletions testdata/preserve_non_string_maps/Widget.avsc
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,16 @@
"type": {
"type": "map",
"values": "string"
}
},
"default": {}
},
{
"name": "a_blarp_map",
"type": {
"type": "map",
"values": "testdata.Blarp"
}
},
"default": {}
},
{
"name": "a_yowza_map",
Expand Down Expand Up @@ -183,7 +185,8 @@
"type": {
"type": "map",
"values": "testdata.StringList"
}
},
"default": {}
},
{
"name": "a_one_of",
Expand Down

0 comments on commit 6f89c9c

Please sign in to comment.