-
Notifications
You must be signed in to change notification settings - Fork 45
/
printer.go
246 lines (225 loc) · 6.37 KB
/
printer.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// package printer prints out sysl datamodels back to source code using the Printer struct.
// Source code does not have complete fidelity, and elements will be printed out in alphabetical order.
package printer
import (
"fmt"
"io"
"strings"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
)
const (
APPLICATIONINDENT = 4
ENDPOINTINDENT = 8
MAXLINE = 80
patterns = "patterns"
)
// Printer prints sysl data structures out to source code
type Printer struct {
io.Writer
}
// NewPrinter returns a printer that can be used to print out sysl source code from data structures
func NewPrinter(buf io.Writer) *Printer {
return &Printer{Writer: buf}
}
// PrintModule Prints a whole module
func (p *Printer) PrintModule(mod *sysl.Module) {
for _, key := range alphabeticalApplications(mod.Apps) {
p.PrintApplication(mod.Apps[key])
}
}
// PrintApplication prints applications:
// App:
func (p *Printer) PrintApplication(a *sysl.Application) {
fmt.Fprintf(p.Writer, "\n%s", strings.Join(a.Name.GetPart(), ""))
p.PrintPatterns(a.GetAttrs())
fmt.Fprint(p.Writer, ":\n")
for _, key := range alphabeticalAttributes(a.Attrs) {
if key == patterns {
continue
}
p.PrintAttrs(key, a.Attrs[key], APPLICATIONINDENT)
}
for _, key := range alphabeticalTypes(a.Types) {
p.PrintTypeDecl(key, a.Types[key])
}
for _, key := range alphabeticalEndpoints(a.Endpoints) {
p.PrintEndpoint(a.Endpoints[key])
}
}
// PrintTypeDecl prints Type decelerations:
// !type Foo:
// this <: string
func (p *Printer) PrintTypeDecl(key string, t *sysl.Type) {
switch v := t.Type.(type) {
case *sysl.Type_Enum_:
fmt.Fprintf(p.Writer, " !enum %s:\n", key)
for _, key := range alphabeticalAttributes(t.GetAttrs()) {
if key != patterns {
p.PrintAttrs(key, t.GetAttrs()[key], ENDPOINTINDENT)
}
}
enumFields := v.Enum.Items
for _, key := range alphabeticalInts(enumFields) {
fmt.Fprintf(p.Writer, " %s: %d\n", key, enumFields[key])
}
default:
fmt.Fprintf(p.Writer, " !type %s", key)
p.PrintPatterns(t.GetAttrs())
fmt.Fprint(p.Writer, ":\n")
tuple := t.GetTuple()
for _, key := range alphabeticalAttributes(t.GetAttrs()) {
if key != patterns {
p.PrintAttrs(key, t.GetAttrs()[key], ENDPOINTINDENT)
}
}
if tuple == nil || tuple.AttrDefs == nil || len(tuple.AttrDefs) == 0 {
fmt.Fprintf(p.Writer, " ...\n")
return
}
for _, key := range alphabeticalTypes(tuple.AttrDefs) {
typeClass, typeIdent := syslutil.GetTypeDetail(tuple.AttrDefs[key])
switch typeClass {
case "primitive":
typeIdent = strings.ToLower(typeIdent)
case "sequence":
if foo := tuple.AttrDefs[key].GetSequence(); foo != nil {
typeClass, typeIdent = syslutil.GetTypeDetail(foo)
if typeClass == "primitive" {
typeIdent = strings.ToLower(typeIdent)
}
}
typeIdent = "sequence of " + typeIdent
}
fmt.Fprintf(p.Writer, " %s <: %s\n", key, typeIdent)
}
}
}
// Prints patterns in square brackets: [~foo, ~bar]
func (p *Printer) PrintPatterns(attrs map[string]*sysl.Attribute) {
if attrs == nil {
return
}
patterns := GetPatterns(attrs)
if len(patterns) > 0 {
fmt.Fprint(p.Writer, "[")
for i, pattern := range patterns {
fmt.Fprintf(p.Writer, "~%s", pattern)
if i != len(patterns)-1 {
fmt.Fprintf(p.Writer, ", ")
}
}
fmt.Fprint(p.Writer, "]")
}
}
func GetPatterns(attrs map[string]*sysl.Attribute) []string {
var ret = []string{}
patterns, has := attrs[patterns]
if has {
if x := patterns.GetA(); x != nil {
for _, y := range x.Elt {
ret = append(ret, y.GetS())
}
}
}
return ret
}
// PrintEndpoint prints endpoints:
// Endpoint:
func (p *Printer) PrintEndpoint(e *sysl.Endpoint) {
fmt.Fprintf(p.Writer, " %s", e.Name)
if len(e.Param) != 0 {
p.PrintParam(e.Param)
}
p.PrintPatterns(e.Attrs)
fmt.Fprintf(p.Writer, ":\n")
if len(e.Stmt) == 0 {
fmt.Fprint(p.Writer, " ...\n")
}
for key, attr := range e.Attrs {
if key == patterns {
continue
}
p.PrintAttrs(key, attr, ENDPOINTINDENT)
}
for _, stmnt := range e.Stmt {
p.PrintStatement(stmnt)
}
}
// PrintParam prints Parameters:
// Endpoint(This <: ParamHere):
func (p *Printer) PrintParam(params []*sysl.Param) {
ans := "("
for i, param := range params {
ans += param.Name + " <: " + p.ParamType(param)
if i != len(params)-1 {
ans += ","
}
}
ans += ")"
fmt.Fprint(p.Writer, ans)
}
// PrintAttrs prints different statements:
// return string
// My <- call
// lookup db
func (p *Printer) PrintStatement(s *sysl.Statement) {
if call := s.GetCall(); call != nil {
p.PrintCall(call)
}
if action := s.GetAction(); action != nil {
p.PrintAction(action)
}
if ret := s.GetRet(); ret != nil {
p.PrintReturn(ret)
}
}
// PrintReturn prints return statements:
// return foo <: type
func (p *Printer) PrintReturn(r *sysl.Return) {
fmt.Fprintf(p.Writer, " return %s\n", r.Payload)
}
// PrintAction prints actions:
// lookup data
func (p *Printer) PrintAction(a *sysl.Action) {
fmt.Fprintf(p.Writer, " %s\n", a.GetAction())
}
// PrintAttrs prints Attributes:
// @owner="server"
func (p *Printer) PrintAttrs(key string, a *sysl.Attribute, indentNum int) {
multiLine := strings.Split(a.GetS(), "\n")
indent := strings.Repeat(" ", indentNum)
if len(multiLine) == 1 && len(multiLine[0]) < MAXLINE {
fmt.Fprintf(p.Writer, "%s@%s = \"%s\"\n", indent, key, multiLine[0])
return
}
fmt.Fprintf(p.Writer, "%s@%s =:\n", indent, key)
for _, line := range multiLine {
for i := 0; i < len(line); i += MAXLINE {
endindex := i + MAXLINE
if lineLen := len(line); endindex >= lineLen {
endindex = lineLen
}
fmt.Fprintf(p.Writer, "%s |%s\n", indent, line[i:endindex])
}
}
}
// ParamType prints:
// foo(this <: <ParamType>):
func (p *Printer) ParamType(param *sysl.Param) string {
if param.Type == nil {
return ""
}
// Ref.Appname.Part is the type name if the type is in the same package and the application name of where it's at
// stored if its in another application and then Ref.Path is the type name
if a := param.Type.GetTypeRef(); a != nil {
ans := append(a.Ref.Appname.Part, a.Ref.Path...)
return strings.Join(ans, ".")
}
return strings.Join(param.Type.GetTypeRef().Ref.Appname.Part, "")
}
// PrintCall prints:
// AnApp <- AnEndpoint
func (p *Printer) PrintCall(c *sysl.Call) {
fmt.Fprintf(p.Writer, " %s <- %s\n", strings.Join(c.Target.GetPart(), ""), c.GetEndpoint())
}