-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xuanwo <[email protected]>
- Loading branch information
Showing
8 changed files
with
178 additions
and
206 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.