-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_pair.go
50 lines (41 loc) · 1.13 KB
/
gen_pair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package definitions
import (
"github.com/Xuanwo/gg"
"github.com/Xuanwo/templateutils"
log "github.com/sirupsen/logrus"
)
func GeneratePair(path string) {
g := gg.New()
f := g.NewGroup()
f.AddLineComment("Code generated by go generate cmd/definitions; DO NOT EDIT.")
f.AddPackage("pairs")
f.NewImport().
AddPath("github.com/fastone-open/go-storage/types")
ps := SortPairs(PairArray)
for _, v := range ps {
pname := templateutils.ToPascal(v.Name)
f.AddLineComment(`With%s will apply %s value to Options.
%s %s`, pname, v.Name, pname, v.Description)
xfn := f.NewFunction("With" + pname)
xfn.AddResult("p", "types.Pair")
// If value type is bool, we don't need to accept parameters.
if v.Type.Name == "bool" {
xfn.AddBody(
gg.Return(
gg.Value("types.Pair").
AddField("Key", gg.Lit(v.Name)).
AddField("Value", "true")))
continue
}
xfn.AddParameter("v", v.Type.FullName("pairs"))
xfn.AddBody(
gg.Return(
gg.Value("types.Pair").
AddField("Key", gg.Lit(v.Name)).
AddField("Value", "v")))
}
err := g.WriteFile(path)
if err != nil {
log.Fatalf("generate to %s: %v", path, err)
}
}