master
Jordan Orelli 8 years ago
parent d3bc077768
commit a99e50e130

@ -4,11 +4,11 @@ package main
//go:generate go run ./gen/main.go ./dota //go:generate go run ./gen/main.go ./dota
import ( import (
"bytes"
"compress/bzip2" "compress/bzip2"
"flag" "flag"
"fmt" "fmt"
"io" "io"
"bytes"
"os" "os"
"reflect" "reflect"
"runtime/pprof" "runtime/pprof"
@ -108,45 +108,70 @@ func printTypes(m proto.Message) {
fmt.Println(reflect.TypeOf(m)) fmt.Println(reflect.TypeOf(m))
} }
func prettyVals(m proto.Message) { func prettySlice(v reflect.Value) string {
v := reflect.ValueOf(m) if v.Type().Elem().Kind() == reflect.Uint8 {
if v.Kind() == reflect.Ptr { l := v.Len()
v = v.Elem() if l > 16 {
l = 16
}
b := make([]byte, l)
for i := 0; i < l; i++ {
b[i] = byte(v.Index(i).Uint())
}
return fmt.Sprintf("%x", b)
} }
if v.Kind() != reflect.Struct { width := 0
fmt.Fprintf(os.Stderr, "don't know how to pretty-print value of type %v", reflect.TypeOf(m)) parts := make([]string, 0, v.Len())
return for i := 0; i < v.Len() && width <= 32; i++ {
parts = append(parts, pretty(v.Index(i)))
width += len(parts[i]) // obligatory byte count is not rune count rabble
}
return fmt.Sprintf("[%s]", strings.Join(parts, ", "))
} }
func prettyStruct(v reflect.Value) string {
var buf bytes.Buffer var buf bytes.Buffer
fmt.Fprintf(&buf, "%s ", v.Type()) fmt.Fprintf(&buf, "{%s ", v.Type())
for fn := 0; fn < v.NumField(); fn++ { for fn := 0; fn < v.NumField(); fn++ {
field := v.Type().Field(fn) field := v.Type().Field(fn)
if field.Name == "XXX_unrecognized" { if field.Name == "XXX_unrecognized" {
continue continue
} }
fv := v.Field(fn) fv := v.Field(fn)
if fv.Kind() == reflect.Ptr { fmt.Fprintf(&buf, "%s: %s ", field.Name, pretty(fv))
if fv.IsNil() {
fmt.Fprintf(&buf, "%s: nil ", field.Name)
continue
} }
fv = fv.Elem() fmt.Fprint(&buf, "}")
return buf.String()
}
func pretty(v reflect.Value) string {
switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
return "nil"
} }
switch fv.Kind() { return pretty(v.Elem())
case reflect.Struct:
return prettyStruct(v)
case reflect.Slice:
return prettySlice(v)
case reflect.String: case reflect.String:
fmt.Fprintf(&buf, "%s: %q ", field.Name, fv.String()) return fmt.Sprintf("%q", v.String())
case reflect.Int32: case reflect.Int32:
fmt.Fprintf(&buf, "%s: %d ", field.Name, fv.Int()) return fmt.Sprintf("%d", v.Int())
case reflect.Uint8, reflect.Uint32:
return fmt.Sprintf("%d", v.Uint())
case reflect.Bool: case reflect.Bool:
fmt.Fprintf(&buf, "%s: %t ", field.Name, fv.Bool()) return fmt.Sprintf("%t", v.Bool())
default: default:
fmt.Fprintf(&buf, "%s: %s ", field.Name, fv.Type()) return v.Type().Name()
} }
} }
fmt.Printf("{%s}\n", buf.String())
func prettyVals(m proto.Message) {
v := reflect.ValueOf(m)
fmt.Println(pretty(v))
} }
func main() { func main() {

Loading…
Cancel
Save