stringifyValue was heavily inspired by the goprotobuf library.
(w *bytes.Buffer, val reflect.Value)
| 39 | // stringifyValue was heavily inspired by the goprotobuf library. |
| 40 | |
| 41 | func stringifyValue(w *bytes.Buffer, val reflect.Value) { |
| 42 | if val.Kind() == reflect.Pointer && val.IsNil() { |
| 43 | w.WriteString("<nil>") |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | v := reflect.Indirect(val) |
| 48 | |
| 49 | switch v.Kind() { |
| 50 | case reflect.Bool: |
| 51 | w.Write(strconv.AppendBool(w.Bytes(), v.Bool())[w.Len():]) |
| 52 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 53 | w.Write(strconv.AppendInt(w.Bytes(), v.Int(), 10)[w.Len():]) |
| 54 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 55 | w.Write(strconv.AppendUint(w.Bytes(), v.Uint(), 10)[w.Len():]) |
| 56 | case reflect.Float32: |
| 57 | w.Write(strconv.AppendFloat(w.Bytes(), v.Float(), 'g', -1, 32)[w.Len():]) |
| 58 | case reflect.Float64: |
| 59 | w.Write(strconv.AppendFloat(w.Bytes(), v.Float(), 'g', -1, 64)[w.Len():]) |
| 60 | case reflect.String: |
| 61 | w.WriteByte('"') |
| 62 | w.WriteString(v.String()) |
| 63 | w.WriteByte('"') |
| 64 | case reflect.Slice: |
| 65 | w.WriteByte('[') |
| 66 | for i := range v.Len() { |
| 67 | if i > 0 { |
| 68 | w.WriteByte(' ') |
| 69 | } |
| 70 | |
| 71 | stringifyValue(w, v.Index(i)) |
| 72 | } |
| 73 | |
| 74 | w.WriteByte(']') |
| 75 | return |
| 76 | case reflect.Struct: |
| 77 | if v.Type().Name() != "" { |
| 78 | w.WriteString(v.Type().String()) |
| 79 | } |
| 80 | |
| 81 | // special handling of Timestamp values |
| 82 | if v.Type() == timestampType { |
| 83 | fmt.Fprintf(w, "{%v}", v.Interface()) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | w.WriteByte('{') |
| 88 | |
| 89 | var sep bool |
| 90 | for i := range v.NumField() { |
| 91 | fv := v.Field(i) |
| 92 | if fv.Kind() == reflect.Pointer && fv.IsNil() { |
| 93 | continue |
| 94 | } |
| 95 | if fv.Kind() == reflect.Slice && fv.IsNil() { |
| 96 | continue |
| 97 | } |
| 98 | if fv.Kind() == reflect.Map && fv.IsNil() { |