2017-07-26 03:51:49 +00:00
|
|
|
package json
|
|
|
|
|
2018-03-15 17:29:26 +00:00
|
|
|
// AppendKey appends a new key to the output JSON.
|
2017-07-26 03:51:49 +00:00
|
|
|
func AppendKey(dst []byte, key string) []byte {
|
2018-03-28 18:49:41 +00:00
|
|
|
if len(dst) > 1 && dst[len(dst)-1] != '{' {
|
2017-07-26 03:51:49 +00:00
|
|
|
dst = append(dst, ',')
|
|
|
|
}
|
|
|
|
dst = AppendString(dst, key)
|
|
|
|
return append(dst, ':')
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:29:26 +00:00
|
|
|
// AppendError encodes the error string to json and appends
|
|
|
|
// the encoded string to the input byte slice.
|
2017-07-26 03:51:49 +00:00
|
|
|
func AppendError(dst []byte, err error) []byte {
|
|
|
|
if err == nil {
|
|
|
|
return append(dst, `null`...)
|
|
|
|
}
|
|
|
|
return AppendString(dst, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:29:26 +00:00
|
|
|
// AppendErrors encodes the error strings to json and
|
|
|
|
// appends the encoded string list to the input byte slice.
|
2017-07-26 03:51:49 +00:00
|
|
|
func AppendErrors(dst []byte, errs []error) []byte {
|
|
|
|
if len(errs) == 0 {
|
|
|
|
return append(dst, '[', ']')
|
|
|
|
}
|
|
|
|
dst = append(dst, '[')
|
|
|
|
if errs[0] != nil {
|
|
|
|
dst = AppendString(dst, errs[0].Error())
|
|
|
|
} else {
|
|
|
|
dst = append(dst, "null"...)
|
|
|
|
}
|
|
|
|
if len(errs) > 1 {
|
|
|
|
for _, err := range errs[1:] {
|
|
|
|
if err == nil {
|
|
|
|
dst = append(dst, ",null"...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dst = AppendString(append(dst, ','), err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst = append(dst, ']')
|
|
|
|
return dst
|
|
|
|
}
|