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

go: generate *Ctx version of public functions #457

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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
44 changes: 32 additions & 12 deletions pkg/gen/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,11 @@ type configCodeTemplate struct {
func (g *goGenerator) getDefaultTemplateBody() *configCodeTemplate {
return &configCodeTemplate{
public: `// {{$.Description}}
func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) {{$.RetType}} {
{{ $.CtxStuff }}
func (c *LekkoClient) {{$.FuncName}}{{ if $.PassCtx }}Ctx{{end}}({{ if $.PassCtx }}ctx context.Context, {{end}}{{$.ArgumentString}}) {{$.RetType}} {
{{- if not $.PassCtx}}
ctx := context.Background()
{{ else -}}{{- end -}}
{{$.CtxStuff }}
result, err := c.{{$.GetFunction}}(ctx, "{{$.Namespace}}", "{{$.Key}}")
if err == nil {
return result
Expand All @@ -401,19 +404,24 @@ func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) {{$.RetType}} {
}
debug.LogDebug("Lekko fallback", "name", "{{$.Namespace}}/{{$.Key}}", "result", result)
return result
}`,
}
`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here and below: this is to have a newline after each function

Copy link
Member

Choose a reason for hiding this comment

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

I thought gofmt should handle this automatically 🤔 but don't mind this change regardless

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in this case the issue was that without a newline the description of the context-aware function was on the same line as the closing } and gofmt can't fix that

private: `// {{$.Description}}
func {{$.PrivateFunc}}({{$.ArgumentString}}) {{$.RetType}} {
{{range $.NativeLanguage}}{{ . }}
{{end}}}`,
{{end}}}
`,
}
}

// Template body for proto config code
func (g *goGenerator) getProtoTemplateBody() *configCodeTemplate {
return &configCodeTemplate{
public: `// {{$.Description}}
func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) *{{$.RetType}} {
func (c *LekkoClient) {{$.FuncName}}{{ if $.PassCtx }}Ctx{{end}}({{ if $.PassCtx }}ctx context.Context, {{end}}{{$.ArgumentString}}) *{{$.RetType}} {
{{- if not $.PassCtx}}
ctx := context.Background()
{{ else -}}{{- end -}}
{{ $.CtxStuff }}
ret := &{{$.RetType}}{}
result, err := c.GetAny(ctx, "{{$.Namespace}}", "{{$.Key}}")
Expand All @@ -427,11 +435,13 @@ func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) *{{$.RetType}} {
}
debug.LogDebug("Lekko fallback", "name", "{{$.Namespace}}/{{$.Key}}", "result", ret)
return ret
}`,
}
`,
private: `// {{$.Description}}
func {{$.PrivateFunc}}({{$.ArgumentString}}) *{{$.RetType}} {
{{range $.NativeLanguage}}{{ . }}
{{end}}}`,
{{end}}}
`,
}
}

Expand All @@ -447,14 +457,18 @@ const (
)

// {{$.Description}}
func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) {{$.RetType}} {
func (c *LekkoClient) {{$.FuncName}}{{ if $.PassCtx }}Ctx{{end}}({{ if $.PassCtx }}ctx context.Context, {{end}}{{$.ArgumentString}}) {{$.RetType}} {
{{- if not $.PassCtx}}
ctx := context.Background()
{{ else -}}{{- end -}}
{{ $.CtxStuff }}
result, err := c.{{$.GetFunction}}(ctx, "{{$.Namespace}}", "{{$.Key}}")
if err == nil {
return result
}
return {{$.PrivateFunc}}({{$.CallString}})
}`,
}
`,
private: `type {{$.EnumTypeName}} string
const (
{{range $index, $field := $.EnumFields}}{{$field.Name}} {{$.EnumTypeName}} = "{{$field.Value}}"
Expand All @@ -464,7 +478,8 @@ const (
// {{$.Description}}
func {{$.PrivateFunc}}({{$.ArgumentString}}) {{$.RetType}} {
{{range $.NativeLanguage}}{{ . }}
{{end}}}`,
{{end}}}
`,
}
}

Expand Down Expand Up @@ -616,6 +631,7 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
CallString string
EnumTypeName string
EnumFields []EnumField
PassCtx bool // whether the public function will accept the context or create it
CtxStuff string
ProtoStructFilling string
}{
Expand All @@ -631,6 +647,7 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
"",
enumTypeName,
enumFields,
false,
"",
protoStructFilling,
}
Expand All @@ -644,7 +661,6 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
data.ArgumentString = fmt.Sprintf("args *%s", staticCtxType.Type)
}
data.CallString = "args"
data.CtxStuff = "ctx := context.Background()\n"
mt, err := g.TypeRegistry.FindMessageByURL(staticCtxType.TypeUrl)
if err != nil {
panic(err)
Expand All @@ -655,7 +671,6 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
data.CtxStuff = data.CtxStuff + fmt.Sprintf("ctx = client.Add(ctx, \"%s\", args.%s)\n", fieldName, strcase.ToCamel(fieldName))
}
} else {
data.CtxStuff = "ctx := context.Background()\n"
data.NativeLanguage = g.translateFeature(f, protoType, false, usedVariables, &generated.usedStrings, &generated.usedSlices)
var arguments []string
var ctxAddLines []string
Expand Down Expand Up @@ -684,6 +699,11 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
if err := templ.Execute(&ret, data); err != nil {
return nil, err
}
// generate context-aware variant, e.g. GetFooCtx(ctx context.Context, ...)
data.PassCtx = true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is ugly and relies on the fact that we don't use PassCtx in private functions
but it was the simplest way to reuse the template

Copy link
Member

Choose a reason for hiding this comment

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

nit: a comment here to explain the reasoning behind doing this twice would be helpful

if err := templ.Execute(&ret, data); err != nil {
return nil, err
}
generated.public = ret.String()
}
if templ, err := template.New("private func").Parse(templateBody.private); err != nil {
Expand Down