Skip to content

Commit

Permalink
go: generate *Ctx version of public functions (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
passichenko authored Sep 23, 2024
1 parent dbf2889 commit 44354c9
Showing 1 changed file with 32 additions and 12 deletions.
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
}`,
}
`,
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
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

0 comments on commit 44354c9

Please sign in to comment.