-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_iterator.go
126 lines (109 loc) · 3.01 KB
/
gen_iterator.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package definitions
import (
"fmt"
"github.com/Xuanwo/gg"
log "github.com/sirupsen/logrus"
)
func GenerateIterator(path string) {
data := []string{
"Block",
"Object",
"Part",
"Storager",
}
g := gg.New()
f := g.NewGroup()
f.AddLineComment("Code generated by go generate cmd/definitions; DO NOT EDIT.")
f.AddPackage("types")
f.NewImport().
AddPath("context").
AddPath("errors").
AddPath("fmt")
f.NewInterface("Continuable").
NewFunction("ContinuationToken").
AddResult("", "string")
for _, name := range data {
typ := name
// Add a `*` if the type is not an interface.
if name != "Storager" {
typ = "*" + typ
}
nextFuncName := fmt.Sprintf("Next%sFunc", name)
pageStructName := fmt.Sprintf("%sPage", name)
iteratorStructName := fmt.Sprintf("%sIterator", name)
f.AddLineComment(`%s is the func used in iterator.
Notes
- ErrDone should be return while there are no items any more.
- Input %s slice should be set every time.
`, nextFuncName, name)
f.AddType(
nextFuncName,
gg.Function("").
AddParameter("ctx", "context.Context").
AddParameter("page", "*"+pageStructName).
AddResult("", "error"))
f.NewStruct(pageStructName).
AddField("Status", "Continuable").
AddField("Data", "[]"+typ)
f.NewStruct(iteratorStructName).
AddField("ctx", "context.Context").
AddField("next", nextFuncName).
AddLine().
AddField("index", "int").
AddField("done", "bool").
AddLine().
AddField("o", pageStructName)
f.NewFunction("New"+iteratorStructName).
AddParameter("ctx", "context.Context").
AddParameter("next", nextFuncName).
AddParameter("status", "Continuable").
AddResult("", "*"+iteratorStructName).
AddBody(gg.Return(
gg.Value("&"+iteratorStructName).
AddField("ctx", "ctx").
AddField("next", "next").
AddField("o", gg.Value(pageStructName).
AddField("Status", "status"))))
f.NewFunction("ContinuationToken").
WithReceiver("it", "*"+iteratorStructName).
AddResult("", "string").
AddBody(gg.Return(
gg.Call("ContinuationToken").WithOwner("it.o.Status")))
f.NewFunction("Next").
WithReceiver("it", "*"+iteratorStructName).
AddResult("object", typ).
AddResult("err", "error").
AddBody(
gg.LineComment("Consume Data via index."),
gg.S(`// Consume Data via index.
if it.index < len(it.o.Data) {
it.index++
return it.o.Data[it.index-1], nil
}
// Return IterateDone if iterator is already done.
if it.done {
return nil, IterateDone
}
// Reset buf before call next.
it.o.Data = it.o.Data[:0]
err = it.next(it.ctx ,&it.o)
if err != nil && !errors.Is(err, IterateDone) {
return nil, fmt.Errorf("iterator next failed: %w", err)
}
// Make iterator to done so that we will not fetch from upstream anymore.
if err != nil {
it.done = true
}
// Return IterateDone directly if we don't have any data.
if len(it.o.Data) == 0 {
return nil, IterateDone
}
// Return the first object.
it.index = 1
return it.o.Data[0], nil`))
}
err := g.WriteFile(path)
if err != nil {
log.Fatalf("generate to %s: %v", path, err)
}
}