Skip to content

Commit

Permalink
custom go struct dump
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Aug 15, 2021
1 parent a778194 commit e35b3a1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
12 changes: 12 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 1 addition & 10 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
}
Expand Down
54 changes: 54 additions & 0 deletions go-dumper.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit e35b3a1

Please sign in to comment.