forked from vsapronov/gopoetry
/
unit.go
54 lines (46 loc) · 1.15 KB
/
unit.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
package scala
import "fmt"
type UnitDeclaration struct {
package_ string
imports []string
declarations []Writable
}
func Unit(package_ string) *UnitDeclaration {
return &UnitDeclaration{
package_,
[]string{},
[]Writable{},
}
}
func (self *UnitDeclaration) AddImports(imports ...string) *UnitDeclaration {
self.imports = append(self.imports, imports...)
return self
}
func (self *UnitDeclaration) Import(package_ string) *UnitDeclaration {
self.AddImports(package_)
return self
}
func (self *UnitDeclaration) AddDeclarations(declarations ...Writable) *UnitDeclaration {
self.declarations = append(self.declarations, declarations...)
return self
}
func (self *UnitDeclaration) Code() string {
writer := CreateWriter()
self.WriteCode(&writer)
return writer.Code()
}
func (self *UnitDeclaration) WriteCode(writer CodeWriter) {
writer.Write(fmt.Sprintf("package %s", self.package_))
writer.Eol()
if len(self.imports) > 0 {
writer.Eol()
for _, import_ := range self.imports {
writer.Write(fmt.Sprintf("import %s", import_))
writer.Eol()
}
}
for _, class := range self.declarations {
writer.Eol()
class.WriteCode(writer)
}
}