From 6f89c9cf9a9826fe4b251820d378aaa0b7ed53a8 Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Tue, 27 Feb 2024 16:52:21 -0500 Subject: [PATCH] - Fix: Accidentally removed map defaults, oops! --- CHANGELOG | 2 ++ avro/field.go | 8 +++++--- testdata/base/Foobar.avsc | 9 ++++++--- testdata/base/Widget.avsc | 12 ++++++++---- testdata/collapse_fields/Foobar.avsc | 9 ++++++--- testdata/collapse_fields/Widget.avsc | 12 ++++++++---- testdata/emit_only/Widget.avsc | 12 ++++++++---- testdata/namespace_map/Foobar.avsc | 9 ++++++--- testdata/namespace_map/Widget.avsc | 12 ++++++++---- testdata/preserve_non_string_maps/Foobar.avsc | 6 ++++-- testdata/preserve_non_string_maps/Widget.avsc | 9 ++++++--- 11 files changed, 67 insertions(+), 33 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2070a2a..5dfe2ce 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/avro/field.go b/avro/field.go index 5a1dbe8..5c1788f 100644 --- a/avro/field.go +++ b/avro/field.go @@ -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 @@ -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) } @@ -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{} } diff --git a/testdata/base/Foobar.avsc b/testdata/base/Foobar.avsc index 043d399..217abce 100644 --- a/testdata/base/Foobar.avsc +++ b/testdata/base/Foobar.avsc @@ -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", diff --git a/testdata/base/Widget.avsc b/testdata/base/Widget.avsc index a28ea95..dfafe8e 100644 --- a/testdata/base/Widget.avsc +++ b/testdata/base/Widget.avsc @@ -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", @@ -166,7 +169,8 @@ "type": { "type": "map", "values": "testdata.StringList" - } + }, + "default": {} }, { "name": "a_one_of", diff --git a/testdata/collapse_fields/Foobar.avsc b/testdata/collapse_fields/Foobar.avsc index 7a98a61..5c1fe30 100644 --- a/testdata/collapse_fields/Foobar.avsc +++ b/testdata/collapse_fields/Foobar.avsc @@ -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", diff --git a/testdata/collapse_fields/Widget.avsc b/testdata/collapse_fields/Widget.avsc index 1d62a64..8fc175b 100644 --- a/testdata/collapse_fields/Widget.avsc +++ b/testdata/collapse_fields/Widget.avsc @@ -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", @@ -163,7 +166,8 @@ "type": "array", "items": "string" } - } + }, + "default": {} }, { "name": "a_one_of", diff --git a/testdata/emit_only/Widget.avsc b/testdata/emit_only/Widget.avsc index a28ea95..dfafe8e 100644 --- a/testdata/emit_only/Widget.avsc +++ b/testdata/emit_only/Widget.avsc @@ -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", @@ -166,7 +169,8 @@ "type": { "type": "map", "values": "testdata.StringList" - } + }, + "default": {} }, { "name": "a_one_of", diff --git a/testdata/namespace_map/Foobar.avsc b/testdata/namespace_map/Foobar.avsc index 66779e6..f1b8046 100644 --- a/testdata/namespace_map/Foobar.avsc +++ b/testdata/namespace_map/Foobar.avsc @@ -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", diff --git a/testdata/namespace_map/Widget.avsc b/testdata/namespace_map/Widget.avsc index 2876db1..8f8c692 100644 --- a/testdata/namespace_map/Widget.avsc +++ b/testdata/namespace_map/Widget.avsc @@ -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", @@ -166,7 +169,8 @@ "type": { "type": "map", "values": "mynamespace.StringList" - } + }, + "default": {} }, { "name": "a_one_of", diff --git a/testdata/preserve_non_string_maps/Foobar.avsc b/testdata/preserve_non_string_maps/Foobar.avsc index 2ed5ad7..3bb6bb9 100644 --- a/testdata/preserve_non_string_maps/Foobar.avsc +++ b/testdata/preserve_non_string_maps/Foobar.avsc @@ -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", diff --git a/testdata/preserve_non_string_maps/Widget.avsc b/testdata/preserve_non_string_maps/Widget.avsc index c0e4e9a..b943346 100644 --- a/testdata/preserve_non_string_maps/Widget.avsc +++ b/testdata/preserve_non_string_maps/Widget.avsc @@ -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", @@ -183,7 +185,8 @@ "type": { "type": "map", "values": "testdata.StringList" - } + }, + "default": {} }, { "name": "a_one_of",