From e35b3a1e1cb8ab32c9499e4664a5fc04ac1dd4d2 Mon Sep 17 00:00:00 2001 From: phith0n Date: Mon, 16 Aug 2021 06:43:47 +0800 Subject: [PATCH] custom go struct dump --- builder.go | 12 ++++++++++++ cli/main.go | 11 +---------- go-dumper.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 6 ++++-- 5 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 go-dumper.go diff --git a/builder.go b/builder.go index 01bd61a..1e3bf27 100644 --- a/builder.go +++ b/builder.go @@ -53,3 +53,15 @@ func SimpleClassDesc(className string, svuid int64, flags byte, handler uint32, return &desc } + +func NewTCValueBytes(data []byte) []*TCValue { + var l []*TCValue + for _, b := range data { + value := &TCValue{ + TypeCode: "B", + Byte: b, + } + l = append(l, value) + } + return l +} diff --git a/cli/main.go b/cli/main.go index aa17ae6..b21e176 100644 --- a/cli/main.go +++ b/cli/main.go @@ -4,13 +4,11 @@ import ( "fmt" "github.com/phith0n/zkar" "github.com/phith0n/zkar/payloads" - "github.com/sanity-io/litter" "github.com/thoas/go-funk" "github.com/urfave/cli/v2" "io/fs" "io/ioutil" "os" - "regexp" "strings" ) @@ -103,14 +101,7 @@ func main() { } if context.Bool("golang") { - sq := litter.Options{ - StripPackageNames: false, - HidePrivateFields: true, - FieldExclusions: regexp.MustCompile(`^(XXX_.*)$`), // XXX_ is a prefix of fields generated by protoc-gen-go - Separator: " ", - HideZeroValues: true, - } - sq.Dump(ser) + zkar.DumpToGoStruct(ser) } else { fmt.Println(ser.ToString()) } diff --git a/go-dumper.go b/go-dumper.go new file mode 100644 index 0000000..82aaf2c --- /dev/null +++ b/go-dumper.go @@ -0,0 +1,54 @@ +package zkar + +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 +} diff --git a/go.mod b/go.mod index b5ded96..0cc037b 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.1.0 // indirect - github.com/sanity-io/litter v1.5.1 + github.com/phith0n/litter v1.5.3 github.com/stretchr/testify v1.7.0 github.com/thoas/go-funk v0.9.0 github.com/urfave/cli/v2 v2.3.0 diff --git a/go.sum b/go.sum index 17dafad..7caf389 100644 --- a/go.sum +++ b/go.sum @@ -11,13 +11,15 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/phith0n/litter v1.5.2 h1:oqFA78YEaLJnSwLesCxwB4VoaIibS3UssYsb+KmKWZU= +github.com/phith0n/litter v1.5.2/go.mod h1:sQD5zUYa1fFVC3IVALS0wkyS5FcQ5LT4OoD1bgC6ZDU= +github.com/phith0n/litter v1.5.3 h1:HJBgdlibJT57lmkT6XZPeWQCuDGKf9QkKHgnJJ98+io= +github.com/phith0n/litter v1.5.3/go.mod h1:T2hptLevnpc+lnUskkc/7yF6wvuej1A6fuMfP2uGbZ0= github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sanity-io/litter v1.5.1 h1:dwnrSypP6q56o3lFxTU+t2fwQ9A+U5qrXVO4Qg9KwVU= -github.com/sanity-io/litter v1.5.1/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=