Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom initialisms support to gen package and avrogen #303

Merged
merged 4 commits into from
Sep 6, 2023
Merged

feat: add custom initialisms support to gen package and avrogen #303

merged 4 commits into from
Sep 6, 2023

Conversation

hhromic
Copy link
Contributor

@hhromic hhromic commented Sep 5, 2023

Hello, this simple PR adds support for custom initialisms to the gen package and avrogen command.

Additional custom initialisms are necessary because strcase.ToGoPascal only uses GoLint's list of initialisms.
Currently, if an initialism in struct/field names is not part of Golint's, then strcase wrongly downcases it.

For example, consider the following schema containing the domain initialism AID:

{
  "type": "record",
  "name": "AIDEventHTTPService",
  "namespace": "example",
  "fields" : [
    {"name": "AIDNumber", "type": "long"},
    {"name": "Desc", "type": "string"}
  ]
}

Currently, avrogen generates the following Go struct (struct/field names: AID -> Aid):

package example

// Code generated by avro/gen. DO NOT EDIT.

// AidEventHTTPService is a generated struct.
type AidEventHTTPService struct {
        AidNumber int64  `avro:"AIDNumber"`
        Desc      string `avro:"Desc"`
}

With this PR, -initialisms AID can be given to avrogen to generate the correct struct/field names:

package example

// Code generated by avro/gen. DO NOT EDIT.

// AIDEventHTTPService is a generated struct.
type AIDEventHTTPService struct {
        AIDNumber int64  `avro:"AIDNumber"`
        Desc      string `avro:"Desc"`
}

There is a related PR in the upstream strcase repository: ettle/strcase#13
That PR allows to preserve any extra initialisms without having to define them in advance, however the maintainers seems to not be merging it any time soon. This PR allows to solve the issue in the mean time.

Copy link
Member

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nitpick about test error assertion, otherwise looks good.

cmd/avrogen/main_test.go Outdated Show resolved Hide resolved
cmd/avrogen/main_test.go Outdated Show resolved Hide resolved
cmd/avrogen/main_test.go Outdated Show resolved Hide resolved
@hhromic
Copy link
Contributor Author

hhromic commented Sep 6, 2023

One nitpick about test error assertion, otherwise looks good.

Thanks for the feedback, indeed looks cleaner after your suggestion.

I merged main too into this branch. Thanks for being open to contributions! Appreciated!

Copy link
Member

@nrwiersma nrwiersma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@nrwiersma nrwiersma merged commit ca141fc into hamba:main Sep 6, 2023
2 checks passed
@hhromic hhromic deleted the gen-initialisms branch September 6, 2023 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants