MCPcopy
hub / github.com/mislav/hub / JSONPath

Function JSONPath

utils/json.go:32–104  ·  view source on GitHub ↗
(out io.Writer, src io.Reader, colorize bool)

Source from the content-addressed store, hash-verified

30}
31
32func JSONPath(out io.Writer, src io.Reader, colorize bool) (hasNextPage bool, endCursor string) {
33 dec := json.NewDecoder(src)
34 dec.UseNumber()
35
36 s := &state{}
37 postEmit := func() {
38 if s.isObject {
39 s.objectKey = ""
40 } else if s.isArray {
41 s.arrayIndex++
42 }
43 }
44
45 color := func(c string, t interface{}) string {
46 if colorize {
47 return fmt.Sprintf("\033[%sm%s\033[m", c, t)
48 } else if tt, ok := t.(string); ok {
49 return tt
50 } else {
51 return fmt.Sprintf("%s", t)
52 }
53 }
54
55 for {
56 token, err := dec.Token()
57 if err == io.EOF {
58 break
59 } else if err != nil {
60 panic(err)
61 }
62 if delim, ok := token.(json.Delim); ok {
63 switch delim {
64 case '{':
65 s = &state{isObject: true, parentState: s}
66 case '[':
67 s = &state{isArray: true, parentState: s}
68 case '}', ']':
69 s = s.parentState
70 postEmit()
71 default:
72 panic("unknown delim")
73 }
74 } else {
75 if s.isObject && s.objectKey == "" {
76 s.objectKey = token.(string)
77 } else {
78 k := stateKey(s)
79 fmt.Fprintf(out, "%s\t", color("0;36", k))
80
81 switch tt := token.(type) {
82 case string:
83 fmt.Fprintf(out, "%s\n", strings.Replace(tt, "\n", "\\n", -1))
84 if strings.HasSuffix(k, ".pageInfo.endCursor") {
85 endCursor = tt
86 }
87 case json.Number:
88 fmt.Fprintf(out, "%s\n", color("0;35", tt))
89 case nil:

Callers 1

apiCommandFunction · 0.92

Calls 2

stateKeyFunction · 0.85
ReplaceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…