8000 fix: Avoid allocation in the stringifyValue by alexandear · Pull Request #3292 · google/go-github · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: Avoid allocation in the stringifyValue #3292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions github/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Stringify(message interface{}) string {

func stringifyValue(w *bytes.Buffer, val reflect.Value) {
if val.Kind() == reflect.Ptr && val.IsNil() {
w.Write([]byte("<nil>"))
w.WriteString("<nil>")
return
}

Expand All @@ -37,20 +37,20 @@ func stringifyValue(w *bytes.Buffer, val reflect.Value) {
case reflect.String:
fmt.Fprintf(w, `"%s"`, v)
case reflect.Slice:
w.Write([]byte{'['})
w.WriteByte('[')
for i := 0; i < v.Len(); i++ {
if i > 0 {
w.Write([]byte{' '})
w.WriteByte(' ')
}

stringifyValue(w, v.Index(i))
}

w.Write([]byte{']'})
w.WriteByte(']')
return
case reflect.Struct:
if v.Type().Name() != "" {
w.Write([]byte(v.Type().String()))
w.WriteString(v.Type().String())
}

// special handling of Timestamp values
Expand All @@ -59,7 +59,7 @@ func stringifyValue(w *bytes.Buffer, val reflect.Value) {
return
}

w.Write([]byte{'{'})
w.WriteByte('{')

var sep bool
for i := 0; i < v.NumField(); i++ {
Expand All @@ -75,17 +75,17 @@ func stringifyValue(w *bytes.Buffer, val reflect.Value) {
}

if sep {
w.Write([]byte(", "))
w.WriteString(", ")
} else {
sep = true
}

w.Write([]byte(v.Type().Field(i).Name))
w.Write([]byte{':'})
w.WriteString(v.Type().Field(i).Name)
w.WriteByte(':')
stringifyValue(w, fv)
}

w.Write([]byte{'}'})
w.WriteByte('}')
default:
if v.CanInterface() {
fmt.Fprint(w, v.Interface())
Expand Down
Loading
0