MCPcopy Index your code
hub / github.com/google/go-github / stringifyValue

Function stringifyValue

github/strings.go:41–119  ·  view source on GitHub ↗

stringifyValue was heavily inspired by the goprotobuf library.

(w *bytes.Buffer, val reflect.Value)

Source from the content-addressed store, hash-verified

39// stringifyValue was heavily inspired by the goprotobuf library.
40
41func 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() {

Callers 1

StringifyFunction · 0.85

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…