/
prettify.go
236 lines (216 loc) · 5.51 KB
/
prettify.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
package syntax
import (
"fmt"
"strings"
"github.com/arr-ai/arrai/pkg/fu"
"github.com/arr-ai/arrai/rel"
)
const indentStr = " "
type Enumerable interface {
ArrayEnumerator() rel.ValueEnumerator
}
// PrettifyString returns a string which represents `rel.Value` with more reabable format.
// For example, `{b: 2, a: 1, c: (a: 2, b: {aa: {bb: (a: 22, d: {3, 1, 2})}})}` is formatted to:
//
// {
// b: 2,
// a: 1,
// c: (
// a: 2,
// b: {
// aa: {
// bb: (
// a: 22,
// d: {3, 1, 2}
// )
// }
// }
// )
// }
func PrettifyString(val interface{}, indentsNum int) (string, error) {
switch t := val.(type) {
case rel.EmptySet:
return "{}", nil
case rel.DictEntryTuple:
key := t.MustGet("@")
prettyKey, err := PrettifyString(key, indentsNum)
if err != nil {
return "", err
}
val := t.MustGet(rel.DictValueAttr)
prettyVal, err := PrettifyString(val, indentsNum)
if err != nil {
return "", err
}
return fmt.Sprintf("%v: %v", prettyKey, prettyVal), nil
case rel.Attr: // a: 1
prettyVal, err := PrettifyString(t.Value, indentsNum)
if err != nil {
return "", err
}
name := t.Name
if name == "" {
name = "''"
}
return fmt.Sprintf("%v: %v", name, prettyVal), nil
case rel.Tuple: // (a: 1)
return prettifyTuple(t, indentsNum+1)
case rel.Array: // [1, 2]
return prettifyArray(t, indentsNum+1)
case rel.Dict: // {'a': 1}
return prettifyDict(t, indentsNum+1)
case rel.Relation:
return prettifyRelation(t, indentsNum+1)
case rel.OrderableSet: // {1, 2}
return prettifyOrderableSet(t, indentsNum+1)
case rel.String:
return prettifyString(t)
case nil:
return "", nil
case fmt.Stringer:
return t.String(), nil
default:
return "", fmt.Errorf("unknown type: %T", t)
}
}
func prettifyOrderableSet(arr rel.OrderableSet, indentsNum int) (string, error) {
content, err := prettifyItems(rel.ValueEnumeratorToSlice(arr.OrderedValues()), indentsNum)
if err != nil {
return "", err
}
return fmt.Sprintf("{%v}", content), nil
}
func prettifyRelation(r rel.Relation, indentsNum int) (string, error) {
sb := strings.Builder{}
indent := func() {
sb.WriteString("\n")
sb.WriteString(getIndents(indentsNum))
}
sb.WriteString("{")
indent()
sorted := r.AttrsName().GetSorted()
sb.WriteString(fmt.Sprintf("|%s|", sorted))
indent()
count := r.Count()
for i := r.OrderedValuesEnumerator(sorted); i.Next(); {
vals := i.Values()
content, err := prettifyItems(vals, indentsNum+1)
if err != nil {
return "", err
}
count--
sb.WriteString("(")
sb.WriteString(content)
if count == 0 {
sb.WriteString("),\n" + getIndents(indentsNum-1))
} else {
sb.WriteString("),\n" + getIndents(indentsNum))
}
}
sb.WriteString("}")
return sb.String(), nil
}
func prettifyArray(arr rel.Array, indentsNum int) (string, error) {
content, err := prettifyItems(arr.Values(), indentsNum)
if err != nil {
return "", err
}
return fmt.Sprintf("[%v]", content), nil
}
func prettifyDict(dict rel.Dict, indentsNum int) (string, error) {
vals := make([]rel.Value, dict.Count())
for i, item := range dict.OrderedEntries() {
vals[i] = item
}
content, err := prettifyItems(vals, indentsNum)
if err != nil {
return "", err
}
return fmt.Sprintf("{%v}", content), nil
}
func prettifyTuple(tuple rel.Tuple, indentsNum int) (string, error) {
var sb strings.Builder
for index, name := range tuple.Names().OrderedNames() {
str, err := prettifyItem(index, rel.NewAttr(name, tuple.MustGet(name)), false, indentsNum)
if err != nil {
return "", err
}
sb.WriteString(str)
}
if tuple.Count() > 0 {
sb.WriteString("\n" + getIndents(indentsNum-1))
}
return fmt.Sprintf("(%v)", sb.String()), nil
}
func prettifyString(str rel.String) (string, error) {
return fu.Repr(str), nil
}
// prettifyItems returns a pretty string representation of the contents of a set or array.
func prettifyItems(vals []rel.Value, indentsNum int) (string, error) {
var sb strings.Builder
simple := isSimpleValues(vals)
for index, item := range vals {
str, err := prettifyItem(index, item, simple, indentsNum)
if err != nil {
return "", err
}
sb.WriteString(str)
}
if !simple {
sb.WriteString("\n" + getIndents(indentsNum-1))
}
return sb.String(), nil
}
// prettifyItem returns the pretty string for an item at an index within a collection.
func prettifyItem(index int, item interface{}, simple bool, indent int) (string, error) {
var sb strings.Builder
formattedStr, err := PrettifyString(item, indent)
if err != nil {
return "", err
}
if simple && index > 0 {
sb.WriteString(", ")
}
if !simple {
sb.WriteString("\n" + getIndents(indent))
}
_, err = fmt.Fprintf(&sb, "%v", formattedStr)
if !simple {
sb.WriteString(",")
}
if err != nil {
return "", err
}
return sb.String(), nil
}
// isSimple returns true if the value should be pretty printed on a single line.
func isSimple(val rel.Value) bool {
switch t := val.(type) {
case rel.Number, rel.String, rel.EmptySet, nil:
return true
case rel.Array:
return isSimpleValues(t.Values())
case rel.GenericSet:
return isSimpleValues(rel.ValueEnumeratorToSlice(t.OrderedValues()))
case rel.Dict:
return t.Count() == 0
}
return false
}
// isSimpleEnumerator returns true if all of the enumerated values should be pretty printed on a
// single line.
func isSimpleValues(vals []rel.Value) bool {
for _, item := range vals {
if !isSimple(item) {
return false
}
}
return true
}
func getIndents(indentsNum int) string {
var sb strings.Builder
for i := 0; i < indentsNum; i++ {
sb.WriteString(indentStr)
}
return sb.String()
}