-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.go
126 lines (105 loc) · 3.03 KB
/
go.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
package strlang
import (
"fmt"
"strings"
)
// Golang represents a Golang code generator that extends the Builder struct.
type Golang struct {
// packageName is a string that represents the name of
// the Go package being generated.
packageName string
// imports is a slice of strings that contains
// the imports required for the generated Go code.
imports []string
// Builder is an embedded struct that represents
// the base builder for generating the Go code.
*Builder
}
// NewGolang returns a new instance of Golang code builder.
func NewGolang(packageName string) *Golang {
return &Golang{
packageName,
[]string{},
NewBuilder(),
}
}
// If generates an if statement in the generated code.
func (b *Golang) If(statement string, inside func(), ln ...int) {
b.Block(fmt.Sprintf("if %s {", statement), inside, "}", ln...)
}
// Else generates an else statement in the generated code.
func (b *Golang) Else(inside func(), ln ...int) {
b.TrimRight("\n")
b.Block(" else {", inside, "}", ln...)
}
// ElseIf generates an else if statement in the generated code.
func (b *Golang) ElseIf(statement string, inside func(), ln ...int) {
b.TrimRight("\n")
b.Block(fmt.Sprintf(" else if %s {", statement), inside, "}", ln...)
}
// AddImports adds import statements to the generated code.
func (b *Golang) AddImports(imports ...string) {
b.imports = append(b.imports, imports...)
}
// Func generates a function definition in the generated code.
func (b *Golang) Func(fromStruct, name, parameters, output string, inside func()) {
if fromStruct != "" {
fromStruct = fmt.Sprintf("(%s) ", fromStruct)
}
if output != "" {
output = fmt.Sprintf("(%s) ", output)
}
b.Block(
fmt.Sprintf(
"func %s%s(%s) %s{",
fromStruct,
name,
parameters,
output,
),
inside,
"}",
2,
)
}
// Struct generates a struct definition in the generated code.
func (b *Golang) Struct(name string, inside func()) {
b.Block(fmt.Sprintf("type %s struct {", name), inside, "}", 2)
}
// StructField generates a struct field definition in the generated code.
func (b *Golang) StructField(name, goType string, docs ...map[string]string) {
strDocs := ""
if len(docs) != 0 {
doc := docs[0]
var arrDocs []string
for key, value := range doc {
arrDocs = append(arrDocs, fmt.Sprintf(`%s:"%s"`, key, value))
}
strDocs = fmt.Sprintf(" `%s`", strings.Join(arrDocs, " "))
}
b.WriteStringln(fmt.Sprintf("%s %s%s", name, goType, strDocs))
}
// String returns the generated code as a string.
func (b *Golang) String() string {
sb := NewBuilder()
sb.SetIndentChar(b.indentChar)
sb.StripIndent(b.currIndent)
sb.WriteStringln(fmt.Sprintf("package %s", b.packageName), 2)
if len(b.imports) != 0 {
var imports []string
alreadyExists := make(map[string]bool)
for _, i := range b.imports {
if !alreadyExists[i] {
imports = append(imports, i)
alreadyExists[i] = true
}
}
sb.Block("import (", func() {
for _, i := range imports {
sb.WriteStringln(fmt.Sprintf(`"%s"`, i))
}
}, ")", 2)
}
sb.WriteString(b.builder.String())
return sb.String()
}