master
Jordan Orelli 8 years ago
parent d3bc077768
commit a99e50e130

@ -4,11 +4,11 @@ package main
//go:generate go run ./gen/main.go ./dota
import (
"bytes"
"compress/bzip2"
"flag"
"fmt"
"io"
"bytes"
"os"
"reflect"
"runtime/pprof"
@ -108,45 +108,70 @@ 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()
func prettySlice(v reflect.Value) string {
if v.Type().Elem().Kind() == reflect.Uint8 {
l := v.Len()
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 {
fmt.Fprintf(os.Stderr, "don't know how to pretty-print value of type %v", reflect.TypeOf(m))
return
width := 0
parts := make([]string, 0, v.Len())
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
fmt.Fprintf(&buf, "%s ", v.Type())
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
fmt.Fprintf(&buf, "%s: %s ", field.Name, pretty(fv))
}
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:
fmt.Fprintf(&buf, "%s: %q ", field.Name, fv.String())
return fmt.Sprintf("%q", v.String())
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:
fmt.Fprintf(&buf, "%s: %t ", field.Name, fv.Bool())
return fmt.Sprintf("%t", v.Bool())
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() {

Loading…
Cancel
Save