Skip to content

Commit

Permalink
Merge pull request #1 from whyrusleeping/feat/map-encoding-flag
Browse files Browse the repository at this point in the history
add flag to select map encoding
  • Loading branch information
hannahhoward authored Aug 17, 2020
2 parents 5892b52 + f24dbbf commit ea96cec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
35 changes: 21 additions & 14 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ import (
var ErrWrongPackageCount = errors.New("Must be part of only one package")

type Generator struct {
Path string
Filename string
Package string
GenStructs []string
Path string
Filename string
Package string
GenStructs []string
MapEncoding bool
}

type templateData struct {
Path string
Filebase string
Package string
GenStructs []string
PkgPath string
Path string
Filebase string
Package string
GenStructs []string
PkgPath string
MapEncoding bool
}

func (g Generator) GenerateCborTypes() error {
Expand All @@ -41,11 +43,12 @@ func (g Generator) GenerateCborTypes() error {
return ErrWrongPackageCount
}
tdata := templateData{
Path: g.Path,
Package: g.Package,
Filebase: strings.TrimSuffix(g.Filename, ".go"),
PkgPath: pkgs[0].PkgPath,
GenStructs: g.GenStructs,
Path: g.Path,
Package: g.Package,
Filebase: strings.TrimSuffix(g.Filename, ".go"),
PkgPath: pkgs[0].PkgPath,
GenStructs: g.GenStructs,
MapEncoding: g.MapEncoding,
}

rt, err := template.New("run_cbor_gen").Parse(runTemplate)
Expand Down Expand Up @@ -98,7 +101,11 @@ import (
func RunCborGen() error {
genName := "{{.Path}}/{{.Filebase}}_cbor_gen.go"
{{ if .MapEncoding }}
if err := cborgen.WriteMapEncodersToFile(
{{ else }}
if err := cborgen.WriteTupleEncodersToFile(
{{ end }}
genName,
"{{.Package}}",
{{range .GenStructs}}
Expand Down
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ func main() {
app := &cli.App{
Name: "cbor-gen-for",
Usage: "Generate CBOR encoders for types",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "map-encoding",
},
},
Action: func(c *cli.Context) error {
filename := os.Getenv("GOFILE")
path, _ := os.Getwd()
err := Generator{
Filename: filename,
Path: path,
Package: os.Getenv("GOPACKAGE"),
GenStructs: c.Args().Slice(),
Filename: filename,
Path: path,
Package: os.Getenv("GOPACKAGE"),
GenStructs: c.Args().Slice(),
MapEncoding: c.Bool("map-encoding"),
}.GenerateCborTypes()

return err
Expand Down

0 comments on commit ea96cec

Please sign in to comment.