forked from vsapronov/gopoetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
83 lines (68 loc) · 2.05 KB
/
property.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
package typescript
import (
"fmt"
"strings"
)
// PropertyDeclaration declares a property
type PropertyDeclaration struct {
name string
typeName string
modifiers []string
optional bool
initializer string
}
func (prop *PropertyDeclaration) addModifier(modifier string) *PropertyDeclaration {
prop.modifiers = append(prop.modifiers, modifier)
return prop
}
// Private marks the property as private
func (prop *PropertyDeclaration) Private() *PropertyDeclaration {
return prop.addModifier("private")
}
// Readonly marks the property as readonly
func (prop *PropertyDeclaration) Readonly() *PropertyDeclaration {
return prop.addModifier("readonly")
}
// Public marks the property as public
func (prop *PropertyDeclaration) Public() *PropertyDeclaration {
return prop.addModifier("public")
}
// Static marks the property as static. Can only be used on classes, not interfaces
func (prop *PropertyDeclaration) Static() *PropertyDeclaration {
return prop.addModifier("static")
}
// Optional marks the property as optional
func (prop *PropertyDeclaration) Optional() *PropertyDeclaration {
prop.optional = true
return prop
}
// Initializer sets the properties initializer
func (prop *PropertyDeclaration) Initializer(initializer string) *PropertyDeclaration {
prop.initializer = initializer
return prop
}
// Property starts building a new property. returns PropertyDeclaration
func Property(typeName string, name string) *PropertyDeclaration {
return &PropertyDeclaration{
name: name,
typeName: typeName,
modifiers: []string{},
}
}
// WriteCode writes the property to the writer
func (prop *PropertyDeclaration) WriteCode(writer CodeWriter) {
optionalStr := ""
if prop.optional {
optionalStr = "?"
}
declaration := fmt.Sprintf("%s%s: %s", prop.name, optionalStr, prop.typeName)
if len(prop.modifiers) > 0 {
declaration = strings.Join(prop.modifiers, " ") + " " + declaration
}
if prop.initializer != "" {
declaration = declaration + " = " + prop.initializer
}
declaration = declaration + ";"
writer.Write(declaration)
writer.Eol()
}