@@ -20,6 +20,7 @@ type Config struct {
2020 Tags map [string ]TagStyle
2121 FullName bool
2222 Encoders bool
23+ Initialisms []string
2324}
2425
2526// TagStyle defines the styling for a tag.
@@ -120,6 +121,7 @@ func StructFromSchema(schema avro.Schema, w io.Writer, cfg Config) error {
120121 opts := []OptsFunc {
121122 WithFullName (cfg .FullName ),
122123 WithEncoders (cfg .Encoders ),
124+ WithInitialisms (cfg .Initialisms ),
123125 }
124126 g := NewGenerator (strcase .ToSnake (cfg .PackageName ), cfg .Tags , opts ... )
125127 g .Parse (rec )
@@ -160,16 +162,27 @@ func WithEncoders(b bool) OptsFunc {
160162 }
161163}
162164
165+ // WithInitialisms configures the generator to use additional custom initialisms
166+ // when styling struct and field names.
167+ func WithInitialisms (ss []string ) OptsFunc {
168+ return func (g * Generator ) {
169+ g .initialisms = ss
170+ }
171+ }
172+
163173// Generator generates Go structs from schemas.
164174type Generator struct {
165- pkg string
166- tags map [string ]TagStyle
167- fullName bool
168- encoders bool
175+ pkg string
176+ tags map [string ]TagStyle
177+ fullName bool
178+ encoders bool
179+ initialisms []string
169180
170181 imports []string
171182 thirdPartyImports []string
172183 typedefs []typedef
184+
185+ nameCaser * strcase.Caser
173186}
174187
175188// NewGenerator returns a generator.
@@ -183,6 +196,17 @@ func NewGenerator(pkg string, tags map[string]TagStyle, opts ...OptsFunc) *Gener
183196 opt (g )
184197 }
185198
199+ initialisms := map [string ]bool {}
200+ for _ , v := range g .initialisms {
201+ initialisms [v ] = true
202+ }
203+
204+ g .nameCaser = strcase .NewCaser (
205+ true , // use standard Golint's initialisms
206+ initialisms ,
207+ nil , // use default word split function
208+ )
209+
186210 return g
187211}
188212
@@ -231,17 +255,17 @@ func (g *Generator) generate(schema avro.Schema) string {
231255
232256func (g * Generator ) resolveTypeName (s avro.NamedSchema ) string {
233257 if g .fullName {
234- return strcase . ToGoPascal (s .FullName ())
258+ return g . nameCaser . ToPascal (s .FullName ())
235259 }
236- return strcase . ToGoPascal (s .Name ())
260+ return g . nameCaser . ToPascal (s .Name ())
237261}
238262
239263func (g * Generator ) resolveRecordSchema (schema * avro.RecordSchema ) string {
240264 fields := make ([]field , len (schema .Fields ()))
241265 for i , f := range schema .Fields () {
242266 typ := g .generate (f .Type ())
243267 tag := f .Name ()
244- fields [i ] = g .newField (strcase . ToGoPascal (f .Name ()), typ , tag )
268+ fields [i ] = g .newField (g . nameCaser . ToPascal (f .Name ()), typ , tag )
245269 }
246270
247271 typeName := g .resolveTypeName (schema )
0 commit comments