Skip to content

Commit

Permalink
Update to go1.19+move to go:embed for typescript templates (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxifom authored Sep 17, 2022
1 parent 8e1d423 commit 4bd3c9a
Show file tree
Hide file tree
Showing 10 changed files with 524 additions and 117 deletions.
36 changes: 19 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
module github.com/maxifom/eos-abigen

go 1.18
go 1.19

require (
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/dave/jennifer v1.5.0
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.10.1
github.com/dave/jennifer v1.5.1
github.com/spf13/afero v1.9.2
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.13.0
github.com/stoewer/go-strcase v1.2.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
507 changes: 453 additions & 54 deletions go.sum

Large diffs are not rendered by default.

58 changes: 50 additions & 8 deletions pkg/commands/generate-ts/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generate

import (
"embed"
"encoding/json"
"fmt"
"os"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/maxifom/eos-abigen/pkg/abitypes"
"github.com/spf13/afero"

"github.com/maxifom/eos-abigen/templates/ts"
"github.com/stoewer/go-strcase"
)

Expand All @@ -36,6 +36,9 @@ func generateIBrackets(count int) string {
return s.String()
}

//go:embed templates/ts/*.gotmpl
var templatesFs embed.FS

func Run(opts Opts) error {
fs := opts.FS
if fs == nil {
Expand All @@ -55,6 +58,11 @@ func Run(opts Opts) error {
Tabs int
}

clientTemplate, err := templatesFs.ReadFile("templates/ts/client.gotmpl")
if err != nil {
return err
}

t, err := template.New("client").Funcs(sprig.TxtFuncMap()).Funcs(map[string]any{
"genStructForFieldMapper": func(f Field, isLast bool, useFullTypes bool, tabs int) StructForFieldMapper {
return StructForFieldMapper{
Expand Down Expand Up @@ -103,31 +111,65 @@ func Run(opts Opts) error {

return s
},
}).Parse(ts.ClientTemplate)
}).Parse(string(clientTemplate))
if err != nil {
return err
}
t, err = t.New("index").Parse(ts.IndexTemplate)

indexTemplate, err := templatesFs.ReadFile("templates/ts/index.gotmpl")
if err != nil {
return err
}
t, err = t.New("struct").Parse(ts.StructTemplate)

t, err = t.New("index").Parse(string(indexTemplate))
if err != nil {
return err
}
t, err = t.New("table_rows").Parse(ts.TableRowsTemplate)

structTemplate, err := templatesFs.ReadFile("templates/ts/struct.gotmpl")
if err != nil {
return err
}
t, err = t.New("types").Parse(ts.TypesTemplate)

t, err = t.New("struct").Parse(string(structTemplate))
if err != nil {
return err
}

tableRowsTemplate, err := templatesFs.ReadFile("templates/ts/table_rows.gotmpl")
if err != nil {
return err
}

t, err = t.New("table_rows").Parse(string(tableRowsTemplate))
if err != nil {
return err
}
t, err = t.New("action_builder").Parse(ts.ActionBuilderTemplate)

typesTemplate, err := templatesFs.ReadFile("templates/ts/types.gotmpl")
if err != nil {
return err
}

t, err = t.New("types").Parse(string(typesTemplate))
if err != nil {
return err
}

actionBuilderTemplate, err := templatesFs.ReadFile("templates/ts/action_builder.gotmpl")
if err != nil {
return err
}
t, err = t.New("action_builder").Parse(string(actionBuilderTemplate))
if err != nil {
return err
}

fieldMapperTemplate, err := templatesFs.ReadFile("templates/ts/field_mapper.gotmpl")
if err != nil {
return err
}
t, err = t.New("map_field").Parse(ts.MapFieldTemplate)
t, err = t.New("map_field").Parse(string(fieldMapperTemplate))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package ts

import "strings"

var ActionBuilderTemplate = strings.TrimSpace(`
// Generated by eos-abigen version {{.Version}}
import * as types from "./types"
import {Authorization} from "./types"
Expand Down Expand Up @@ -60,4 +55,3 @@ export class ActionBuilder {
return authorizations
}
}
`)
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package ts

import "strings"

var ClientTemplate = strings.TrimSpace(`
// Generated by eos-abigen version {{.Version}}
import {JsonRpc} from "eosjs";
import * as types from "./types";
Expand Down Expand Up @@ -43,4 +38,3 @@ export class Client {
}
{{end -}}
}
`)
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package ts

import "strings"

var MapFieldTemplate = strings.TrimSpace(`
{{ define "fieldmapper" }}
{{- if .F.GenerateMapper -}}
{{generateTabs .Tabs }}{{- .F.Name}}: {{ template "arraywrapper" (dict "F" .F "I" 0 "UseFullTypes" .UseFullTypes) }},
Expand All @@ -22,4 +17,3 @@ a{{.I}} => a{{.I}}.map({{ template "arraywrapper" (dict "I" (add .I 1) "F" .F "U
{{- end -}}
{{- end -}}
{{- end -}}
`)
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package ts

import "strings"

var IndexTemplate = strings.TrimSpace(`
// Generated by eos-abigen version {{.Version}}

export * from "./client";
export * from "./action_builder";
export * from "./types";
`)
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package ts

var StructTemplate = `
export type {{.Name}} = {
{{$lenFields := len .Fields -}}
{{ range $i, $f := .Fields -}}{{$f.Name}}: {{$f.Type}}
Expand All @@ -20,4 +17,3 @@ export function map{{.Name}}(r: {{.Name}}Interm): {{.Name}} {
{{- end -}}
{{generateTabs 1 -}} }
}
`
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package ts

var TableRowsTemplate = `export type {{.TableName}}Rows = {
export type {{.TableName}}Rows = {
more: boolean;
next_key: string;
rows: {{.TableName}}[];
Expand All @@ -11,4 +9,3 @@ export type {{.TableName}}RowsInterm = {
next_key: string;
rows: {{.TableName}}Interm[];
};
`
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
package ts

import "strings"

var TypesTemplate = strings.TrimSpace(`// Generated by eos-abigen version {{.Version}}
// Generated by eos-abigen version {{.Version}}

export const CONTRACT_NAME = "{{.ContractName}}";

Expand Down Expand Up @@ -90,4 +86,3 @@ export class ExtendedAsset {
}
{{ if ne .StructTypes nil }}{{ .StructTypes }}{{ end }}
{{ if ne .RowTypes nil }}{{ .RowTypes }}{{ end }}
`)

0 comments on commit 4bd3c9a

Please sign in to comment.