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

WIP: PoC expose a fluent api to generate code #36

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- run: make deps
- run: make lint
- run: make install
- run: make test
- run: make build
- run: make test-examples
if: matrix.os != 'windows-latest'
- name: Download dependencies
run: go mod download
- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
- name: Run golangci-lint
run: golangci-lint run
- name: Run tests
run: go test -v ./...
1 change: 0 additions & 1 deletion .go-version

This file was deleted.

Empty file removed ADOPTERS.md
Empty file.
Empty file removed CHANGELOG.md
Empty file.
33 changes: 0 additions & 33 deletions Makefile

This file was deleted.

131 changes: 0 additions & 131 deletions cmd/genz/genz.go

This file was deleted.

37 changes: 0 additions & 37 deletions cmd/genz/root.go

This file was deleted.

46 changes: 0 additions & 46 deletions cmd/genz/test.go

This file was deleted.

38 changes: 0 additions & 38 deletions cmd/genz/version.go

This file was deleted.

55 changes: 55 additions & 0 deletions examples/1_validator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"bytes"
genz2 "github.com/Joffref/genz/genz"
"github.com/Joffref/genz/genz/cli"
"github.com/Joffref/genz/genz/models"
"io"
)

func main() {
err := cli.NewCommandFromGenerator("validator", MyCustomGenerator).Execute()
if err != nil {
return
}
}

func MyCustomGenerator(file io.Writer, element models.ParsedElement) error {
code := genz2.NewCode(file, element.PackageName).
WithHeaderComments("File generated by GenZ with template validator").
WithImports("fmt", "unicode").
WithDeclarations(
genz2.Function("Validate").
WithReceiver("v", element.Type.InternalName, false).
WithReturns("error").
WithBody(body(element.Element)),
)
err := code.Generate()
if err != nil {
return err
}
return nil
}

func body(element models.Element) string {
var buf bytes.Buffer
for _, attribute := range element.Attributes {
if attribute.Type.InternalName == "string" {
for _, comment := range attribute.Comments {
if comment == "+required" {
buf.WriteString("if v." + attribute.Name + " == \"\" {\n")
buf.WriteString("return fmt.Errorf(\"attribute '" + attribute.Name + "' must be set\")\n")
buf.WriteString("}\n")
}
if comment == "+startsWithCapital" {
buf.WriteString("if v." + attribute.Name + " != \"\" && !unicode.IsUpper(rune(v." + attribute.Name + "[0])) {\n")
buf.WriteString("return fmt.Errorf(\"attribute '" + attribute.Name + "' should start with a capital letter\")\n")
buf.WriteString("}\n")
}
}
}
}
buf.WriteString("return nil")
return buf.String()
}
Loading
Loading