From d3bc0777685c90646cb512e238dc8e88db3be5f3 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 7 Aug 2016 20:32:45 -0600 Subject: [PATCH] pretty --- main.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/main.go b/main.go index c6439f9..e981f70 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,13 @@ import ( "flag" "fmt" "io" + "bytes" "os" + "reflect" "runtime/pprof" "strings" + + "github.com/golang/protobuf/proto" ) const ( @@ -98,6 +102,53 @@ func cpuprofile(dest string) func() { } } +func check(msg proto.Message) {} + +func printTypes(m proto.Message) { + fmt.Println(reflect.TypeOf(m)) +} + +func prettyVals(m proto.Message) { + v := reflect.ValueOf(m) + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + + if v.Kind() != reflect.Struct { + fmt.Fprintf(os.Stderr, "don't know how to pretty-print value of type %v", reflect.TypeOf(m)) + return + } + + var buf bytes.Buffer + fmt.Fprintf(&buf, "%s ", v.Type()) + + for fn := 0; fn < v.NumField(); fn++ { + field := v.Type().Field(fn) + if field.Name == "XXX_unrecognized" { + continue + } + fv := v.Field(fn) + if fv.Kind() == reflect.Ptr { + if fv.IsNil() { + fmt.Fprintf(&buf, "%s: nil ", field.Name) + continue + } + fv = fv.Elem() + } + switch fv.Kind() { + case reflect.String: + fmt.Fprintf(&buf, "%s: %q ", field.Name, fv.String()) + case reflect.Int32: + fmt.Fprintf(&buf, "%s: %d ", field.Name, fv.Int()) + case reflect.Bool: + fmt.Fprintf(&buf, "%s: %t ", field.Name, fv.Bool()) + default: + fmt.Fprintf(&buf, "%s: %s ", field.Name, fv.Type()) + } + } + fmt.Printf("{%s}\n", buf.String()) +} + func main() { var opts options flag.BoolVar(&opts.b, "b", false, "input is expected to be bzip-compressed") @@ -107,6 +158,18 @@ func main() { flag.StringVar(&opts.cpuprofile, "cpuprofile", "", "cpu profile destination") flag.Parse() + var handle func(proto.Message) + switch flag.Arg(0) { + case "": + handle = check + case "types": + handle = printTypes + case "pretty": + handle = prettyVals + default: + bail(1, "no such action: %s", flag.Arg(0)) + } + if opts.memprofile != "" { defer memprofile(opts.memprofile) } @@ -125,11 +188,14 @@ func main() { delete(p.ewl, EBaseGameEvents_GE_SosStartSoundEvent) delete(p.ewl, EDotaUserMessages_DOTA_UM_SpectatorPlayerUnitOrders) delete(p.ewl, EDotaUserMessages_DOTA_UM_SpectatorPlayerClick) + delete(p.ewl, EDotaUserMessages_DOTA_UM_TE_UnitAnimation) + delete(p.ewl, EDotaUserMessages_DOTA_UM_TE_UnitAnimationEnd) go p.run(c) for m := range c { if m.error != nil { fmt.Fprintln(os.Stderr, m.error) } else { + handle(m.Message) messages.Return(m.Message) } }