-
Notifications
You must be signed in to change notification settings - Fork 52
/
go-dumper.go
54 lines (46 loc) · 1.16 KB
/
go-dumper.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
package serz
import (
"encoding/hex"
"fmt"
"github.com/phith0n/litter"
"io"
"reflect"
"regexp"
"strings"
)
func DumpToGoStruct(ser *Serialization) {
sq := litter.Options{
StripPackageNames: false,
HidePrivateFields: true,
FieldExclusions: regexp.MustCompile(`^(XXX_.*)$`), // XXX_ is a prefix of fields generated by protoc-gen-go
Separator: " ",
DumpFunc: customDumpFunction,
HideZeroValues: true,
}
sq.Dump(ser)
}
func customDumpFunction(v reflect.Value, w io.Writer) bool {
if !v.CanInterface() {
return false
}
if bytearr, ok := v.Interface().([]*TCValue); ok {
b := strings.Builder{}
for _, value := range bytearr {
if value.TypeCode != "B" {
return false
}
b.WriteString("\\x" + hex.EncodeToString([]byte{value.Byte}))
}
_, _ = io.WriteString(w, fmt.Sprintf(`zkar.NewTCValueBytes([]byte("%s"))`, b.String()))
return true
}
if bytearr, ok := v.Interface().([]byte); ok {
_, _ = io.WriteString(w, `[]byte("`)
for _, value := range bytearr {
_, _ = io.WriteString(w, fmt.Sprintf("\\x%s", hex.EncodeToString([]byte{value})))
}
_, _ = io.WriteString(w, `")`)
return true
}
return false
}