/
types.go
218 lines (195 loc) · 4.8 KB
/
types.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (C) 2019 rameshvk. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.
package fussy
import "strings"
// Info contains all the info needed to generate code
type Info struct {
// Generator indicates the raw code which generates this
Generator string
Package string
Imports [][2]string
Components []ComponentInfo
}
// ComponentInfo holds info related to a component
type ComponentInfo struct {
Name, Type string
Ctor string
Args, Results []ArgInfo
Variadic bool
Subs []SubComponentInfo
}
// SubComponentInfo holds sub-component related info
type SubComponentInfo struct {
LocalName string
ComponentInfo
}
// ArgInfo holds func arg related info
type ArgInfo struct {
Name, Type string
IsState bool
ImplementsEquals bool
ImplementsStream bool
ImplementsClose bool
}
// NonContextArgsArray returns the list of non-context args, including state
func (c *ComponentInfo) NonContextArgsArray() []ArgInfo {
return c.Args[1:]
}
// StreamStateArgs returns all state args that implement events
func (c *ComponentInfo) StreamStateArgs() []ArgInfo {
result := []ArgInfo{}
for kk, a := range c.Args {
if kk > 0 && a.IsState && a.ImplementsStream {
result = append(result, a)
}
}
return result
}
// PublicArgsDecl returns all public args and types
func (c *ComponentInfo) PublicArgsDecl() string {
result := []string{}
for kk, a := range c.Args {
tt := "interface{}"
if kk > 0 {
tt = a.Type
}
if c.Variadic && kk == len(c.Args)-1 {
tt = "..." + tt[2:]
}
if !a.IsState {
result = append(result, a.Name+" "+tt)
}
}
return strings.Join(result, ",")
}
// PublicResultsDecl returns all public return values and their types
func (c *ComponentInfo) PublicResultsDecl() string {
result := []string{}
for _, a := range c.Results {
if !a.IsState {
result = append(result, a.Name+" "+a.Type)
}
}
if len(result) > 0 {
return "(" + strings.Join(result, ",") + ")"
}
return strings.Join(result, ",")
}
// ContextName returns the name of the context arg
func (c *ComponentInfo) ContextName() string {
return c.Args[0].Name
}
// ContextType returns the name of the context arg
func (c *ComponentInfo) ContextType() string {
return c.Args[0].Type[1:]
}
// PublicArgs returns a list of all public args
func (c *ComponentInfo) PublicArgs() string {
result := []string{}
for kk, a := range c.Args {
n := a.Name
if c.Variadic && kk == len(c.Args)-1 {
n += "..."
}
if !a.IsState {
result = append(result, n)
}
}
return strings.Join(result, ",")
}
// LastPublicResults returns all the public results but prefixed with last
func (c *ComponentInfo) LastPublicResults() string {
result := []string{}
for _, a := range c.Results {
if !a.IsState {
result = append(result, "last"+a.Name)
}
}
return strings.Join(result, ",")
}
// PublicResultsArray returns the non-state resultss
func (c *ComponentInfo) PublicResultsArray() []ArgInfo {
result := []ArgInfo{}
for _, a := range c.Results {
if !a.IsState {
result = append(result, a)
}
}
return result
}
// PublicArgsArrayEquals returns all non-state args
// implementing Equals
func (c *ComponentInfo) PublicArgsArrayEquals() []ArgInfo {
result := []ArgInfo{}
for kk, a := range c.Args {
if kk > 0 && !a.IsState && a.ImplementsEquals {
result = append(result, a)
}
}
return result
}
// LastArg returns the last arg
func (c *ComponentInfo) LastArg() ArgInfo {
return c.Args[len(c.Args)-1]
}
// PublicArgsArrayOther returns all non-state args which don't
// implement equals
func (c *ComponentInfo) PublicArgsArrayOther() []ArgInfo {
result := []ArgInfo{}
for kk, a := range c.Args {
if c.Variadic && kk == len(c.Args)-1 {
continue
}
if kk > 0 && !a.IsState && !a.ImplementsEquals {
result = append(result, a)
}
}
return result
}
// PublicArgsArray returns all non-state args
func (c *ComponentInfo) PublicArgsArray() []ArgInfo {
result := []ArgInfo{}
for kk, a := range c.Args {
if kk > 0 && !a.IsState {
result = append(result, a)
}
}
return result
}
// Invoke builds the "invocation" line
func (c *ComponentInfo) Invoke() string {
results, args := []string{}, []string{}
for _, a := range c.Results {
results = append(results, "last"+a.Name)
}
idx := 0
for kk, a := range c.Args {
name := a.Name
if kk == 0 {
name = name + "Local"
} else if a.IsState {
lIdx := 0
for _, r := range c.Results {
if !r.IsState {
continue
}
if lIdx == idx {
idx++
name = "last" + r.Name
break
}
lIdx++
}
}
if c.Variadic && kk == len(c.Args)-1 {
name += "..."
}
args = append(args, name)
}
prefix := ""
if len(results) > 0 {
prefix = strings.Join(results, ", ") + "="
}
return prefix + c.Name + "(" + strings.Join(args, ", ") + ")"
}