From 41654210823f5cca8bcb24c891e897b0def4b669 Mon Sep 17 00:00:00 2001 From: 0xjac Date: Mon, 7 Oct 2024 13:06:50 +0200 Subject: [PATCH] test: plain map/slice CLI flags --- cmd/avrogen/main_test.go | 27 +++++++++++++++++++ cmd/avrogen/testdata/golden.go | 6 +++-- cmd/avrogen/testdata/golden_encoders.go | 8 +++--- .../testdata/golden_encoders_fullschema.go | 8 +++--- cmd/avrogen/testdata/golden_fullname.go | 6 +++-- cmd/avrogen/testdata/golden_plainmap.go | 11 ++++++++ cmd/avrogen/testdata/golden_plainslice.go | 11 ++++++++ cmd/avrogen/testdata/golden_stricttypes.go | 6 +++-- cmd/avrogen/testdata/schema.avsc | 6 +++-- 9 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 cmd/avrogen/testdata/golden_plainmap.go create mode 100644 cmd/avrogen/testdata/golden_plainslice.go diff --git a/cmd/avrogen/main_test.go b/cmd/avrogen/main_test.go index c15c3c4..10d7807 100644 --- a/cmd/avrogen/main_test.go +++ b/cmd/avrogen/main_test.go @@ -189,6 +189,33 @@ func TestAvroGen_GeneratesSchemaWithStrictTypes(t *testing.T) { assert.Equal(t, want, got) } +func TestAvroGen_GeneratePlain(t *testing.T) { + for _, opt := range []string{"map", "slice"} { + t.Run(opt, func(t *testing.T) { + t.Parallel() + + path := t.TempDir() + file := filepath.Join(path, "test.go") + + args := []string{"avrogen", "-pkg", "testpkg", "-o", file, "-plain-" + opt, "testdata/schema.avsc"} + gotCode := realMain(args, io.Discard, io.Discard) + require.Equal(t, 0, gotCode) + + got, err := os.ReadFile(file) + require.NoError(t, err) + + if *update { + err = os.WriteFile("testdata/golden_plain"+opt+".go", got, 0600) + require.NoError(t, err) + } + + want, err := os.ReadFile("testdata/golden_plain" + opt + ".go") + require.NoError(t, err) + assert.Equal(t, want, got) + }) + } +} + func TestParseTags(t *testing.T) { tests := []struct { name string diff --git a/cmd/avrogen/testdata/golden.go b/cmd/avrogen/testdata/golden.go index 1f17271..4583397 100644 --- a/cmd/avrogen/testdata/golden.go +++ b/cmd/avrogen/testdata/golden.go @@ -5,6 +5,8 @@ package testpkg // Test is a test struct. type Test struct { // SomeString is a string. - SomeString string `avro:"someString"` - SomeInt int `avro:"someInt"` + SomeString string `avro:"someString"` + SomeInt int `avro:"someInt"` + SomeNullableMap *map[string]int `avro:"someNullableMap"` + SomeNullableSlice *[]int `avro:"someNullableSlice"` } diff --git a/cmd/avrogen/testdata/golden_encoders.go b/cmd/avrogen/testdata/golden_encoders.go index 44b3e50..a5a552d 100644 --- a/cmd/avrogen/testdata/golden_encoders.go +++ b/cmd/avrogen/testdata/golden_encoders.go @@ -8,11 +8,13 @@ import ( // Test is a test struct. type Test struct { // SomeString is a string. - SomeString string `avro:"someString"` - SomeInt int `avro:"someInt"` + SomeString string `avro:"someString"` + SomeInt int `avro:"someInt"` + SomeNullableMap *map[string]int `avro:"someNullableMap"` + SomeNullableSlice *[]int `avro:"someNullableSlice"` } -var schemaTest = avro.MustParse(`{"name":"a.b.test","type":"record","fields":[{"name":"someString","type":"string"},{"name":"someInt","type":"int"}]}`) +var schemaTest = avro.MustParse(`{"name":"a.b.test","type":"record","fields":[{"name":"someString","type":"string"},{"name":"someInt","type":"int"},{"name":"someNullableMap","type":["null",{"type":"map","values":"int"}]},{"name":"someNullableSlice","type":["null",{"type":"array","items":"int"}]}]}`) // Schema returns the schema for Test. func (o *Test) Schema() avro.Schema { diff --git a/cmd/avrogen/testdata/golden_encoders_fullschema.go b/cmd/avrogen/testdata/golden_encoders_fullschema.go index 7bc7736..9471dfc 100644 --- a/cmd/avrogen/testdata/golden_encoders_fullschema.go +++ b/cmd/avrogen/testdata/golden_encoders_fullschema.go @@ -8,11 +8,13 @@ import ( // Test is a test struct. type Test struct { // SomeString is a string. - SomeString string `avro:"someString"` - SomeInt int `avro:"someInt"` + SomeString string `avro:"someString"` + SomeInt int `avro:"someInt"` + SomeNullableMap *map[string]int `avro:"someNullableMap"` + SomeNullableSlice *[]int `avro:"someNullableSlice"` } -var schemaTest = avro.MustParse(`{"name":"a.b.test","doc":"Test is a test struct","type":"record","fields":[{"name":"someString","doc":"SomeString is a string","type":"string"},{"name":"someInt","type":"int"}]}`) +var schemaTest = avro.MustParse(`{"name":"a.b.test","doc":"Test is a test struct","type":"record","fields":[{"name":"someString","doc":"SomeString is a string","type":"string"},{"name":"someInt","type":"int"},{"name":"someNullableMap","type":["null",{"type":"map","values":"int"}]},{"name":"someNullableSlice","type":["null",{"type":"array","items":"int"}]}]}`) // Schema returns the schema for Test. func (o *Test) Schema() avro.Schema { diff --git a/cmd/avrogen/testdata/golden_fullname.go b/cmd/avrogen/testdata/golden_fullname.go index 5b44801..65327f4 100644 --- a/cmd/avrogen/testdata/golden_fullname.go +++ b/cmd/avrogen/testdata/golden_fullname.go @@ -4,6 +4,8 @@ package testpkg // Test is a test struct. type ABTest struct { // SomeString is a string. - SomeString string `avro:"someString"` - SomeInt int `avro:"someInt"` + SomeString string `avro:"someString"` + SomeInt int `avro:"someInt"` + SomeNullableMap *map[string]int `avro:"someNullableMap"` + SomeNullableSlice *[]int `avro:"someNullableSlice"` } diff --git a/cmd/avrogen/testdata/golden_plainmap.go b/cmd/avrogen/testdata/golden_plainmap.go new file mode 100644 index 0000000..a276792 --- /dev/null +++ b/cmd/avrogen/testdata/golden_plainmap.go @@ -0,0 +1,11 @@ +// Code generated by avro/gen. DO NOT EDIT. +package testpkg + +// Test is a test struct. +type Test struct { + // SomeString is a string. + SomeString string `avro:"someString"` + SomeInt int `avro:"someInt"` + SomeNullableMap map[string]int `avro:"someNullableMap"` + SomeNullableSlice *[]int `avro:"someNullableSlice"` +} diff --git a/cmd/avrogen/testdata/golden_plainslice.go b/cmd/avrogen/testdata/golden_plainslice.go new file mode 100644 index 0000000..fa01932 --- /dev/null +++ b/cmd/avrogen/testdata/golden_plainslice.go @@ -0,0 +1,11 @@ +// Code generated by avro/gen. DO NOT EDIT. +package testpkg + +// Test is a test struct. +type Test struct { + // SomeString is a string. + SomeString string `avro:"someString"` + SomeInt int `avro:"someInt"` + SomeNullableMap *map[string]int `avro:"someNullableMap"` + SomeNullableSlice []int `avro:"someNullableSlice"` +} diff --git a/cmd/avrogen/testdata/golden_stricttypes.go b/cmd/avrogen/testdata/golden_stricttypes.go index a3edc67..012d8f6 100644 --- a/cmd/avrogen/testdata/golden_stricttypes.go +++ b/cmd/avrogen/testdata/golden_stricttypes.go @@ -4,6 +4,8 @@ package testpkg // Test is a test struct. type Test struct { // SomeString is a string. - SomeString string `avro:"someString"` - SomeInt int32 `avro:"someInt"` + SomeString string `avro:"someString"` + SomeInt int32 `avro:"someInt"` + SomeNullableMap *map[string]int32 `avro:"someNullableMap"` + SomeNullableSlice *[]int32 `avro:"someNullableSlice"` } diff --git a/cmd/avrogen/testdata/schema.avsc b/cmd/avrogen/testdata/schema.avsc index 8754f39..8c75f7f 100644 --- a/cmd/avrogen/testdata/schema.avsc +++ b/cmd/avrogen/testdata/schema.avsc @@ -5,6 +5,8 @@ "doc": "Test is a test struct", "fields": [ { "name": "someString", "type": "string", "doc": "SomeString is a string" }, - { "name": "someInt", "type": "int" } + { "name": "someInt", "type": "int" }, + { "name": "someNullableMap", "type": ["null", {"type": "map", "values": "int"}]}, + { "name": "someNullableSlice", "type": ["null", {"type": "array", "items": "int"}]} ] -} \ No newline at end of file +}