Skip to content

Commit

Permalink
feat: Use gg to generate code for pairs (#728)
Browse files Browse the repository at this point in the history
* feat: Use go-codegen to generate code for pairs

Signed-off-by: Xuanwo <[email protected]>

* Another style

Signed-off-by: Xuanwo <[email protected]>

* API demo

Signed-off-by: Xuanwo <[email protected]>

* Use Xuanwo/gg to play good game

Signed-off-by: Xuanwo <[email protected]>

* Merge orgin master

Signed-off-by: Xuanwo <[email protected]>

* Bump version about gg

Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored Sep 3, 2021
1 parent ff8dbd1 commit 3cdcd3f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 164 deletions.
24 changes: 1 addition & 23 deletions cmd/definitions/bindata/bindata.go

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

72 changes: 70 additions & 2 deletions cmd/definitions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 +22,6 @@ import (

var (
infoT = newTmpl("cmd/definitions/tmpl/info")
pairT = newTmpl("cmd/definitions/tmpl/pair")
serviceT = newTmpl("cmd/definitions/tmpl/service")
operationT = newTmpl("cmd/definitions/tmpl/operation")
functionT = newTmpl("cmd/definitions/tmpl/function")
Expand All @@ -33,7 +33,7 @@ func generateGlobal(data *Data) {
generateT(infoT, "types/info.generated.go", data)

// Pair generate
generateT(pairT, "pairs/generated.go", data)
generatePair(data, "pairs/generated.go")

// Operation generate
generateT(operationT, "types/operation.generated.go", data)
Expand All @@ -42,6 +42,74 @@ 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
36 changes: 0 additions & 36 deletions cmd/definitions/tmpl/pair.tmpl

This file was deleted.

4 changes: 3 additions & 1 deletion cmd/definitions/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func (f *Feature) Format(s specs.Feature) {
type Pair struct {
Name string

ptype string
ptype string
originalDescription string

Defaultable bool

Expand All @@ -166,6 +167,7 @@ func (p *Pair) Format(s specs.Pair, global bool) {
p.Global = global
p.Defaultable = s.Defaultable

p.originalDescription = s.Description
p.Description = formatDescription(templateutils.ToPascal(p.Name), s.Description)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/beyondstorage/go-storage/v4
go 1.15

require (
github.com/Xuanwo/gg v0.0.1
github.com/Xuanwo/templateutils v0.1.0
github.com/dave/dst v0.26.2
github.com/golang/mock v1.6.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/Xuanwo/gg v0.0.1 h1:SefvV3pdOV8sovx6CSzKwNsqvUwQZbKQCtN2UOPLfyk=
github.com/Xuanwo/gg v0.0.1/go.mod h1:TjGrC5F6B9H//RFhXGxv/znfrz0uRq46gZdZ0f0bvVQ=
github.com/Xuanwo/go-bufferpool v0.2.0 h1:DXzqJD9lJufXbT/03GrcEvYOs4gXYUj9/g5yi6Q9rUw=
github.com/Xuanwo/go-bufferpool v0.2.0/go.mod h1:Mle++9GGouhOwGj52i9PJLNAPmW2nb8PWBP7JJzNCzk=
github.com/Xuanwo/templateutils v0.1.0 h1:WpkWOqQtIQ2vAIpJLa727DdN8WtxhUkkbDGa6UhntJY=
github.com/Xuanwo/templateutils v0.1.0/go.mod h1:OdE0DJ+CJxDBq6psX5DPV+gOZi8bhuHuVUpPCG++Wb8=
github.com/dave/dst v0.26.2 h1:lnxLAKI3tx7MgLNVDirFCsDTlTG9nKTk7GcptKcWSwY=
Expand Down
Loading

0 comments on commit 3cdcd3f

Please sign in to comment.