-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.go
178 lines (146 loc) · 3.82 KB
/
fmt.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
package protocmp
import (
"fmt"
"strconv"
"strings"
"google.golang.org/protobuf/reflect/protoreflect"
)
func quoteString(v interface{}) string {
return strconv.Quote(fmt.Sprintf("%s", v))
}
func fmtMessage(m protoreflect.Message) string {
f := &formatter{}
f.printMessage(m)
return f.String()
}
func fmtList(list protoreflect.List, fd protoreflect.FieldDescriptor) string {
f := &formatter{}
f.printList(list, fd)
return f.String()
}
func fmtMap(mmap protoreflect.Map, fd protoreflect.FieldDescriptor) string {
f := &formatter{}
f.printMap(mmap, fd)
return f.String()
}
type formatter struct {
str string
}
func (f *formatter) String() string {
return strings.TrimSpace(f.str)
}
func (f *formatter) print(a ...interface{}) {
for _, v := range a {
f.str += fmt.Sprintf("%s", v)
}
}
func (f *formatter) trim() {
f.str = strings.TrimSuffix(f.str, " ")
}
func (f *formatter) trimAndPrint(v interface{}) {
f.trim()
f.str += fmt.Sprintf("%s", v)
}
func (f *formatter) printMessage(m protoreflect.Message) {
f.print("<")
defer f.trimAndPrint(">")
messageDesc := m.Descriptor()
fieldDescs := messageDesc.Fields()
size := fieldDescs.Len()
for i := 0; i < size; {
fd := fieldDescs.Get(i)
if od := fd.ContainingOneof(); od != nil {
fd = m.WhichOneof(od)
i += od.Fields().Len()
} else {
i++
}
if fd == nil || !m.Has(fd) {
continue
}
name := fd.Name()
f.print(name, ":")
// Use type name for group field name.
if fd.Kind() == protoreflect.GroupKind {
name = fd.Message().Name()
}
val := m.Get(fd)
f.printField(val, fd)
f.print(" ")
}
}
func (f *formatter) printField(val protoreflect.Value, fd protoreflect.FieldDescriptor) {
switch {
case fd.IsList():
f.printList(val.List(), fd)
case fd.IsMap():
f.printMap(val.Map(), fd)
default:
f.printSingular(val, fd)
}
}
func (f *formatter) printList(list protoreflect.List, fd protoreflect.FieldDescriptor) {
f.print("[")
defer f.trimAndPrint("]")
size := list.Len()
for i := 0; i < size; i++ {
f.printSingular(list.Get(i), fd)
if i != size-1 {
f.print(" ")
}
}
}
func (f *formatter) printMap(mmap protoreflect.Map, fd protoreflect.FieldDescriptor) {
f.print("map[")
defer f.trimAndPrint("]")
SortedMapRange(mmap, fd.MapKey().Kind(), func(key protoreflect.MapKey, val protoreflect.Value) bool {
f.printSingularKey(key.Value(), fd.MapKey())
f.print(":")
f.printSingular(val, fd.MapValue())
f.print(" ")
return true
})
}
func (f *formatter) printSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) {
f.printSingularWrapped(val, fd, true)
}
func (f *formatter) printSingularKey(val protoreflect.Value, fd protoreflect.FieldDescriptor) {
f.printSingularWrapped(val, fd, false)
}
func (f *formatter) printSingularWrapped(val protoreflect.Value, fd protoreflect.FieldDescriptor, wrapString bool) {
kind := fd.Kind()
switch kind {
case protoreflect.StringKind:
if !wrapString {
f.print(val.String())
return
}
s := val.String()
f.print("\"", s, "\"")
case protoreflect.BoolKind, protoreflect.Int32Kind, protoreflect.Int64Kind,
protoreflect.Sint32Kind, protoreflect.Sint64Kind,
protoreflect.Sfixed32Kind, protoreflect.Sfixed64Kind,
protoreflect.Uint32Kind, protoreflect.Uint64Kind,
protoreflect.Fixed32Kind, protoreflect.Fixed64Kind,
protoreflect.FloatKind,
protoreflect.DoubleKind,
protoreflect.BytesKind:
f.print(val)
case protoreflect.EnumKind:
num := val.Enum()
if desc := fd.Enum().Values().ByNumber(num); desc != nil {
f.print(string(desc.Name()))
} else {
// Use numeric value if there is no enum description.
f.print(int64(num))
}
case protoreflect.MessageKind, protoreflect.GroupKind:
if !val.Message().IsValid() {
f.print("<nil>")
return
}
f.printMessage(val.Message())
default:
panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
}
}