From 043d2c362bf5a723cc0233c95a75718c87561fb7 Mon Sep 17 00:00:00 2001 From: Michael Pleshakov Date: Tue, 5 Mar 2024 15:46:17 -0500 Subject: [PATCH] Remove field name decapitalization in the scheme Problem: For consistency with DataFabric examples, we make field names start with lowercase letter. As a result, we convert the first character of field name in a go struct to lowercase. For example, MyField -> myField. However, the generated scheme names for fields like HTTPRoutes look a bit ugly: HTTPRoutes -> hTTPRoutes Solution: This is not a hard requirement for Avro scheme, we can use names like HTTPRoutes just fine. long? HTTPRoutes = null; CLOSES -- https://github.com/nginxinc/telemetry-exporter/issues/33 --- cmd/generator/scheme.go | 9 ++------- cmd/generator/tests/data.avdl | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/cmd/generator/scheme.go b/cmd/generator/scheme.go index df06259..2d8294a 100644 --- a/cmd/generator/scheme.go +++ b/cmd/generator/scheme.go @@ -6,7 +6,6 @@ import ( "fmt" "go/types" "io" - "strings" "text/template" ) @@ -42,10 +41,6 @@ func getAvroPrimitiveType(kind types.BasicKind) string { } } -func getAvroFieldName(name string) string { - return strings.ToLower(name[:1]) + name[1:] -} - type schemeGen struct { Namespace string Protocol string @@ -78,7 +73,7 @@ func generateScheme(writer io.Writer, cfg schemeGenConfig) error { schemeFields = append(schemeFields, schemeField{ Comment: f.docString, Type: fmt.Sprintf("union {null, array<%s>}", getAvroPrimitiveType(f.fieldType)), - Name: getAvroFieldName(f.name), + Name: f.name, }) } else if f.embeddedStruct { createSchemeFields(f.embeddedStructFields) @@ -86,7 +81,7 @@ func generateScheme(writer io.Writer, cfg schemeGenConfig) error { schemeFields = append(schemeFields, schemeField{ Comment: f.docString, Type: getAvroPrimitiveType(f.fieldType) + "?", - Name: getAvroFieldName(f.name), + Name: f.name, }) } } diff --git a/cmd/generator/tests/data.avdl b/cmd/generator/tests/data.avdl index 36c96af..41d8078 100644 --- a/cmd/generator/tests/data.avdl +++ b/cmd/generator/tests/data.avdl @@ -9,53 +9,53 @@ /** SomeString is a string field. */ - string? someString = null; + string? SomeString = null; /** SomeInt is an int64 field. */ - long? someInt = null; + long? SomeInt = null; /** SomeFloat is a float64 field. More comments. */ - double? someFloat = null; + double? SomeFloat = null; /** SomeBool is a bool field. */ - boolean? someBool = null; + boolean? SomeBool = null; /** SomeStrings is a slice of strings. */ - union {null, array} someStrings = null; + union {null, array} SomeStrings = null; /** SomeInts is a slice of int64. */ - union {null, array} someInts = null; + union {null, array} SomeInts = null; /** SomeFloats is a slice of float64. */ - union {null, array} someFloats = null; + union {null, array} SomeFloats = null; /** SomeBools is a slice of bool. */ - union {null, array} someBools = null; + union {null, array} SomeBools = null; /** AnotherSomeString is a string field. */ - string? anotherSomeString = null; + string? AnotherSomeString = null; /** AnotherSomeInt is an int64 field. */ - long? anotherSomeInt = null; + long? AnotherSomeInt = null; /** AnotherSomeFloat is a float64 field. */ - double? anotherSomeFloat = null; + double? AnotherSomeFloat = null; /** AnotherSomeBool is a bool field. */ - boolean? anotherSomeBool = null; + boolean? AnotherSomeBool = null; /** AnotherSomeStrings is a slice of strings. */ - union {null, array} anotherSomeStrings = null; + union {null, array} AnotherSomeStrings = null; /** AnotherSomeInts is a slice of int64. */ - union {null, array} anotherSomeInts = null; + union {null, array} AnotherSomeInts = null; /** AnotherSomeFloats is a slice of float64. */ - union {null, array} anotherSomeFloats = null; + union {null, array} AnotherSomeFloats = null; /** AnotherSomeBools is a slice of bool. */ - union {null, array} anotherSomeBools = null; + union {null, array} AnotherSomeBools = null; } }