Skip to content

Commit e9dac78

Browse files
Improving: code generator's template
1 parent 636951e commit e9dac78

File tree

6 files changed

+29
-28
lines changed

6 files changed

+29
-28
lines changed

cmd/generator/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ generator -n my_contract -c KT1...
1717
Args:
1818

1919
* `c` - contract address. For example, `KT1WxV6DDSFogKDg9DeAZZZr1HnVvKadpd3S`. Required if `f` is not set.
20-
* `n` - your contract name. Optional. Default: `contract`.
20+
* `n` - your contract name. Optional. Default: `my_contract`.
2121
* `u` - base TzKT API URL. Optional. Default: `https://api.tzkt.io/`.
2222
* `o` - output directory. Optional. Default: current directory.
2323
* `f` - path to JSON schema file. Required if `c` is not set.

cmd/generator/generator.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package main
22

33
import (
4-
"bufio"
4+
"embed"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"strings"
@@ -14,17 +13,18 @@ import (
1413
"github.com/iancoleman/strcase"
1514
)
1615

17-
type typesTemplateContext struct {
18-
PackageName string
19-
}
20-
21-
type contractTemplateContext struct {
16+
type templateContext struct {
2217
PackageName string
2318
TypeName string
2419
EntrypointTypes map[string]types.EntrypointData
2520
Contract string
2621
}
2722

23+
var (
24+
//go:embed template/*.tmpl
25+
templates embed.FS
26+
)
27+
2828
// Generate -
2929
func Generate(schema api.ContractJSONSchema, name, contract, dest string) error {
3030
if dest == "" {
@@ -59,7 +59,7 @@ func Generate(schema api.ContractJSONSchema, name, contract, dest string) error
5959

6060
func generateContractObject(name, contract, dest string, result types.ContractTypeResult) error {
6161
className := strcase.ToCamel(name)
62-
return generateFromTemplate(result.PackageName, "contract.tmpl", dest, contractTemplateContext{
62+
return generateFromTemplate(result.PackageName, "contract", dest, templateContext{
6363
PackageName: result.PackageName,
6464
TypeName: className,
6565
Contract: contract,
@@ -85,29 +85,22 @@ func generateContractTypes(schema api.ContractJSONSchema, packageName, dest stri
8585
}
8686

8787
func generateDefaultTypes(packageName, dest string) error {
88-
return generateFromTemplate(packageName, "types.tmpl", dest, typesTemplateContext{packageName})
88+
return generateFromTemplate(packageName, "types", dest, templateContext{PackageName: packageName})
8989
}
9090

9191
func generateFromTemplate(packageName, templateFileName, dest string, ctx interface{}) error {
92-
fileName := fmt.Sprintf("%s.go", strings.TrimSuffix(templateFileName, ".tmpl"))
93-
buf, err := ioutil.ReadFile(filepath.Join("./templates", templateFileName))
94-
if err != nil {
95-
return err
96-
}
97-
tmpl, err := template.New(packageName).Parse(string(buf))
92+
tmpl, err := template.ParseFS(templates, "template/*")
9893
if err != nil {
9994
return err
10095
}
101-
targetFile := filepath.Join(dest, fileName)
96+
targetFile := filepath.Join(dest, fmt.Sprintf("%s.go", templateFileName))
10297
templateFile, err := os.OpenFile(targetFile, os.O_WRONLY|os.O_CREATE, 0755)
10398
if err != nil {
10499
return err
105100
}
106-
w := bufio.NewWriter(templateFile)
107-
if err := tmpl.Execute(w, ctx); err != nil {
101+
if err := tmpl.ExecuteTemplate(templateFile, fmt.Sprintf("%s.tmpl", templateFileName), ctx); err != nil {
108102
return err
109103
}
110-
w.Flush()
111104

112105
return templateFile.Close()
113106
}

cmd/generator/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type args struct {
1515
Contract string `validate:"required_with=URL,len=36"`
1616
URL string `validate:"url,required_with=Contract"`
1717
Name string `validate:"omitempty"`
18-
Output string `validate:"dir,omitempty"`
18+
Output string `validate:"omitempty,dir"`
1919
File string `validate:"required_without=URL"`
2020
}
2121

@@ -24,7 +24,7 @@ func main() {
2424

2525
flag.StringVar(&args.Contract, "c", "", "Contract address `KT1...`")
2626
flag.StringVar(&args.URL, "u", "https://api.tzkt.io/", "TzKT base URL")
27-
flag.StringVar(&args.Name, "n", "contract", "Contract name")
27+
flag.StringVar(&args.Name, "n", "my_contract", "Contract name")
2828
flag.StringVar(&args.Output, "o", "", "Output directory")
2929
flag.StringVar(&args.File, "f", "", "Path to JSON schema file")
3030

cmd/generator/templates/contract.tmpl renamed to cmd/generator/template/contract.tmpl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ import (
1313
"github.com/dipdup-net/go-lib/tzkt/events"
1414
)
1515

16-
16+
{{ range $key, $value := .EntrypointTypes }}
17+
// {{ $value.Type }}Tx - `{{$key}}` transaction entity
18+
type {{ $value.Type }}Tx struct {
19+
*events.Transaction
20+
{{ $value.Type }} {{ $value.Type }}
21+
}
22+
{{ end }}
1723
// {{.TypeName}} - struct which implementing contract interaction
1824
type {{.TypeName}} struct {
1925
tzktAPI *api.API
2026
tzktEvents *events.TzKT
2127

2228
{{- range $key, $value := .EntrypointTypes }}
23-
{{ $value.Var }} chan {{ $value.Type }}
29+
{{ $value.Var }} chan {{ $value.Type }}Tx
2430
{{- end }}
2531
wg sync.WaitGroup
2632
}
@@ -31,7 +37,7 @@ func New(baseURL string) *{{.TypeName}} {
3137
tzktAPI: api.New(baseURL),
3238
tzktEvents: events.NewTzKT(fmt.Sprintf("%s/v1/events", baseURL)),
3339
{{- range $key, $value := .EntrypointTypes }}
34-
{{ $value.Var }}: make(chan {{ $value.Type }}, 1024),
40+
{{ $value.Var }}: make(chan {{ $value.Type }}Tx, 1024),
3541
{{- end }}
3642
}
3743
}
@@ -64,7 +70,7 @@ func (contract *{{.TypeName}}) Close() error {
6470

6571
{{ range $key, $value := .EntrypointTypes }}
6672
// {{$value.Type}}Events - listen `{{$key}}` events channel
67-
func (contract *{{$.TypeName}}) {{$value.Type}}Events() <-chan {{$value.Type}} {
73+
func (contract *{{$.TypeName}}) {{$value.Type}}Events() <-chan {{ $value.Type }}Tx {
6874
return contract.{{$value.Var}}
6975
}
7076
{{ end }}
@@ -100,7 +106,9 @@ func (contract *{{.TypeName}}) listen(ctx context.Context) {
100106
log.Println(err)
101107
continue
102108
}
103-
contract.{{ $value.Var }} <- data
109+
contract.{{ $value.Var }} <- {{ $value.Type }}Tx{
110+
tx, data,
111+
}
104112
{{- end }}
105113
}
106114
}
File renamed without changes.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/dipdup-net/go-lib
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
github.com/dave/jennifer v1.4.1

0 commit comments

Comments
 (0)