-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_struct.go
62 lines (48 loc) · 1.27 KB
/
block_struct.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
package codegen
import (
"strings"
)
type structBlock struct {
name string
props []*PropertyDecl
}
// Struct creates a new struct block
func (f *File) Struct(structName string) *structBlock {
str := newStruct(structName)
f.append(str)
return str
}
// Props adds property declarations to the struct block
func (s *structBlock) Props(properties ...*PropertyDecl) *structBlock {
s.props = properties
return s
}
// AddProp adds a new property declaration to the struct block
func (s *structBlock) AddProp(propertyName, typeName string) *PropertyDecl {
prop := Property(propertyName, typeName)
s.addProp(prop)
return prop
}
// AddQualProp adds a new property declaration with its alias to the struct block
func (s *structBlock) AddQualProp(propertyName, alias, typeName string) *PropertyDecl {
prop := QualProperty(propertyName, alias, typeName)
s.addProp(prop)
return prop
}
func (s *structBlock) addProp(property *PropertyDecl) {
s.props = append(s.props, property)
}
func newStruct(name string) *structBlock {
return &structBlock{
name: name,
props: make([]*PropertyDecl, 0),
}
}
func (s *structBlock) write(sb *strings.Builder) {
writeNewLineF(sb, "type %s struct {", s.name)
for _, prop := range s.props {
prop.wr(sb)
newLine(sb)
}
writeByteNewLine(sb, '}')
}