-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
256 lines (239 loc) · 6.34 KB
/
printer.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
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
248
249
250
251
252
253
254
255
256
// Copyright (C) 2023 by Posit Software, PBC
package rsf
import (
"bufio"
"fmt"
"io"
"reflect"
"strings"
)
func Print(w io.Writer, r *bufio.Reader) error {
// Create a new reader since we need to read the RSF data.
reader := NewReader()
// Read the RSF index. We'll use this data to help print the information.
idx, err := reader.ReadIndex(r)
if err != nil {
return fmt.Errorf("error reading index: %s", err)
}
// Iterate the fields recursively and print the data.
var i int
for {
i++
// Read full object size
_, err = reader.ReadSizeField(r)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
// Add blank newline unless at first object
if i > 1 {
_, err = fmt.Fprintln(w, "")
if err != nil {
return nil
}
}
// Print object header
pad := strings.Repeat(" ", 16)
header := fmt.Sprintf("%sObject[%d]%s", pad, i, pad)
line := strings.Repeat("-", len(header))
_, err = fmt.Fprintf(w, "%s\n%s\n%s\n", line, header, line)
if err != nil {
return nil
}
// Print data for each field of the object.
for _, f := range idx {
err = printField("", f, w, r, reader, 0)
if err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("error printing data: %s", err)
}
}
}
}
func printField(parentKey string, f IndexEntry, w io.Writer, r *bufio.Reader, reader Reader, indent int) error {
pad := strings.Repeat(" ", indent*4)
switch f.FieldType {
case FieldTypeBool:
b, err := reader.ReadBoolField(r)
if err != nil {
return fmt.Errorf("error reading bool: %s", err)
}
_, err = fmt.Fprintf(w, "%s%s (bool): %t\n", pad, f.FieldName, b)
if err != nil {
return err
}
case FieldTypeInt64:
i, err := reader.ReadIntField(r)
if err != nil {
return fmt.Errorf("error reading int: %s", err)
}
_, err = fmt.Fprintf(w, "%s%s (int): %d\n", pad, f.FieldName, i)
if err != nil {
return err
}
case FieldTypeFloat:
fl, err := reader.ReadFloatField(r)
if err != nil {
return fmt.Errorf("error reading float: %s", err)
}
_, err = fmt.Fprintf(w, "%s%s (float): %f\n", pad, f.FieldName, fl)
if err != nil {
return err
}
case FieldTypeFixedStr:
s, err := reader.ReadFixedStringField(f.FieldSize, r)
if err != nil {
return fmt.Errorf("error reading fixed-length string: %s", err)
}
_, err = fmt.Fprintf(w, "%s%s (string(%d)): %s\n", pad, f.FieldName, f.FieldSize, s)
if err != nil {
return err
}
case FieldTypeVarStr:
s, err := reader.ReadStringField(r)
if err != nil {
return fmt.Errorf("error reading variable-length string field %s: %s", f.FieldName, err)
}
_, err = fmt.Fprintf(w, "%s%s (string): %s\n", pad, f.FieldName, s)
if err != nil {
return err
}
case FieldTypeArray:
sz, err := reader.ReadSizeField(r)
if err != nil {
return fmt.Errorf("error reading array size: %s", err)
}
arrayLen, err := reader.ReadSizeField(r)
if err != nil {
return fmt.Errorf("error reading array length: %s", err)
}
key := f.FieldName
if parentKey != "" {
key = strings.Join([]string{parentKey, f.FieldName}, "...")
}
indexValues := make([]any, 0)
// Record index values
if f.Indexed {
indexSz := f.IndexSize
for i := 0; i < arrayLen; i++ {
switch reflect.Kind(f.IndexType) {
case reflect.String:
var sIndexVal string
sIndexVal, err = reader.ReadFixedStringField(indexSz, r)
if err != nil {
return fmt.Errorf("error reading index string value: %s", err)
}
indexValues = append(indexValues, sIndexVal)
case reflect.Int64:
var intIndexVal int64
intIndexVal, err = reader.ReadIntField(r)
if err != nil {
return fmt.Errorf("error reading index int64 value: %s", err)
}
indexValues = append(indexValues, intIndexVal)
}
// Discard index size
err = reader.Discard(4, r)
if err != nil {
return fmt.Errorf("error discarding index bytes: %s", err)
}
}
}
if len(indexValues) > 0 {
_, err = fmt.Fprintf(w, "%s%s (indexed array(%d)):\n", pad, f.FieldName, arrayLen)
if err != nil {
return err
}
} else {
_, err = fmt.Fprintf(w, "%s%s (array(%d)):\n", pad, f.FieldName, arrayLen)
if err != nil {
return err
}
}
fields:
for i := 0; i < arrayLen; i++ {
if f.Subfields != nil {
var indexVal string
if len(indexValues) > 0 {
switch t := indexValues[i].(type) {
case string:
indexVal = fmt.Sprintf(" %s", t)
case int64:
indexVal = fmt.Sprintf(" %d", t)
}
}
_, err = fmt.Fprintf(w, "%s-%s\n", pad+strings.Repeat(" ", 4), indexVal)
for _, subfield := range f.Subfields {
err = printField(key, subfield, w, r, reader, indent+1)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
} else {
_, err = fmt.Fprintf(w, "%s-", pad+strings.Repeat(" ", 4))
switch reflect.Kind(f.SubfieldType) {
case reflect.String:
var s string
s, err = reader.ReadStringField(r)
if err != nil {
return fmt.Errorf("error reading array string field: %s", err)
}
_, err = fmt.Fprintf(w, "%s\n", s)
if err != nil {
return err
}
case reflect.Bool:
var b bool
b, err = reader.ReadBoolField(r)
if err != nil {
return fmt.Errorf("error reading array bool field: %s", err)
}
_, err = fmt.Fprintf(w, "%t\n", b)
if err != nil {
return err
}
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
var d int64
d, err = reader.ReadIntField(r)
if err != nil {
return fmt.Errorf("error reading array int field: %s", err)
}
_, err = fmt.Fprintf(w, "%d\n", d)
if err != nil {
return err
}
case reflect.Float32, reflect.Float64:
var fl float64
fl, err = reader.ReadFloatField(r)
if err != nil {
return fmt.Errorf("error reading array float field: %s", err)
}
_, err = fmt.Fprintf(w, "%f\n", fl)
if err != nil {
return err
}
default:
_, err = fmt.Fprintf(w, " cannot print data for arrays of arrays\n")
if err != nil {
return err
}
err = reader.Discard(sz-8, r)
if err != nil {
return fmt.Errorf("error reading unknown array field data: %s", err)
}
break fields
}
}
}
default:
return fmt.Errorf("cannot print unknown field %s with type %d", f.FieldName, f.FieldType)
}
return nil
}