Skip to content

Commit

Permalink
perf: use strings.Builder in GoString
Browse files Browse the repository at this point in the history
  • Loading branch information
huzedong1 committed Mar 15, 2023
1 parent ea2d5ad commit e8d5a6f
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,23 @@ func (ve *ValidationError) Error() string {
return fmt.Sprintf("jsonschema: %s does not validate with %s: %s", quote(leaf.InstanceLocation), u+"#"+leaf.KeywordLocation, leaf.Message)
}

func (ve *ValidationError) GoString() string {
func (ve *ValidationError) goStringTo(tgt *strings.Builder, indent int) {
sloc := ve.AbsoluteKeywordLocation
sloc = sloc[strings.IndexByte(sloc, '#')+1:]
msg := fmt.Sprintf("[I#%s] [S#%s] %s", ve.InstanceLocation, sloc, ve.Message)
for i := 0; i < indent; i++ {
tgt.WriteByte(' ')
}
tgt.WriteString(fmt.Sprintf("[I#%s] [S#%s] %s", ve.InstanceLocation, sloc, ve.Message))
for _, c := range ve.Causes {
for _, line := range strings.Split(c.GoString(), "\n") {
msg += "\n " + line
}
tgt.WriteByte('\n')
c.goStringTo(tgt, indent+2)
}
return msg
}

func (ve *ValidationError) GoString() string {
var msgBuf strings.Builder
ve.goStringTo(&msgBuf, 0)
return msgBuf.String()
}

func joinPtr(ptr1, ptr2 string) string {
Expand Down

0 comments on commit e8d5a6f

Please sign in to comment.