Open
Description
Contact Details
No response
What is the problem?
At the moment GenZ uses a template to render go code. GenZ workflow could be described as follows:
flowchart LR
parser[Parse Go code] ---> object[GenZ parsed element]
generator[GenZ generator] --->|Consume|object
generator --->|Inject ParsedElement|Template
Template --->|Generate| code[Go code]
There are some caveats with this approach:
- Template is hard to write as there are no common linting solutions or syntax checkers.
What is the solution?
On viable solution could be to expose a fluent API, to write Go code with Go code. It ensures syntax consistency, flexibility, and hardened testing.
API Definition
Code object
// ...
Code(buffer, "name").
WithHeader("My Header").
WithHeaders([]string{"line 1", "line 2"}).
WithImport("test").
WithImports([]string{"hello", "world"}).
WithNamedImport("b", "bob")
WithNamedImports(map[string]string{
"t": "toto"
}).Generate()
// ...
// My Header
// line 1
// line 2
package name
import (
"test"
"hello"
"world"
b "bob"
t "toto"
)
Declaration
A declaration is defined as the following interface:
type Declaration interface {
Generate() string
}
// ...
Code(buffer, "name").
WithDeclaration(MyDeclaration).
WithDeclarations([]Declaration{MyDeclarationOne, MyDeclarationTwo}).
Generate()
// ...
Function Declaration
// ...
FuncDecl("HelloWorld").
WithDoc("HelloWorld does blabla").
WithReceiver("m", "MyReceiver", true).
WithParameter("name", "string").
WithParamaters(map[string]string{
"world": "string",
"n": "int"
}).
WithReturn("string").
WithReturns([]string{"int", "error"}).
WithBody(`return "", 0, nil`).
Generate()
// ...
// HelloWorld does blabla
func (m *MyReceiver) HelloWorld(name string, world string, n int) (string, int, error) {
return "", 0, nil
}
Struct Declaration
// ...
StructDecl("MyStruct").
WithDoc("A comment").
WithAttribute("integer" "int", "json:int").
WithAttributes(??).
Generate()
// ...
// A comment
type MyStruct struct {
integer int `json: int`
hello func() string
}
Interface Declaration
Why is this important?
A reason happened!
What are the alternatives?
An alternative happened!
What is the impact?
An impact happened!
Code of Conduct
- I agree to follow this project's Code of Conduct