Skip to content

Commit

Permalink
feat(codegen): convert-keys run option (#199)
Browse files Browse the repository at this point in the history
[Breaking Change] this commit introduces a new run option called `convert-keys` as discussed in #129.

Supported values: `snake`, `camel`, `kebab`, `none`. Default value is `none`.

---------

Co-authored-by: Louis FRADIN <louis.fradin@gmail.com>
  • Loading branch information
lo00l and lerenn committed Apr 16, 2024
1 parent 8869a48 commit 946a713
Show file tree
Hide file tree
Showing 44 changed files with 2,523 additions and 57 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ your broker to the generated code.

## CLI options

The default options for oapi-codegen will generate everything; user, application,
### Generation parts (`-g`)

The default options for asyncapi-codegen will generate everything; user, application,
and type definitions but you can generate subsets of those via the -generate
flag. It defaults to user,application,types
but you can specify any combination of those.
Expand All @@ -303,6 +305,40 @@ Here are the universal parts that you can generate:
This will be everything under `#components`, as well as request parameter,
request body, and response type objects.

### JSON keys conversion (`--convert-keys`)

By default, the generation will use the key specified in the asyncapi codegen.
This is also called the `none` convertion. You can also convert your keys to
`snake`, `camel`, or `kebab`.

#### Example

Given this schema:

```yaml
Payload:
type: object
properties:
This_is a-Property:
type: string
```
Here are the generated JSON sent, given by the different options:
```json
/* No conversion (none) */
{ "This_is a-Property": "value" }

/* Camel case (camel) */
{ "ThisIsAProperty": "value" }

/* Kebab case (kebab) */
{ "this-is-a-property": "value" }

/* Snale case (snake) */
{ "this_is_a_property": "value" }
```

## Advanced topics

### Middlewares
Expand Down
7 changes: 7 additions & 0 deletions cmd/asyncapi-codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Flags struct {
// DisableFormatting states if the formatting should be disabled when
// writing the generated code
DisableFormatting bool

// ConvertKeys defines a schema property keys conversion strategy.
// Supported values: snake, camel, kebab, none
ConvertKeys string
}

// ProcessFlags processes command line flags and fill the Flags structure with them.
Expand All @@ -46,6 +50,8 @@ func ProcessFlags() Flags {
flag.StringVar(&f.PackageName, "p", "asyncapi", "Golang package name")
flag.StringVar(&f.Generate, "g", "user,application,types", "Generation options")
flag.BoolVar(&f.DisableFormatting, "disable-formatting", false, "Disables the code generation formatting")
flag.StringVar(&f.ConvertKeys, "convert-keys", "none",
"Schema property key names conversion strategy. Supported values: snake, camel, kebab, none")

flag.Parse()

Expand All @@ -58,6 +64,7 @@ func (f Flags) ToCodegenOptions() (options.Options, error) {
OutputPath: f.OutputPath,
PackageName: f.PackageName,
DisableFormatting: f.DisableFormatting,
ConvertKeys: f.ConvertKeys,
}

if f.Generate != "" {
Expand Down
6 changes: 6 additions & 0 deletions cmd/asyncapi-codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/lerenn/asyncapi-codegen/pkg/codegen"
"github.com/lerenn/asyncapi-codegen/pkg/utils/template"
)

func run() int {
Expand All @@ -22,6 +23,11 @@ func run() int {
return 255
}

if err := template.SetConvertKeyFn(opt.ConvertKeys); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
return 255
}

if err := cg.Generate(opt); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
return 255
Expand Down
4 changes: 2 additions & 2 deletions examples/ping/v2/kafka/app/app.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v2/kafka/user/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v2/nats-jetstream/app/app.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v2/nats-jetstream/user/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v2/nats/app/app.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v2/nats/user/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v3/kafka/app/app.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v3/kafka/user/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v3/nats-jetstream/app/app.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v3/nats-jetstream/user/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v3/nats/app/app.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ping/v3/nats/user/user.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (t *{{ .Name }}) UnmarshalJSON(data []byte) error {
t.AdditionalProperties = make(map[string]{{template "schema-name" .AdditionalProperties}}, len(m))
for k, v := range m {
switch k {
{{- range $key, $value := .Properties -}}
case "{{snakeCase $key}}":
{{ range $key, $value := .Properties -}}
case "{{convertKey $key}}":
continue
{{- end}}
{{ end -}}
default:
t.AdditionalProperties[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/generators/v2/templates/schema_definition.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type {{ .Name }} struct {
{{else if and $value.ReferenceTo $value.ReferenceTo.Description}}
// Description: {{$value.ReferenceTo.Description}}
{{end -}}
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `json:"{{snakeCase $key}}"`
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `json:"{{convertKey $key}}"`
{{end -}}

{{- if .AdditionalProperties}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (t *{{ .Name }}) UnmarshalJSON(data []byte) error {
t.AdditionalProperties = make(map[string]{{template "schema-name" .AdditionalProperties}}, len(m))
for k, v := range m {
switch k {
{{- range $key, $value := .Properties -}}
case "{{snakeCase $key}}":
{{ range $key, $value := .Properties -}}
case "{{convertKey $key}}":
continue
{{- end}}
{{ end -}}
default:
t.AdditionalProperties[k] = v
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/generators/v3/templates/schema_definition.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type {{ .Name }} struct {
{{else if and $value.ReferenceTo $value.ReferenceTo.Description}}
// Description: {{$value.ReferenceTo.Description}}
{{end -}}
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `json:"{{snakeCase $key}}"`
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `json:"{{convertKey $key}}"`
{{end -}}

{{- if .AdditionalProperties}}
Expand Down
4 changes: 4 additions & 0 deletions pkg/codegen/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ type Options struct {
// DisableFormatting states if the formatting should be disabled when
// writing the generated code
DisableFormatting bool

// ConvertKeys defines a schema property keys conversion strategy.
// Supported values: snake, camel, kebab, none
ConvertKeys string
}
Loading

0 comments on commit 946a713

Please sign in to comment.