-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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}}") | ||
|
@@ -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}}} | ||
`, | ||
} | ||
} | ||
|
||
|
@@ -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}}" | ||
|
@@ -464,7 +478,8 @@ const ( | |
// {{$.Description}} | ||
func {{$.PrivateFunc}}({{$.ArgumentString}}) {{$.RetType}} { | ||
{{range $.NativeLanguage}}{{ . }} | ||
{{end}}}`, | ||
{{end}}} | ||
`, | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
}{ | ||
|
@@ -631,6 +647,7 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR | |
"", | ||
enumTypeName, | ||
enumFields, | ||
false, | ||
"", | ||
protoStructFilling, | ||
} | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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