MCPcopy Create free account
hub / github.com/diillson/chatcli / stringFromJSONArg

Function stringFromJSONArg

cli/plugins/args_extract.go:48–71  ·  view source on GitHub ↗

stringFromJSONArg pulls a string value out of a single JSON argument blob, handling both flat formats (`{"url":"…"}`) and the nested @coder envelope (`{"cmd":"…","args":{"url":"…"}}`).

(raw string, keys []string)

Source from the content-addressed store, hash-verified

46// blob, handling both flat formats (`{"url":"…"}`) and the nested @coder
47// envelope (`{"cmd":"…","args":{"url":"…"}}`).
48func stringFromJSONArg(raw string, keys []string) string {
49 raw = strings.TrimSpace(raw)
50 if raw == "" || (raw[0] != '{' && raw[0] != '[') {
51 return ""
52 }
53 var top map[string]json.RawMessage
54 if err := json.Unmarshal([]byte(raw), &top); err != nil {
55 return ""
56 }
57 // Look in top level.
58 if v := stringFromMap(top, keys); v != "" {
59 return v
60 }
61 // Then in nested "args" if present.
62 if argsField, ok := top["args"]; ok {
63 var inner map[string]json.RawMessage
64 if err := json.Unmarshal(argsField, &inner); err == nil {
65 if v := stringFromMap(inner, keys); v != "" {
66 return v
67 }
68 }
69 }
70 return ""
71}
72
73// stringFromMap returns the first non-empty string value found for any
74// of the given keys in the JSON map. Numbers and booleans are ignored

Callers 1

extractStringArgFunction · 0.85

Calls 1

stringFromMapFunction · 0.85

Tested by

no test coverage detected