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

[Enhancement]: Consume GenZ as code - Proposal WIP #38

Open
1 task done
Joffref opened this issue Jan 22, 2024 · 0 comments · May be fixed by #36
Open
1 task done

[Enhancement]: Consume GenZ as code - Proposal WIP #38

Joffref opened this issue Jan 22, 2024 · 0 comments · May be fixed by #36
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@Joffref
Copy link
Owner

Joffref commented Jan 22, 2024

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]
Loading

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
@Joffref Joffref added documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed question Further information is requested labels Jan 22, 2024
@Joffref Joffref self-assigned this Jan 22, 2024
@Joffref Joffref linked a pull request Jan 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant