Skip to content

Commit

Permalink
feat: Generate Object via Xuanwo/gg
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Sep 3, 2021
1 parent 8412789 commit ca2c1ff
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 206 deletions.
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.

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

package main

import (
"fmt"

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

func generateObject(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").
Path("time").
Path("sync")

f.LineComment("Field index in object bit")
cons := f.Const()
for k, v := range data.ObjectMeta {
pname := templateutils.ToPascal(v.Name)
cons.Field(
fmt.Sprintf("objectIndex%s uint64", pname),
fmt.Sprintf("1<<%d", k))
}

f.LineComment(`
Object is the smallest unit in go-storage.
NOTES:
- Object's fields SHOULD not be changed outside services.
- Object CANNOT be copied
- Object is concurrent safe.
- Only ID, Path, Mode are required during list operations, other fields
could be fetched via lazy stat logic: https://beyondstorage.io/docs/go-storage/internal/object-lazy-stat
`)
ob := f.Struct("Object")
for _, v := range data.ObjectMeta {
if v.originalDescription != "" {
ob.LineComment(v.originalDescription)
}
ob.Field(v.TypeName(), v.Type())
}

ob.LineComment("client is the client in which Object is alive.")
ob.Field("client", "Storager")

ob.LineComment("bit used as a bitmap for object value, 0 means not set, 1 means set")
ob.Field("bit", "uint64")
ob.Field("done", "uint32")
ob.Field("m", "sync.Mutex")

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

if v.Export {
f.Function("Get"+v.DisplayName()).
Receiver("o", "*Object").
Result("", v.Type()).
Body(gg.Return().Id("o." + v.TypeName()))
f.Function("Set"+v.DisplayName()).
Receiver("o", "*Object").
Parameter("v", v.Type()).
Result("", "*Object").
Body(
gg.String("o.%s = v", v.TypeName()),
gg.Return().Id("o"),
)
continue
}
f.Function("Get"+v.DisplayName()).
Receiver("o", "*Object").
NamedLineComment(`will get %s from Object.
%s
`, v.DisplayName(), v.Description).
Result("", v.Type()).
Result("", "bool").
Body(
gg.String("o.stat()"),
gg.Line(),
gg.If(gg.String("o.bit & objectIndex%s != 0", pname)).
Body(
gg.Return().Id("o."+v.TypeName()).Lit(true),
),
gg.Return().Id(templateutils.ZeroValue(v.Type())).Lit(false),
)
f.Function("MustGet"+v.DisplayName()).
Receiver("o", "*Object").
NamedLineComment(`will get %s from Object.
%s
`, v.DisplayName(), v.originalDescription).
Result("", v.Type()).
Body(
gg.String("o.stat()"),
gg.Line(),
gg.If(gg.String("o.bit & objectIndex%s == 0", pname)).
Body(
gg.String(`panic(fmt.Sprintf("object %s is not set"))`, v.Name),
),
gg.Return().Id("o."+v.TypeName()),
)
f.Function("Set"+v.DisplayName()).
Receiver("o", "*Object").
NamedLineComment(`will set %s into Object.
%s
`, v.DisplayName(), v.originalDescription).
Parameter("v", v.Type()).
Result("", "*Object").
Body(
gg.String("o.%s = v", v.TypeName()),
gg.String("o.bit |= objectIndex%s", pname),
gg.Return().Id("o"),
)
}
fn := f.Function("clone").
Receiver("o", "*Object").
Parameter("xo", "*Object")
for _, v := range data.ObjectMeta {
fn.Body(gg.String("o.%s = xo.%s", v.TypeName(), v.TypeName()))
}
fn.Body(gg.String("o.bit = xo.bit"))

err := f.WriteFile(path)
if err != nil {
log.Fatalf("generate to %s: %v", path, err)
}
}
4 changes: 2 additions & 2 deletions cmd/definitions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
serviceT = newTmpl("cmd/definitions/tmpl/service")
operationT = newTmpl("cmd/definitions/tmpl/operation")
functionT = newTmpl("cmd/definitions/tmpl/function")
objectT = newTmpl("cmd/definitions/tmpl/object")
)

func generateGlobal(data *Data) {
Expand All @@ -37,7 +36,8 @@ func generateGlobal(data *Data) {
generateT(operationT, "types/operation.generated.go", data)

// Object generate
generateT(objectT, "types/object.generated.go", data)
generateObject(data, "types/object.generated.go")

}

func generateService(data *Data) {
Expand Down
97 changes: 0 additions & 97 deletions cmd/definitions/tmpl/object.tmpl

This file was deleted.

3 changes: 3 additions & 0 deletions cmd/definitions/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ type Info struct {
Export bool
Description string

originalDescription string

itype string

Global bool
Expand All @@ -193,6 +195,7 @@ func (i *Info) Format(s specs.Info, global bool) {
i.Export = s.Export
i.Description = formatDescription(templateutils.ToPascal(s.Name), s.Description)

i.originalDescription = s.Description
i.Global = global
}

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

require (
github.com/Xuanwo/gg v0.0.2-0.20210903045407-659a0d118a9c
github.com/Xuanwo/gg v0.0.2
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: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Xuanwo/gg v0.0.2-0.20210903045407-659a0d118a9c h1:F/NffCLcEmh21AYBQlIzKP8WzhtAARMjZ1nfck03Ya8=
github.com/Xuanwo/gg v0.0.2-0.20210903045407-659a0d118a9c/go.mod h1:TjGrC5F6B9H//RFhXGxv/znfrz0uRq46gZdZ0f0bvVQ=
github.com/Xuanwo/gg v0.0.2 h1:nYYjzNGOkPF15Ny4EQC8QQS9HEPXA0I4dwDykRIHusM=
github.com/Xuanwo/gg v0.0.2/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=
Expand Down
Loading

0 comments on commit ca2c1ff

Please sign in to comment.