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

feat: Generate info via Xuanwo/gg #737

Merged
merged 3 commits into from
Sep 3, 2021
Merged
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
23 changes: 0 additions & 23 deletions cmd/definitions/bindata/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions cmd/definitions/gen_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//go:build tools
// +build tools

package main

import (
"fmt"

"github.com/Xuanwo/gg"
"github.com/Xuanwo/templateutils"
log "github.com/sirupsen/logrus"
)

func generateInfo(data *Data, path string) {
f := gg.Group()
f.LineComment("Code generated by go generate cmd/definitions; DO NOT EDIT.")
f.Package("types")
f.Imports().Path("fmt")

for serviceName, infos := range data.InfosMap {
serviceNameC := templateutils.ToCamel(serviceName)
serviceNameP := templateutils.ToPascal(serviceName)
serviceNameK := templateutils.ToKebab(serviceName)

// Generate field bits
f.LineComment("Field index in %s bit", serviceNameC)
consts := f.Const()
for k, v := range infos {
consts.Field(
fmt.Sprintf("%sIndex%s",
serviceNameC, templateutils.ToPascal(v.Name)),
fmt.Sprintf("1<<%d", k),
)
}

// Generate struct
st := f.Struct(serviceNameP)
for _, v := range infos {
st.Field(v.TypeName(), v.Type())
}
st.Line()
st.LineComment("bit used as a bitmap for object value, 0 means not set, 1 means set")
st.Field("bit", "uint64")
st.Field("m", "map[string]interface{}")

// Generate Get/Set functions.
for _, v := range infos {
// If the value is export, we don't need to generate MustGetXxxx
if v.Export {
f.Function("Get"+v.DisplayName()).
Receiver("m", "*"+serviceNameP).
Result("", v.Type()).
Body(gg.Return().Id("m." + v.TypeName()))
f.Function("Set"+v.DisplayName()).
Receiver("m", "*"+serviceNameP).
Parameter("v", v.Type()).
Result("", "*"+serviceNameP).
Body(
gg.String("m.%s = v", v.TypeName()),
gg.Return().Id("m"),
)
continue
}
f.Function("Get"+v.DisplayName()).
Receiver("m", "*"+serviceNameP).
Result("", v.Type()).
Result("", "bool").
Body(
gg.If(gg.String("m.bit & %sIndex%s != 0",
serviceNameC, templateutils.ToPascal(v.Name))).
Body(
gg.Return().Id("m."+v.TypeName()).Lit(true)),
gg.Return().Id(templateutils.ZeroValue(v.Type())).Lit(false),
)
f.Function("MustGet"+v.DisplayName()).
Receiver("m", "*"+serviceNameP).
Result("", v.Type()).
Body(
gg.If(gg.String("m.bit & %sIndex%s == 0",
serviceNameC, templateutils.ToPascal(v.Name))).
Body(gg.String(
`panic(fmt.Sprintf("%s %s is not set"))`,
serviceNameK, v.Name)),
gg.Return().Id("m."+v.TypeName()),
)
f.Function("Set"+v.DisplayName()).
Receiver("m", "*"+serviceNameP).
Parameter("v", v.Type()).
Result("", "*"+serviceNameP).
Body(
gg.String("m.%s = v", v.TypeName()),
gg.String("m.bit |= %sIndex%s",
serviceNameC, templateutils.ToPascal(v.Name)),
gg.Return().Id("m"),
)
}
}

err := f.WriteFile(path)
if err != nil {
log.Fatalf("generate to %s: %v", path, err)
}
}
80 changes: 80 additions & 0 deletions cmd/definitions/gen_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//go:build tools
// +build tools

package main

import (
"sort"

"github.com/Xuanwo/gg"
"github.com/Xuanwo/templateutils"
log "github.com/sirupsen/logrus"
)

func generatePair(data *Data, path string) {
f := gg.Group()
f.LineComment("Code generated by go generate cmd/definitions; DO NOT EDIT.")
f.Package("pairs")
f.Imports().
Path("context").
Path("time").
Line().
Path("github.com/beyondstorage/go-storage/v4/pkg/httpclient").
Dot("github.com/beyondstorage/go-storage/v4/types")

ps := make([]*Pair, 0, len(data.Pairs))
for _, v := range data.Pairs {
v := v
ps = append(ps, v)
}
sort.SliceStable(ps, func(i, j int) bool {
return ps[i].Name < ps[j].Name
})

for _, v := range ps {
pname := templateutils.ToPascal(v.Name)

xfn := f.Function("With" + pname)

xfn.NamedLineComment(`will apply %s value to Options.

%s %s`, v.Name, pname, v.originalDescription)
xfn.Parameter("v", v.Type())
xfn.Result("p", "Pair")
xfn.Body(
gg.Embed(func() gg.Node {
r := gg.Return()
r.Value("Pair").
Field("Key", gg.Lit(v.Name).String()).
Field("Value", "v")
return r
}),
)
if v.Defaultable {
name := "default_" + v.Name
pname := templateutils.ToPascal(name)

xfn := f.Function("With" + pname)

xfn.NamedLineComment(`will apply %s value to Options.

%s %s`, name, pname, v.originalDescription)
xfn.Parameter("v", v.Type())
xfn.Result("p", "Pair")
xfn.Body(
gg.Embed(func() gg.Node {
r := gg.Return()
r.Value("Pair").
Field("Key", gg.Lit(name).String()).
Field("Value", "v")
return r
}),
)
}
}

err := f.WriteFile(path)
if err != nil {
log.Fatalf("generate to %s: %v", path, err)
}
}
72 changes: 1 addition & 71 deletions cmd/definitions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sort"
"text/template"

"github.com/Xuanwo/gg"
"github.com/Xuanwo/templateutils"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
Expand All @@ -21,7 +20,6 @@ import (
)

var (
infoT = newTmpl("cmd/definitions/tmpl/info")
serviceT = newTmpl("cmd/definitions/tmpl/service")
operationT = newTmpl("cmd/definitions/tmpl/operation")
functionT = newTmpl("cmd/definitions/tmpl/function")
Expand All @@ -30,7 +28,7 @@ var (

func generateGlobal(data *Data) {
// Metas generate
generateT(infoT, "types/info.generated.go", data)
generateInfo(data, "types/info.generated.go")

// Pair generate
generatePair(data, "pairs/generated.go")
Expand All @@ -42,74 +40,6 @@ func generateGlobal(data *Data) {
generateT(objectT, "types/object.generated.go", data)
}

func generatePair(data *Data, path string) {
f := gg.Group()
f.Comment("Code generated by go generate cmd/definitions; DO NOT EDIT.")
f.Package("pairs")
f.Imports().
Path("context").
Path("time").
Line().
Path("github.com/beyondstorage/go-storage/v4/pkg/httpclient").
Dot("github.com/beyondstorage/go-storage/v4/types")

ps := make([]*Pair, 0, len(data.Pairs))
for _, v := range data.Pairs {
v := v
ps = append(ps, v)
}
sort.SliceStable(ps, func(i, j int) bool {
return ps[i].Name < ps[j].Name
})

for _, v := range ps {
pname := templateutils.ToPascal(v.Name)

xfn := f.Function("With" + pname)

xfn.CommentF(`With%s will apply %s value to Options.

%s %s`, pname, v.Name, pname, v.originalDescription)
xfn.Parameter("v", v.Type())
xfn.Result("p", "Pair")
xfn.Body(
gg.Embed(func() gg.Node {
r := gg.Return()
r.Value("Pair").
Field("Key", gg.Lit(v.Name).String()).
Field("Value", "v")
return r
}),
)
if v.Defaultable {
name := "default_" + v.Name
pname := templateutils.ToPascal(name)

xfn := f.Function("With" + pname)

xfn.CommentF(`With%s will apply %s value to Options.

%s %s`, pname, name, pname, v.originalDescription)
xfn.Parameter("v", v.Type())
xfn.Result("p", "Pair")
xfn.Body(
gg.Embed(func() gg.Node {
r := gg.Return()
r.Value("Pair").
Field("Key", gg.Lit(name).String()).
Field("Value", "v")
return r
}),
)
}
}

err := f.WriteFile(path)
if err != nil {
log.Fatalf("generate to %s: %v", path, err)
}
}

func generateService(data *Data) {
generateT(serviceT, "generated.go", data.Service)
for _, v := range data.Service.Namespaces {
Expand Down
61 changes: 0 additions & 61 deletions cmd/definitions/tmpl/info.tmpl

This file was deleted.

Loading