Skip to content

Commit

Permalink
README version 0.1
Browse files Browse the repository at this point in the history
Signed-off-by: Joffref <[email protected]>
  • Loading branch information
Joffref committed Nov 10, 2023
1 parent d27dda7 commit 4ba2cf6
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 16 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SUPPORTED_ARCH=amd64 arm64
install:
go mod download
go install github.com/golangci/golangci-lint/cmd/[email protected]
go install .

lint:
golangci-lint run
Expand Down
141 changes: 135 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,139 @@
# GenZ
## Requirements

- [Go >1.20](https://go.dev/doc/install)
## Try it out
Explore built-in `examples`, clone repo, and run
[![Go Reference](https://pkg.go.dev/badge/github.com/Joffref/genz.svg)](https://pkg.go.dev/github.com/Joffref/genz)
[![Go Report Card](https://goreportcard.com/badge/github.com/Joffref/genz)](https://goreportcard.com/report/github.com/Joffref/genz)
[![License](https://img.shields.io/github/license/Joffref/genz)](LICENSE)
![GitHub stars](https://img.shields.io/github/stars/Joffref/genz)
> At this moment, this project is in development. It is not ready for production use.
> Refers to the [Roadmap](#roadmap) section for more information on what has been done.
>
> See the [Contributing](#contributing) section if you would like to help make it better.

<p align="center" width="100%">
<img width="50%" src="./docs/assets/logo_500_500.png">
</p>

GenZ is your all-in-one opinionated code generator for Go. It leverages the power of Go templates to generate code from your own templates.

Thanks to GenZ, you can generate code for your own needs, without having to write a single line of code.


## Installation

You can install GenZ in different ways, depending on your needs.
We strongly recommend using the Go version for production use.

### Using Go - Production

```bash
go install github.com/Joffref/genz
```

### From source - Development

```bash
git clone https://github.com/Joffref/genz.git
cd genz
make install
```

## Usage

Here is a simple example of how to use GenZ.

### Go

Let say you have a `main.tmpl` file in your project, with the following content:
```gotemplate
package main
import (
"fmt"
)
func (v {{ .Type }}) Validate() error {
{{ range .Attributes }} {{$attribute := .}}
{{ if eq .Type "string" }}{{ range .Comments }}
{{ if eq . "+required" }}
if v.{{ $attribute.Name }} == "" {
return fmt.Errorf("attribute '{{ $attribute.Name }}' must be set")
}
{{ end }}{{ end }}{{ end }}{{ end }}
return nil
}
```
And the following `human.go` file:
```go
package main

type Human struct {
// +required
Firstname string
// +required
Lastname string
Age uint
}
```

You can generate a `human.gen.go` file with the following command:
```bash
genz -type Human -template main.tmpl -output human.gen.go
```

or using the `go generate` command:
```go
go install . && go generate ./...
//go:generate genz -type Human -template main.tmpl -output human.gen.go
```
to generate the samples.

The generated file will look like this:
```go
package main

import (
"fmt"
)

func (v Human) Validate() error {
if v.Firstname == "" {
return fmt.Errorf("attribute 'Firstname' must be set")
}
if v.Lastname == "" {
return fmt.Errorf("attribute 'Lastname' must be set")
}
return nil
}
```


### CLI
```bash
Usage of genz:
genz [flags] -type T -template foo.tmpl [directory]
genz [flags] -type T -template foo.tmpl files... # Must be a single package
Flags:
-output string
output file name; default srcdir/<type>.gen.go
-tags string
comma-separated list of build tags to apply
-template string
go-template file name
-type string
comma-separated list of type names; must be set
```

## Contributing

If you would like to contribute to this project, please read the [CONTRIBUTING.md](CONTRIBUTING.md) file.

## License

This project is licensed under the Apache 2.0 - see the [LICENSE](LICENSE) file for details.

## Code of Conduct

This project is governed by the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code.

## Roadmap

- TBD
Binary file added docs/assets/logo_500_500.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 6 additions & 9 deletions examples/1_validator/main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import (

func (v {{ .Type }}) Validate() error {
{{ range .Attributes }} {{$attribute := .}}
{{ if eq .Type "string" }}
{{ range .Comments }}
{{ if eq . "+optional" }}
{{ if eq .Type "string" }}{{ range .Comments }}
{{ if eq . "+required" }}
if v.{{ $attribute.Name }} == "" {
return fmt.Errorf("attribute '{{ $attribute.Name }}' must be set")
}
{{ end }}
{{ if eq . "+startsWithCapital" }}
if v.{{ $attribute.Name }} == "" || !unicode.IsUpper(rune(v.{{ $attribute.Name }}[0])) {
{{ end }}
{{ if eq . "+startsWithCapital" }}
if v.{{ $attribute.Name }} != "" && !unicode.IsUpper(rune(v.{{ $attribute.Name }}[0])) {
return fmt.Errorf("attribute '{{ $attribute.Name }}' should start with a capital letter")
}
{{ end }}
{{ end }}
{{ end }}
{{ end }}{{ end }}{{ end }}
{{ if eq .Type "int" "uint" "uint8" "uint32" "float32" "float64" }}
// WORK IN PROGRESS
{{ range .Comments }}
Expand Down
2 changes: 1 addition & 1 deletion examples/1_validator/test/human.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Human struct {
//+startsWithCapital
Firstname string

//+optional
//+required
//+startsWithCapital
Lastname string

Expand Down

0 comments on commit 4ba2cf6

Please sign in to comment.