Joffref / Joffref/genz

[Enhancement]: Consume GenZ as code - Proposal WIP

Open
#38 0 comments 0 reactions 1 assignee Claimed by @Joffref View on GitHub
documentation enhancement help wanted question
Dominant language
Go
Stars
7
Forks
3
PR merge metrics
No merged PRs in 30d

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:
```mermaid
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

```go
// ...
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()
// ...
```

```go
// 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:

```go
type Declaration interface {
Generate() string
}
```

```go
// ...
Code(buffer, "name").
WithDeclaration(MyDeclaration).
WithDeclarations([]Declaration{MyDeclarationOne, MyDeclarationTwo}).
Generate()
// ...
```

### Function Declaration

```go
// ...
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()
// ...
```

```go
// HelloWorld does blabla
func (m *MyReceiver) HelloWorld(name string, world string, n int) (string, int, error) {
return "", 0, nil
}
```

### Struct Declaration

```go
// ...
StructDecl("MyStruct").
WithDoc("A comment").
WithAttribute("integer" "int", "json:int").
WithAttributes(??).
Generate()
// ...
```

```go
// 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

- [X] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.