-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatting.go
74 lines (65 loc) · 1.23 KB
/
formatting.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
package testutils
import (
"encoding/json"
"fmt"
"strings"
)
func NiceJs(testJson string) string {
var data map[string]interface{}
err := json.Unmarshal([]byte(testJson), &data)
if err != nil {
panic(fmt.Errorf("invalid test json"))
}
result, err := json.MarshalIndent(data, "", " ")
if err != nil {
panic(fmt.Errorf("unexpected json marshal error"))
}
return string(result)
}
func NiceYaml(testYaml string) string {
const tabStop = 2
var sb strings.Builder
col := 0
for _, ch := range testYaml {
if ch == '\n' {
col = 0
} else if ch == '\t' {
sb.WriteRune(' ')
col++
for col%tabStop != 0 {
sb.WriteRune(' ')
col++
}
continue
} else if ch < 32 {
continue
} else {
col++
}
sb.WriteRune(ch)
}
testYaml = sb.String()
maxSpaces := len(testYaml)
lines := strings.Split(testYaml, "\n")
for _, line := range lines {
if len(line) > 0 {
spaces := 0
for spaces < len(line) && line[spaces] == ' ' {
spaces++
}
if spaces < maxSpaces && spaces < len(line) {
maxSpaces = spaces
}
}
}
sb.Reset()
for _, line := range lines {
if len(line) > maxSpaces {
text := line[maxSpaces:]
if len(text) > 0 {
sb.WriteString(text + "\n")
}
}
}
return sb.String()
}