-
Notifications
You must be signed in to change notification settings - Fork 7
/
formatter.go
99 lines (79 loc) · 2.38 KB
/
formatter.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
package utils
import (
"fmt"
"github.com/PxyUp/fitter/pkg/builder"
"github.com/PxyUp/fitter/pkg/references"
"github.com/tidwall/gjson"
"html"
"os"
"strings"
)
const (
jsonPathStart = "{{{"
jsonPathEnd = "}}}"
placeHolder = "{PL}"
indexPlaceHolder = "{INDEX}"
humanIndexPlaceHolder = "{HUMAN_INDEX}"
refNamePrefix = "RefName="
envNamePrefix = "FromEnv="
)
func Format(str string, value builder.Jsonable, index *uint32) string {
if len(str) == 0 {
return str
}
if strings.Contains(str, placeHolder) && value != nil && (value.ToJson() != builder.EmptyString && len(value.ToJson()) != 0) {
str = strings.ReplaceAll(str, placeHolder, html.UnescapeString(value.ToJson()))
}
if strings.Contains(str, indexPlaceHolder) && index != nil {
str = strings.ReplaceAll(str, indexPlaceHolder, fmt.Sprintf("%d", *index))
}
if strings.Contains(str, humanIndexPlaceHolder) && index != nil {
str = strings.ReplaceAll(str, humanIndexPlaceHolder, fmt.Sprintf("%d", *index+1))
}
return formatJsonPathString(str, value)
}
func processPrefix(prefix string, value builder.Jsonable) string {
if strings.HasPrefix(prefix, refNamePrefix) {
refValue := strings.Split(strings.TrimPrefix(prefix, refNamePrefix), " ")
tmp := ""
if len(refValue) > 1 {
tmp = gjson.Parse(html.UnescapeString(references.Get(refValue[0]).ToJson())).Get(refValue[1]).String()
}
if len(refValue) == 1 {
tmp = html.UnescapeString(references.Get(refValue[0]).ToJson())
}
return builder.PureString(tmp).ToJson()
}
if strings.HasPrefix(prefix, envNamePrefix) {
envValue := strings.TrimPrefix(prefix, envNamePrefix)
return builder.PureString(os.Getenv(envValue)).ToJson()
}
return gjson.Parse(value.ToJson()).Get(prefix).String()
}
func formatJsonPathString(str string, value builder.Jsonable) string {
new := ""
runes := []rune(str)
isInJSONPath := false
path := ""
for i := 0; i < len(runes); i++ {
if !isInJSONPath {
new += string(runes[i])
} else {
path += string(runes[i])
}
if isInJSONPath && strings.HasSuffix(path, jsonPathEnd) {
path = strings.TrimSuffix(path, jsonPathEnd)
new += processPrefix(path, value)
isInJSONPath = false
path = ""
}
if !isInJSONPath && strings.HasSuffix(new, jsonPathStart) {
new = strings.TrimSuffix(new, jsonPathStart)
isInJSONPath = true
}
}
if isInJSONPath {
return str
}
return new
}