forked from temporalio/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.go
More file actions
247 lines (227 loc) · 5.98 KB
/
Copy pathstringify.go
File metadata and controls
247 lines (227 loc) · 5.98 KB
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
247
package stringify
import (
"encoding/base64"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
"unicode"
"github.com/fatih/color"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/converter"
)
const (
maxWordLength = 120 // if text length is larger than maxWordLength, it will be inserted spaces
)
func AnyToString(val interface{}, printFully bool, maxFieldLength int, dc converter.DataConverter) string {
v := reflect.ValueOf(val)
if val == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
return ""
}
// Special types
switch tVal := val.(type) {
case string:
return tVal
case time.Time:
if tVal.IsZero() {
return "<zero>"
}
return tVal.String()
case *commonpb.Payload:
return dc.ToString(tVal)
case *commonpb.Payloads:
return fmt.Sprintf("[%s]", strings.Join(dc.ToStrings(tVal), ", "))
case int:
return strconv.FormatInt(int64(tVal), 10)
case int64:
return strconv.FormatInt(tVal, 10)
case int32:
return strconv.FormatInt(int64(tVal), 10)
case float64:
return strconv.FormatFloat(tVal, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(tVal), 'f', -1, 64)
case bool:
return strconv.FormatBool(tVal)
case byte:
return strconv.FormatInt(int64(tVal), 10)
case []byte:
if len(tVal) == 0 {
return ""
}
return fmt.Sprintf("[%v]", bytesToString(tVal))
}
switch v.Kind() {
case reflect.Invalid:
return ""
case reflect.Slice:
// All but []byte which is already handled.
return sliceToString(v, printFully, maxFieldLength, dc)
case reflect.Ptr:
return AnyToString(v.Elem().Interface(), printFully, maxFieldLength, dc)
case reflect.Map:
type keyValuePair struct {
key string
value string
}
kvPairs := make([]keyValuePair, 0, v.Len())
iter := v.MapRange()
for iter.Next() {
mapKey := iter.Key()
mapVal := iter.Value()
if !mapKey.CanInterface() || !mapVal.CanInterface() {
continue
}
mapKeyStr := AnyToString(mapKey.Interface(), true, 0, dc)
if mapKeyStr == "" {
continue
}
mapValStr := AnyToString(mapVal.Interface(), true, 0, dc)
if mapValStr == "" {
continue
}
kvPairs = append(kvPairs, keyValuePair{key: mapKeyStr, value: mapValStr})
}
if len(kvPairs) == 0 {
return ""
}
sort.Slice(kvPairs, func(i, j int) bool {
return strings.Compare(kvPairs[i].key, kvPairs[j].key) < 0
})
var b strings.Builder
b.WriteString("map{")
for i, kvPair := range kvPairs {
b.WriteString(kvPair.key)
b.WriteRune(':')
b.WriteString(kvPair.value)
if i != len(kvPairs)-1 {
b.WriteString(", ")
}
}
b.WriteRune('}')
return b.String()
case reflect.Struct:
var b strings.Builder
t := reflect.TypeOf(val)
b.WriteRune('{')
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.Kind() == reflect.Invalid {
continue
}
// Filter out private fields.
if !f.CanInterface() {
continue
}
fieldName := t.Field(i).Name
fieldStr := AnyToString(f.Interface(), printFully, maxFieldLength, dc)
if fieldStr == "" {
continue
}
if !isAttributeName(fieldName) && !strings.HasSuffix(fieldName, "Failure") {
if !printFully {
fieldStr = trimTextAndBreakWords(fieldStr, maxFieldLength)
} else if maxFieldLength != 0 { // for command run workflow and observe history
fieldStr = trimText(fieldStr, maxFieldLength)
}
}
if b.Len() > 1 {
b.WriteString(", ")
}
if strings.HasSuffix(fieldName, "Reason") ||
strings.HasSuffix(fieldName, "Cause") ||
strings.HasSuffix(fieldName, "Details") {
b.WriteString(color.MagentaString(fieldName))
} else if strings.HasSuffix(fieldName, "Input") ||
strings.HasSuffix(fieldName, "Result") {
b.WriteString(color.CyanString(fieldName))
} else if strings.HasSuffix(fieldName, "Failure") ||
strings.HasSuffix(fieldName, "Error") {
b.WriteString(color.RedString(fieldName))
} else {
b.WriteString(fieldName)
}
b.WriteRune(':')
b.WriteString(fieldStr)
}
if b.Len() == 1 { // '{' only
return ""
}
b.WriteRune('}')
return b.String()
default:
return fmt.Sprint(val)
}
}
func sliceToString(slice reflect.Value, printFully bool, maxFieldLength int, dc converter.DataConverter) string {
var b strings.Builder
b.WriteRune('[')
for i := 0; i < slice.Len(); i++ {
if i == 0 || printFully {
b.WriteString(AnyToString(slice.Index(i).Interface(), printFully, maxFieldLength, dc))
if i < slice.Len()-1 {
b.WriteRune(',')
}
if !printFully && slice.Len() > 1 {
b.WriteString(fmt.Sprintf("...%d more]", slice.Len()-1))
return b.String()
}
}
}
b.WriteRune(']')
return b.String()
}
func bytesToString(val []byte) string {
s := string(val)
isPrintable := true
for _, r := range s {
if !unicode.IsPrint(r) {
isPrintable = false
break
}
}
if isPrintable {
return strings.TrimSpace(s)
}
return base64.StdEncoding.EncodeToString(val)
}
// limit the maximum length for each field
func trimText(input string, maxFieldLength int) string {
if len(input) > maxFieldLength {
input = fmt.Sprintf("%s ... %s", input[:maxFieldLength/2], input[(len(input)-maxFieldLength/2):])
}
return input
}
// limit the maximum length for each field, and break long words for table item correctly wrap words
func trimTextAndBreakWords(input string, maxFieldLength int) string {
input = trimText(input, maxFieldLength)
return breakLongWords(input, maxWordLength)
}
// long words will make output in table cell looks bad,
// break long text "ltltltltllt..." to "ltlt ltlt lt..." will make use of table autowrap so that output is pretty.
func breakLongWords(input string, maxWordLength int) string {
if len(input) <= maxWordLength {
return input
}
cnt := 0
for i := 0; i < len(input); i++ {
if cnt == maxWordLength {
cnt = 0
input = input[:i] + " " + input[i:]
continue
}
cnt++
if input[i] == ' ' {
cnt = 0
}
}
return input
}
func isAttributeName(name string) bool {
eventType := strings.TrimSuffix(name, "EventAttributes")
_, ok := enumspb.EventType_value[eventType]
return ok
}