-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
157 lines (131 loc) · 3.86 KB
/
json.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
package instrument
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
const (
valueSep = ", "
null = "null"
startMap = "{"
endMap = "}"
startArray = "["
endArray = "]"
)
const (
emptyMap = startMap + endMap
emptyArray = startArray + endArray
)
// Color used in human-readable terminal output, chosen to mimic the default `jq` colors.
var (
stringColor = newStyle("#8ae234")
boolColor = newStyle("#34e2e2")
numberColor = newStyle("#fce94f")
nullColor = newStyle("#ad7fa8")
)
// The default color profile determined by lipgloss, used by ResetColor.
var defaultProfile = lipgloss.ColorProfile()
// Colorize forces colorized output, even without a TTY.
func Colorize() {
lipgloss.SetColorProfile(termenv.TrueColor)
}
// ResetColor returns to lipgloss' default color handling.
func ResetColor() {
lipgloss.SetColorProfile(defaultProfile)
}
// sprintf writes a string in the given color.
func sprintf(c *lipgloss.Style, format string, args ...interface{}) string {
return c.Render(fmt.Sprintf(format, args...))
}
// marshal emits colorized JSON output. Adapted from TylerBrock/colorjson with instrument-specific modifications:
// - Uses lipgloss rather than fatih/color.
// - The caller determines the key color, for example based on log level.
// - No newlines in the output.
// - No indentation.
// - No max string length.
// - No sorting map keys.
// - No input validation.
// - Handles more built-in types.
func marshal(jsonObj Tags, keyColor *lipgloss.Style) []byte {
buffer := bytes.Buffer{}
marshalValue(jsonObj, &buffer, keyColor)
return buffer.Bytes()
}
// marshalMap writes a JSON map.
func marshalMap(input map[string]interface{}, buf *bytes.Buffer, keyColor *lipgloss.Style) {
remaining := len(input)
if remaining == 0 {
buf.WriteString(emptyMap)
return
}
buf.WriteString(startMap)
for key, val := range input {
buf.WriteString(sprintf(keyColor, "\"%s\": ", key))
marshalValue(val, buf, keyColor)
remaining--
if remaining != 0 {
buf.WriteString(valueSep)
}
}
buf.WriteString(endMap)
}
// marshalArray writes a JSON array.
func marshalArray(input []interface{}, buf *bytes.Buffer, keyColor *lipgloss.Style) {
if len(input) == 0 {
buf.WriteString(emptyArray)
return
}
buf.WriteString(startArray)
for i, v := range input {
marshalValue(v, buf, keyColor)
if i < len(input)-1 {
buf.WriteString(valueSep)
}
}
buf.WriteString(endArray)
}
// marshalValue handles a bunch of different built-in types.
//
//nolint:cyclop
func marshalValue(input interface{}, buf *bytes.Buffer, keyColor *lipgloss.Style) {
switch val := input.(type) {
case Tags:
marshalMap(val, buf, keyColor)
case map[string]interface{}:
marshalMap(val, buf, keyColor)
case []interface{}:
marshalArray(val, buf, keyColor)
case string:
marshalString(val, buf)
case int, int8, int16, int32, int64:
i := reflect.ValueOf(val).Int()
buf.WriteString(sprintf(numberColor, "%d", i))
case uint, uint8, uint16, uint32, uint64:
i := reflect.ValueOf(val).Uint()
buf.WriteString(sprintf(numberColor, "%d", i))
case float32, float64:
f := reflect.ValueOf(val).Float()
buf.WriteString(sprintf(numberColor, strconv.FormatFloat(f, 'f', -1, 64)))
case bool:
buf.WriteString(sprintf(boolColor, (strconv.FormatBool(val))))
case nil:
buf.WriteString(sprintf(nullColor, null))
case json.Number:
buf.WriteString(sprintf(numberColor, val.String()))
case error:
buf.WriteString(sprintf(stringColor, "\"%s\"", val.Error()))
case time.Time:
buf.WriteString(sprintf(stringColor, "\"%s\"", val.UTC().Format(time.RFC3339)))
case fmt.Stringer:
buf.WriteString(sprintf(stringColor, "\"%s\"", val.String()))
}
}
// marshalString writes a JSON string.
func marshalString(str string, buf *bytes.Buffer) {
buf.WriteString(sprintf(stringColor, "\"%s\"", str))
}