forked from vsapronov/gopoetry
/
trait.go
82 lines (66 loc) · 1.79 KB
/
trait.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
package scala
import (
"strings"
)
type TraitDeclaration struct {
name string
extends []string
modifiers []string
attributes []Writable
members *StatementsDeclaration
}
func (self *TraitDeclaration) addModifier(modifier string) *TraitDeclaration {
self.modifiers = append(self.modifiers, modifier)
return self
}
func (self *TraitDeclaration) Private() *TraitDeclaration {
return self.addModifier("private")
}
func (self *TraitDeclaration) Public() *TraitDeclaration {
return self.addModifier("public")
}
func (self *TraitDeclaration) Sealed() *TraitDeclaration {
return self.addModifier("sealed")
}
func (self *TraitDeclaration) Extends(types ...string) *TraitDeclaration {
self.extends = append(self.extends, types...)
return self
}
func (self *TraitDeclaration) Define(block bool) *StatementsDeclaration {
self.members = Statements(block, true)
return self.members
}
func (self *TraitDeclaration) AddAttributes(attributes ...Writable) *TraitDeclaration {
self.attributes = append(self.attributes, attributes...)
return self
}
func (self *TraitDeclaration) Attribute(code string) *TraitDeclaration {
return self.AddAttributes(Attribute(code))
}
func Trait(name string) *TraitDeclaration {
return &TraitDeclaration{
name: name,
attributes: []Writable{},
members: nil,
}
}
func (self *TraitDeclaration) WriteCode(writer CodeWriter) {
for _, attribute := range self.attributes {
attribute.WriteCode(writer)
writer.Eol()
}
if len(self.modifiers) > 0 {
writer.Write(strings.Join(self.modifiers, " ") + " ")
}
writer.Write("trait ")
writer.Write(self.name)
if len(self.extends) > 0 {
writer.Write(" extends " + strings.Join(self.extends, " with "))
}
if self.members != nil {
writer.Write(" ")
self.members.WriteCode(writer)
} else {
writer.Eol()
}
}