From 218451673266c95139603c52379fe3579995664c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Legrand?= <135633967+clement-heetch@users.noreply.github.com> Date: Fri, 29 Nov 2024 18:30:51 +0100 Subject: [PATCH] Fix Go names case (#142) --- cmd/avrogo/generate.go | 7 +- cmd/avrogo/generate_test.go | 17 +++ cmd/avrogo/template.go | 2 +- cmd/avrogo/testdata/schema/go.mod | 15 +++ cmd/avrogo/testdata/schema/go.sum | 8 ++ cmd/avrogo/testdata/schema/object.avsc | 46 +++++++ cmd/avrogo/testdata/schema/object.golden.go | 135 ++++++++++++++++++++ go.mod | 6 + go.sum | 13 ++ 9 files changed, 245 insertions(+), 4 deletions(-) create mode 100644 cmd/avrogo/testdata/schema/go.mod create mode 100644 cmd/avrogo/testdata/schema/go.sum create mode 100644 cmd/avrogo/testdata/schema/object.avsc create mode 100644 cmd/avrogo/testdata/schema/object.golden.go diff --git a/cmd/avrogo/generate.go b/cmd/avrogo/generate.go index 46a3683..7fb212f 100644 --- a/cmd/avrogo/generate.go +++ b/cmd/avrogo/generate.go @@ -4,14 +4,15 @@ import ( "bytes" "encoding/json" "fmt" - "golang.org/x/text/cases" - "golang.org/x/text/language" "io" "regexp" "sort" "strconv" "strings" + "golang.org/x/text/cases" + "golang.org/x/text/language" + "github.com/actgardner/gogen-avro/v10/parser" "github.com/actgardner/gogen-avro/v10/schema" ) @@ -425,7 +426,7 @@ func (gc *generateContext) defaultFuncLiteral(v interface{}, t schema.AvroType) func goName(s string) (string, error) { lastIndex := strings.LastIndex(s, ".") name := s[lastIndex+1:] - name = cases.Title(language.Und).String(strings.Trim(name, "_")) + name = cases.Title(language.Und, cases.NoLower).String(strings.Trim(name, "_")) if !isExportedGoIdentifier(name) { return "", fmt.Errorf("cannot form an exported Go identifier from %q", s) } diff --git a/cmd/avrogo/generate_test.go b/cmd/avrogo/generate_test.go index ccf0d7f..2f978e0 100644 --- a/cmd/avrogo/generate_test.go +++ b/cmd/avrogo/generate_test.go @@ -1,8 +1,11 @@ package main import ( + "bytes" "github.com/actgardner/gogen-avro/v10/parser" "github.com/actgardner/gogen-avro/v10/schema" + "github.com/sebdah/goldie/v2" + "github.com/stretchr/testify/assert" "testing" avro "github.com/actgardner/gogen-avro/v10/schema" @@ -75,3 +78,17 @@ func TestShouldImportAvroTypeGen(t *testing.T) { }) } } + +func TestGenerate(t *testing.T) { + fixtureDir := "testdata/schema" + g := goldie.New(t, goldie.WithFixtureDir(fixtureDir), goldie.WithNameSuffix(".golden.go")) + + var buf bytes.Buffer + testPackage := "dummy" + ns, fileDefinitions, err := parseFiles([]string{"testdata/schema/object.avsc"}) + assert.NoError(t, err) + + err = generate(&buf, testPackage, ns, fileDefinitions[0]) + assert.NoError(t, err) + g.Assert(t, "object", buf.Bytes()) +} diff --git a/cmd/avrogo/template.go b/cmd/avrogo/template.go index b48071e..998167e 100644 --- a/cmd/avrogo/template.go +++ b/cmd/avrogo/template.go @@ -142,7 +142,7 @@ func defName(def schema.Definition) string { } func symbolName(e *schema.EnumDefinition, symbol string) string { - return defName(e) + cases.Title(language.Und).String(symbol) + return defName(e) + cases.Title(language.Und, cases.NoLower).String(symbol) } func quote(s string) string { diff --git a/cmd/avrogo/testdata/schema/go.mod b/cmd/avrogo/testdata/schema/go.mod new file mode 100644 index 0000000..8c32c8c --- /dev/null +++ b/cmd/avrogo/testdata/schema/go.mod @@ -0,0 +1,15 @@ +module dummy + +go 1.23 + +require ( + github.com/google/uuid v1.6.0 + github.com/heetch/avro v0.4.6-0.20241128170218-20b562ce498f + github.com/heetch/galaxy-go/v2 v2.24.0 +) + +require github.com/actgardner/gogen-avro/v10 v10.2.1 // indirect + +replace ( + github.com/heetch/avro => ../../../.. +) diff --git a/cmd/avrogo/testdata/schema/go.sum b/cmd/avrogo/testdata/schema/go.sum new file mode 100644 index 0000000..c544cd3 --- /dev/null +++ b/cmd/avrogo/testdata/schema/go.sum @@ -0,0 +1,8 @@ +github.com/actgardner/gogen-avro/v10 v10.2.1 h1:z3pOGblRjAJCYpkIJ8CmbMJdksi4rAhaygw0dyXZ930= +github.com/actgardner/gogen-avro/v10 v10.2.1/go.mod h1:QUhjeHPchheYmMDni/Nx7VB0RsT/ee8YIgGY/xpEQgQ= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/heetch/avro v0.4.6-0.20241128170218-20b562ce498f h1:iK4+HtIuNeOEJsVBRMOmmEl9mVbnhbf2MYbvzvK22+E= +github.com/heetch/avro v0.4.6-0.20241128170218-20b562ce498f/go.mod h1:gxf9GnbjTXmWmqxhdNbAMcZCjpye7RV5r9t3Q0dL6ws= +github.com/heetch/galaxy-go/v2 v2.24.0 h1:qn5wIMWpFSogzQQ2SzvVoUrd1sc74U9xjbsjmbkWils= +github.com/heetch/galaxy-go/v2 v2.24.0/go.mod h1:VSyZlEylS5T7UDPlaC9fjVvMz5qo0sh3JyHjajWyh9E= diff --git a/cmd/avrogo/testdata/schema/object.avsc b/cmd/avrogo/testdata/schema/object.avsc new file mode 100644 index 0000000..06295f3 --- /dev/null +++ b/cmd/avrogo/testdata/schema/object.avsc @@ -0,0 +1,46 @@ +{ + "heetchmeta": { + "version": 1 + }, + "name": "Object", + "namespace": "ns", + "type": "record", + "fields": [ + { + "doc": "Child1", + "name": "Child1", + "type": { + "default": "UNSPECIFIED", + "doc": "doc", + "heetchmeta": { + "skipRFC": true + }, + "name": "Child1", + "namespace": "subns", + "symbols": [ + "UNSPECIFIED", + "VALUE1", + "VALUE2", + "VALUE_EXTRA_1", + "VALUE_EXTRA_2" + ], + "type": "enum" + } + }, + { + "default": "UNSPECIFIED", + "name": "Child2", + "type": { + "name": "Child2", + "symbols": [ + "UNSPECIFIED", + "STARTED", + "COMPLETED", + "FAILED", + "ADDED_BY_OPERATOR" + ], + "type": "enum" + } + } + ] +} diff --git a/cmd/avrogo/testdata/schema/object.golden.go b/cmd/avrogo/testdata/schema/object.golden.go new file mode 100644 index 0000000..a3f2b68 --- /dev/null +++ b/cmd/avrogo/testdata/schema/object.golden.go @@ -0,0 +1,135 @@ + +// Code generated by avrogen. DO NOT EDIT. + +package dummy + +import ( + "fmt" + "github.com/heetch/avro/avrotypegen" + "strconv" +) + + + + type Child2 int + const ( + Child2UNSPECIFIED Child2 = iota + Child2STARTED + Child2COMPLETED + Child2FAILED + Child2ADDED_BY_OPERATOR + ) + + var _Child2_strings = []string{ + "UNSPECIFIED", + "STARTED", + "COMPLETED", + "FAILED", + "ADDED_BY_OPERATOR", + } + + // String returns the textual representation of Child2. + func (e Child2) String() string { + if e < 0 || int(e) >= len(_Child2_strings) { + return "Child2(" + strconv.FormatInt(int64(e), 10) + ")" + } + return _Child2_strings[e] + } + + // MarshalText implements encoding.TextMarshaler + // by returning the textual representation of Child2. + func (e Child2) MarshalText() ([]byte, error) { + if e < 0 || int(e) >= len(_Child2_strings) { + return nil, fmt.Errorf("Child2 value %d is out of bounds", e) + } + return []byte(_Child2_strings[e]), nil + } + + // UnmarshalText implements encoding.TextUnmarshaler + // by expecting the textual representation of Child2. + func (e *Child2) UnmarshalText(data []byte) error { + // Note for future: this could be more efficient. + for i, s := range _Child2_strings { + if string(data) == s { + *e = Child2(i) + return nil + } + } + return fmt.Errorf("unknown value %q for Child2", data) + } + + + + + + type Object struct { + // Child1 +Child1 Child1 + Child2 Child2 + + } + + // AvroRecord implements the avro.AvroRecord interface. + func (Object) AvroRecord() avrotypegen.RecordInfo { + return avrotypegen.RecordInfo{ +Schema: `{"fields":[{"doc":"Child1","name":"Child1","type":{"default":"UNSPECIFIED","doc":"doc","heetchmeta":{"skipRFC":true},"name":"Child1","namespace":"subns","symbols":["UNSPECIFIED","VALUE1","VALUE2","VALUE_EXTRA_1","VALUE_EXTRA_2"],"type":"enum"}},{"default":"UNSPECIFIED","name":"Child2","type":{"name":"Child2","symbols":["UNSPECIFIED","STARTED","COMPLETED","FAILED","ADDED_BY_OPERATOR"],"type":"enum"}}],"heetchmeta":{"version":1},"name":"ns.Object","type":"record"}`, +Required: []bool{ +0: true, +}, +} + } + + + + + +// doc +type Child1 int + const ( + Child1UNSPECIFIED Child1 = iota + Child1VALUE1 + Child1VALUE2 + Child1VALUE_EXTRA_1 + Child1VALUE_EXTRA_2 + ) + + var _Child1_strings = []string{ + "UNSPECIFIED", + "VALUE1", + "VALUE2", + "VALUE_EXTRA_1", + "VALUE_EXTRA_2", + } + + // String returns the textual representation of Child1. + func (e Child1) String() string { + if e < 0 || int(e) >= len(_Child1_strings) { + return "Child1(" + strconv.FormatInt(int64(e), 10) + ")" + } + return _Child1_strings[e] + } + + // MarshalText implements encoding.TextMarshaler + // by returning the textual representation of Child1. + func (e Child1) MarshalText() ([]byte, error) { + if e < 0 || int(e) >= len(_Child1_strings) { + return nil, fmt.Errorf("Child1 value %d is out of bounds", e) + } + return []byte(_Child1_strings[e]), nil + } + + // UnmarshalText implements encoding.TextUnmarshaler + // by expecting the textual representation of Child1. + func (e *Child1) UnmarshalText(data []byte) error { + // Note for future: this could be more efficient. + for i, s := range _Child1_strings { + if string(data) == s { + *e = Child1(i) + return nil + } + } + return fmt.Errorf("unknown value %q for Child1", data) + } + + + diff --git a/go.mod b/go.mod index 56fdf9a..b593af5 100644 --- a/go.mod +++ b/go.mod @@ -9,18 +9,24 @@ require ( github.com/kr/pretty v0.3.0 github.com/linkedin/goavro/v2 v2.11.1 github.com/rogpeppe/go-internal v1.9.0 + github.com/sebdah/goldie/v2 v2.5.5 + github.com/stretchr/testify v1.7.1 golang.org/x/text v0.3.0 gopkg.in/httprequest.v1 v1.2.1 gopkg.in/retry.v1 v1.0.3 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/go-cmp v0.5.6 // indirect github.com/julienschmidt/httprouter v1.3.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sergi/go-diff v1.0.0 // indirect golang.org/x/net v0.0.0-20200505041828-1ed23360d12c // indirect golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect gopkg.in/errgo.v1 v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 6a4094e..2f67be2 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ github.com/actgardner/gogen-avro/v10 v10.2.1 h1:z3pOGblRjAJCYpkIJ8CmbMJdksi4rAhaygw0dyXZ930= github.com/actgardner/gogen-avro/v10 v10.2.1/go.mod h1:QUhjeHPchheYmMDni/Nx7VB0RsT/ee8YIgGY/xpEQgQ= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20= github.com/frankban/quicktest v1.7.2/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o= github.com/frankban/quicktest v1.10.0/go.mod h1:ui7WezCLWMWxVWr1GETZY3smRy0G4KWq9vcPtJmFl7Y= @@ -33,13 +35,22 @@ github.com/linkedin/goavro/v2 v2.11.1 h1:4cuAtbDfqkKnBXp9E+tRkIJGa6W6iAjwonwt8O1 github.com/linkedin/goavro/v2 v2.11.1/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM= github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/sebdah/goldie/v2 v2.5.5 h1:rx1mwF95RxZ3/83sdS4Yp7t2C5TCokvWP4TBRbAyEWY= +github.com/sebdah/goldie/v2 v2.5.5/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI= +github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -76,4 +87,6 @@ gopkg.in/retry.v1 v1.0.3 h1:a9CArYczAVv6Qs6VGoLMio99GEs7kY9UzSF9+LD+iGs= gopkg.in/retry.v1 v1.0.3/go.mod h1:FJkXmWiMaAo7xB+xhvDF59zhfjDWyzmyAxiT4dB688g= gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=