jsonpath/result.go

63 lines
903 B
Go
Raw Normal View History

2019-02-26 13:05:15 +04:00
package jsonpath
import (
"bytes"
"fmt"
)
const (
2019-10-19 01:41:57 +04:00
JSONObject = iota
JSONArray
JSONString
JSONNumber
JSONNull
JSONBool
2019-02-26 13:05:15 +04:00
)
type Result struct {
Keys []interface{}
Value []byte
Type int
}
func (r *Result) Pretty(showPath bool) string {
b := bytes.NewBufferString("")
printed := false
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
if showPath {
for _, k := range r.Keys {
switch v := k.(type) {
case int:
b.WriteString(fmt.Sprintf("%d", v))
default:
b.WriteString(fmt.Sprintf("%q", v))
}
b.WriteRune('\t')
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
printed = true
}
} else if r.Value == nil {
if len(r.Keys) > 0 {
printed = true
switch v := r.Keys[len(r.Keys)-1].(type) {
case int:
b.WriteString(fmt.Sprintf("%d", v))
default:
b.WriteString(fmt.Sprintf("%q", v))
}
}
}
if r.Value != nil {
printed = true
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
b.WriteString(fmt.Sprintf("%s", r.Value))
}
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
if printed {
b.WriteRune('\n')
}
2019-10-19 02:14:19 +04:00
2019-02-26 13:05:15 +04:00
return b.String()
}