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

fix: remove param names from params struct name #111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/codegen/generators/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func loadTemplate(paths ...string) (*template.Template, error) {

func templateFunctions() template.FuncMap {
return template.FuncMap{
"namifyWithoutParam": templates.NamifyWithoutParams,
"namify": templates.Namify,
"snakeCase": templates.SnakeCase,
"referenceToStructAttributePath": templates.ReferenceToStructAttributePath,
Expand Down
8 changes: 4 additions & 4 deletions pkg/codegen/generators/templates/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *{{ .Prefix }}Controller) UnsubscribeAll(ctx context.Context) {
//
// Callback function 'fn' will be called each time a new message is received.
{{- if .Parameters}}
func (c *{{ $.Prefix }}Controller) Subscribe{{operationName $value}}(ctx context.Context, params {{namify $key}}Parameters, fn func (ctx context.Context, msg {{channelToMessageTypeName $value}})) error {
func (c *{{ $.Prefix }}Controller) Subscribe{{operationName $value}}(ctx context.Context, params {{namifyWithoutParam $key}}Parameters, fn func (ctx context.Context, msg {{channelToMessageTypeName $value}})) error {
{{- else}}
func (c *{{ $.Prefix }}Controller) Subscribe{{operationName $value}}(ctx context.Context, fn func (ctx context.Context, msg {{channelToMessageTypeName $value}})) error {
{{- end }}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (c *{{ $.Prefix }}Controller) Subscribe{{operationName $value}}(ctx context
// Unsubscribe{{operationName $value}} will unsubscribe messages from '{{$key}}' channel.
// A timeout can be set in context to avoid blocking operation, if needed.
{{- if .Parameters}}
func (c *{{ $.Prefix }}Controller) Unsubscribe{{operationName $value}}(ctx context.Context, params {{namify $key}}Parameters) {
func (c *{{ $.Prefix }}Controller) Unsubscribe{{operationName $value}}(ctx context.Context, params {{namifyWithoutParam $key}}Parameters) {
{{- else}}
func (c *{{ $.Prefix }}Controller) Unsubscribe{{operationName $value}}(ctx context.Context) {
{{- end}}
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *{{ $.Prefix }}Controller) Unsubscribe{{operationName $value}}(ctx conte
{{- range $key, $value := .PublishChannels}}
// Publish{{operationName $value}} will publish messages to '{{$key}}' channel
{{- if .Parameters }}
func (c *{{ $.Prefix }}Controller) Publish{{operationName $value}}(ctx context.Context, params {{namify $key}}Parameters, msg {{channelToMessageTypeName $value}}) error {
func (c *{{ $.Prefix }}Controller) Publish{{operationName $value}}(ctx context.Context, params {{namifyWithoutParam $key}}Parameters, msg {{channelToMessageTypeName $value}}) error {
{{- else }}
func (c *{{ $.Prefix }}Controller) Publish{{operationName $value}}(ctx context.Context, msg {{channelToMessageTypeName $value}}) error {
{{- end }}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (c *{{ $.Prefix }}Controller) Publish{{operationName $value}}(ctx context.C
//
// A timeout can be set in context to avoid blocking operation, if needed.
{{- if .Parameters}}
func (c *UserController) WaitFor{{operationName $value}}(ctx context.Context, params {{namify $key}}Parameters, publishMsg MessageWithCorrelationID, pub func(ctx context.Context) error) ({{channelToMessageTypeName $value}}, error) {
func (c *UserController) WaitFor{{operationName $value}}(ctx context.Context, params {{namifyWithoutParam $key}}Parameters, publishMsg MessageWithCorrelationID, pub func(ctx context.Context) error) ({{channelToMessageTypeName $value}}, error) {
{{- else}}
func (c *UserController) WaitFor{{operationName $value}}(ctx context.Context, publishMsg MessageWithCorrelationID, pub func(ctx context.Context) error) ({{channelToMessageTypeName $value}}, error) {
{{- end}}
Expand Down
10 changes: 10 additions & 0 deletions pkg/codegen/generators/templates/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
"github.com/stoewer/go-strcase"
)

// NamifyWithoutParams will convert a sentence to a golang conventional type name.
// and will remove all parameters that can appear between '{' and '}'.
func NamifyWithoutParams(sentence string) string {
// Remove parameters
re := regexp.MustCompile("{[^()]*}")
sentence = string(re.ReplaceAll([]byte(sentence), []byte("_")))

return Namify(sentence)
}

// Namify will convert a sentence to a golang conventional type name.
func Namify(sentence string) string {
// Remove everything except alphanumerics and '_'
Expand Down
59 changes: 36 additions & 23 deletions pkg/codegen/generators/templates/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,46 @@ type HelpersSuite struct {
suite.Suite
}

type namifyCases struct {
In string
Out string
}

var namifyBaseCases = []namifyCases{
// Remove leading digits
{In: "0name0", Out: "Name0"},
// Remove non alphanumerics
{In: "?#!name", Out: "Name"},
// Capitalize
{In: "name", Out: "Name"},
// Snake Case
{In: "eh_oh__ah", Out: "EhOhAh"},
// With acronym in middle
{In: "IdTata", Out: "IDTata"},
// With acronym in middle
{In: "TotoIdLala", Out: "TotoIDLala"},
// With acronym at the end
{In: "TotoId", Out: "TotoID"},
// Without acronym, but still the same letters as the acronym
{In: "identity", Out: "Identity"},
{In: "Identity", Out: "Identity"},
{In: "covid", Out: "Covid"},
}

func (suite *HelpersSuite) TestNamify() {
cases := []struct {
In string
Out string
}{
// Remove leading digits
{In: "0name0", Out: "Name0"},
// Remove non alphanumerics
{In: "?#!name", Out: "Name"},
// Capitalize
{In: "name", Out: "Name"},
// Snake Case
{In: "eh_oh__ah", Out: "EhOhAh"},
// With acronym in middle
{In: "IdTata", Out: "IDTata"},
// With acronym in middle
{In: "TotoIdLala", Out: "TotoIDLala"},
// With acronym at the end
{In: "TotoId", Out: "TotoID"},
// Without acronym, but still the same letters as the acronym
{In: "identity", Out: "Identity"},
{In: "Identity", Out: "Identity"},
{In: "covid", Out: "Covid"},
for i, c := range namifyBaseCases {
suite.Require().Equal(c.Out, Namify(c.In), i)
}
}

func (suite *HelpersSuite) TestNamifyWithoutParams() {
cases := []namifyCases{
// With argument
{In: "name.{id}", Out: "Name"},
}

for i, c := range cases {
suite.Require().Equal(c.Out, Namify(c.In), i)
suite.Require().Equal(c.Out, NamifyWithoutParams(c.In), i)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/codegen/generators/templates/types.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (e *Error) Error() string {
{{range $key, $value := .Channels -}}

{{- if $value.Parameters -}}
// {{ namify .Name }}Parameters represents {{ namify .Name }} channel parameters
type {{ namify .Name }}Parameters struct {
// {{ namifyWithoutParam .Name }}Parameters represents {{ namify .Name }} channel parameters
type {{ namifyWithoutParam .Name }}Parameters struct {
{{- range $key, $value := .Parameters}}
{{- template "parameter" $value}}
{{- end}}
Expand Down