2
0

json.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. package executor
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. )
  9. // jsonText is a JSON value produced by a JSON1 function. It carries the JSON
  10. // subtype across nested function calls (so json_insert can embed the result of
  11. // json() verbatim) and is normalized to a plain string before it is stored.
  12. type jsonText string
  13. // jsonEncode marshals a decoded JSON tree without HTML escaping.
  14. func jsonEncode(v interface{}) (string, error) {
  15. var buf bytes.Buffer
  16. enc := json.NewEncoder(&buf)
  17. enc.SetEscapeHTML(false)
  18. if err := enc.Encode(v); err != nil {
  19. return "", err
  20. }
  21. return strings.TrimRight(buf.String(), "\n"), nil
  22. }
  23. // jsonDecode parses JSON text, preserving number precision with json.Number.
  24. func jsonDecode(s string) (interface{}, error) {
  25. dec := json.NewDecoder(strings.NewReader(s))
  26. dec.UseNumber()
  27. var v interface{}
  28. if err := dec.Decode(&v); err != nil {
  29. return nil, fmt.Errorf("malformed JSON")
  30. }
  31. // Reject trailing content, matching JSON1's strict parsing.
  32. if dec.More() {
  33. return nil, fmt.Errorf("malformed JSON")
  34. }
  35. return v, nil
  36. }
  37. // jsonInput resolves a JSON1 input argument to a decoded tree.
  38. func jsonInput(v interface{}) (interface{}, error) {
  39. switch t := v.(type) {
  40. case nil:
  41. return nil, nil
  42. case jsonText:
  43. return jsonDecode(string(t))
  44. case string:
  45. return jsonDecode(t)
  46. case []byte:
  47. return jsonDecode(string(t))
  48. default:
  49. return nil, fmt.Errorf("malformed JSON")
  50. }
  51. }
  52. // jsonArgToNode converts a SQL value into a JSON tree node for embedding.
  53. func jsonArgToNode(v interface{}) interface{} {
  54. switch t := v.(type) {
  55. case nil:
  56. return nil
  57. case jsonText:
  58. if node, err := jsonDecode(string(t)); err == nil {
  59. return node
  60. }
  61. return string(t)
  62. case bool:
  63. return t
  64. case int:
  65. return json.Number(strconv.FormatInt(int64(t), 10))
  66. case int64:
  67. return json.Number(strconv.FormatInt(t, 10))
  68. case uint64:
  69. return json.Number(strconv.FormatUint(t, 10))
  70. case float64:
  71. return t
  72. case string:
  73. return t
  74. case []byte:
  75. return string(t)
  76. default:
  77. return fmt.Sprintf("%v", t)
  78. }
  79. }
  80. // jsonNodeToSQL converts a JSON tree node to the SQL value json_extract returns.
  81. func jsonNodeToSQL(v interface{}) interface{} {
  82. switch t := v.(type) {
  83. case nil:
  84. return nil
  85. case json.Number:
  86. if i, err := t.Int64(); err == nil {
  87. return i
  88. }
  89. f, _ := t.Float64()
  90. return f
  91. case bool:
  92. if t {
  93. return int64(1)
  94. }
  95. return int64(0)
  96. case string:
  97. return t
  98. case []interface{}, map[string]interface{}:
  99. s, err := jsonEncode(t)
  100. if err != nil {
  101. return nil
  102. }
  103. return jsonText(s)
  104. default:
  105. return t
  106. }
  107. }
  108. // jsonPathSeg is one component of a parsed JSON path.
  109. type jsonPathSeg struct {
  110. key string
  111. isIndex bool
  112. index int
  113. appendOp bool // [#] — append for insert/set
  114. fromEnd bool // negative index
  115. }
  116. // parseJSONPath parses a JSON1 path expression such as $.a.b[0] or $[#].
  117. func parseJSONPath(path string) ([]jsonPathSeg, error) {
  118. if !strings.HasPrefix(path, "$") {
  119. return nil, fmt.Errorf("JSON path error: %s", path)
  120. }
  121. var segs []jsonPathSeg
  122. i := 1
  123. for i < len(path) {
  124. switch path[i] {
  125. case '.':
  126. i++
  127. if i >= len(path) {
  128. return nil, fmt.Errorf("JSON path error: %s", path)
  129. }
  130. if path[i] == '"' {
  131. end := strings.IndexByte(path[i+1:], '"')
  132. if end < 0 {
  133. return nil, fmt.Errorf("JSON path error: %s", path)
  134. }
  135. segs = append(segs, jsonPathSeg{key: path[i+1 : i+1+end]})
  136. i += end + 2
  137. continue
  138. }
  139. start := i
  140. for i < len(path) && path[i] != '.' && path[i] != '[' {
  141. i++
  142. }
  143. segs = append(segs, jsonPathSeg{key: path[start:i]})
  144. case '[':
  145. end := strings.IndexByte(path[i:], ']')
  146. if end < 0 {
  147. return nil, fmt.Errorf("JSON path error: %s", path)
  148. }
  149. inner := path[i+1 : i+end]
  150. i += end + 1
  151. if inner == "#" {
  152. segs = append(segs, jsonPathSeg{isIndex: true, appendOp: true})
  153. continue
  154. }
  155. if strings.HasPrefix(inner, "\"") && strings.HasSuffix(inner, "\"") && len(inner) >= 2 {
  156. segs = append(segs, jsonPathSeg{key: inner[1 : len(inner)-1]})
  157. continue
  158. }
  159. n, err := strconv.Atoi(inner)
  160. if err != nil {
  161. return nil, fmt.Errorf("JSON path error: %s", path)
  162. }
  163. segs = append(segs, jsonPathSeg{isIndex: true, index: n, fromEnd: n < 0})
  164. default:
  165. return nil, fmt.Errorf("JSON path error: %s", path)
  166. }
  167. }
  168. return segs, nil
  169. }
  170. // jsonLookup walks a decoded tree to the node addressed by segs.
  171. func jsonLookup(root interface{}, segs []jsonPathSeg) (interface{}, bool) {
  172. cur := root
  173. for _, seg := range segs {
  174. if seg.isIndex {
  175. arr, ok := cur.([]interface{})
  176. if !ok {
  177. return nil, false
  178. }
  179. idx := seg.index
  180. if seg.fromEnd {
  181. idx = len(arr) + idx
  182. }
  183. if idx < 0 || idx >= len(arr) {
  184. return nil, false
  185. }
  186. cur = arr[idx]
  187. continue
  188. }
  189. obj, ok := cur.(map[string]interface{})
  190. if !ok {
  191. return nil, false
  192. }
  193. val, ok := obj[seg.key]
  194. if !ok {
  195. return nil, false
  196. }
  197. cur = val
  198. }
  199. return cur, true
  200. }
  201. // jsonApplyPatch applies json_set/insert/replace mutations to a decoded tree.
  202. // mode is "set", "insert", or "replace".
  203. func jsonApplyPatch(root interface{}, segs []jsonPathSeg, value interface{}, mode string) (interface{}, error) {
  204. if len(segs) == 0 {
  205. return value, nil
  206. }
  207. seg := segs[0]
  208. last := len(segs) == 1
  209. childSegs := segs[1:]
  210. if seg.isIndex {
  211. arr, ok := root.([]interface{})
  212. if !ok {
  213. // A missing container is created only by json_set/json_insert.
  214. if mode == "replace" {
  215. return root, nil
  216. }
  217. arr = []interface{}{}
  218. }
  219. idx := seg.index
  220. if seg.appendOp {
  221. if last {
  222. return append(arr, value), nil
  223. }
  224. child, childOK := interface{}(nil), false
  225. _ = child
  226. _ = childOK
  227. // append a new container for nested paths
  228. container := newJSONContainer(childSegs[0])
  229. newChild, err := jsonApplyPatch(container, childSegs, value, mode)
  230. if err != nil {
  231. return nil, err
  232. }
  233. return append(arr, newChild), nil
  234. }
  235. if seg.fromEnd {
  236. idx = len(arr) + idx
  237. }
  238. if idx < 0 || idx > len(arr) {
  239. return root, nil
  240. }
  241. if last {
  242. if idx == len(arr) {
  243. if mode == "replace" {
  244. return root, nil
  245. }
  246. return append(arr, value), nil
  247. }
  248. replaced := append([]interface{}(nil), arr...)
  249. replaced[idx] = value
  250. return replaced, nil
  251. }
  252. if idx == len(arr) {
  253. if mode == "replace" {
  254. return root, nil
  255. }
  256. container := newJSONContainer(childSegs[0])
  257. newChild, err := jsonApplyPatch(container, childSegs, value, mode)
  258. if err != nil {
  259. return nil, err
  260. }
  261. return append(arr, newChild), nil
  262. }
  263. newChild, err := jsonApplyPatch(arr[idx], childSegs, value, mode)
  264. if err != nil {
  265. return nil, err
  266. }
  267. replaced := append([]interface{}(nil), arr...)
  268. replaced[idx] = newChild
  269. return replaced, nil
  270. }
  271. obj, ok := root.(map[string]interface{})
  272. if !ok {
  273. if mode == "replace" {
  274. return root, nil
  275. }
  276. obj = map[string]interface{}{}
  277. }
  278. if last {
  279. _, exists := obj[seg.key]
  280. if exists && mode == "insert" {
  281. return root, nil
  282. }
  283. if !exists && mode == "replace" {
  284. return root, nil
  285. }
  286. newObj := make(map[string]interface{}, len(obj)+1)
  287. for k, v := range obj {
  288. newObj[k] = v
  289. }
  290. newObj[seg.key] = value
  291. return newObj, nil
  292. }
  293. child, exists := obj[seg.key]
  294. if !exists {
  295. if mode == "replace" {
  296. return root, nil
  297. }
  298. child = newJSONContainer(childSegs[0])
  299. }
  300. newChild, err := jsonApplyPatch(child, childSegs, value, mode)
  301. if err != nil {
  302. return nil, err
  303. }
  304. newObj := make(map[string]interface{}, len(obj)+1)
  305. for k, v := range obj {
  306. newObj[k] = v
  307. }
  308. newObj[seg.key] = newChild
  309. return newObj, nil
  310. }
  311. func newJSONContainer(seg jsonPathSeg) interface{} {
  312. if seg.isIndex {
  313. return []interface{}{}
  314. }
  315. return map[string]interface{}{}
  316. }
  317. // jsonTypeName returns the JSON1 type name for a decoded node.
  318. func jsonTypeName(v interface{}) string {
  319. switch v.(type) {
  320. case nil:
  321. return "null"
  322. case bool:
  323. return "false" // caller distinguishes true below
  324. case json.Number:
  325. s := string(v.(json.Number))
  326. if !strings.ContainsAny(s, ".eE") {
  327. return "integer"
  328. }
  329. return "real"
  330. case string:
  331. return "text"
  332. case []interface{}:
  333. return "array"
  334. case map[string]interface{}:
  335. return "object"
  336. default:
  337. return "null"
  338. }
  339. }
  340. // evalJSONFunction evaluates a JSON1 scalar function. It returns handled=false
  341. // for names it does not own.
  342. func evalJSONFunction(name string, args []interface{}) (interface{}, bool, error) {
  343. upper := strings.ToUpper(name)
  344. switch upper {
  345. case "JSON", "JSONB":
  346. if len(args) != 1 {
  347. return nil, true, fmt.Errorf("wrong number of arguments to %s()", name)
  348. }
  349. if args[0] == nil {
  350. return nil, true, nil
  351. }
  352. node, err := jsonInput(args[0])
  353. if err != nil {
  354. return nil, true, err
  355. }
  356. s, err := jsonEncode(node)
  357. if err != nil {
  358. return nil, true, err
  359. }
  360. return jsonText(s), true, nil
  361. case "JSON_VALID", "JSONB_VALID":
  362. if len(args) != 1 {
  363. return nil, true, fmt.Errorf("wrong number of arguments to %s()", name)
  364. }
  365. if _, err := jsonInput(args[0]); err != nil {
  366. return int64(0), true, nil
  367. }
  368. return int64(1), true, nil
  369. case "JSON_TYPE":
  370. if len(args) < 1 || len(args) > 2 {
  371. return nil, true, fmt.Errorf("wrong number of arguments to json_type()")
  372. }
  373. if args[0] == nil {
  374. return nil, true, nil
  375. }
  376. node, err := jsonInput(args[0])
  377. if err != nil {
  378. return nil, true, err
  379. }
  380. if len(args) == 2 {
  381. path, ok := args[1].(string)
  382. if !ok {
  383. if jt, is := args[1].(jsonText); is {
  384. path = string(jt)
  385. } else {
  386. return nil, true, fmt.Errorf("JSON path error")
  387. }
  388. }
  389. segs, perr := parseJSONPath(path)
  390. if perr != nil {
  391. return nil, true, perr
  392. }
  393. found, ok := jsonLookup(node, segs)
  394. if !ok {
  395. return nil, true, nil
  396. }
  397. node = found
  398. }
  399. if b, isBool := node.(bool); isBool {
  400. if b {
  401. return "true", true, nil
  402. }
  403. return "false", true, nil
  404. }
  405. return jsonTypeName(node), true, nil
  406. case "JSON_EXTRACT", "JSONB_EXTRACT":
  407. if len(args) < 2 {
  408. return nil, true, fmt.Errorf("wrong number of arguments to json_extract()")
  409. }
  410. if args[0] == nil {
  411. return nil, true, nil
  412. }
  413. node, err := jsonInput(args[0])
  414. if err != nil {
  415. return nil, true, err
  416. }
  417. for _, p := range args[1:] {
  418. path, ok := jsonPathArg(p)
  419. if !ok {
  420. return nil, true, fmt.Errorf("JSON path error")
  421. }
  422. segs, perr := parseJSONPath(path)
  423. if perr != nil {
  424. return nil, true, perr
  425. }
  426. found, ok := jsonLookup(node, segs)
  427. if !ok {
  428. return nil, true, nil
  429. }
  430. node = found
  431. }
  432. return jsonNodeToSQL(node), true, nil
  433. case "JSON_SET", "JSONB_SET", "JSON_INSERT", "JSONB_INSERT", "JSON_REPLACE", "JSONB_REPLACE":
  434. if len(args) < 3 || len(args)%2 == 0 {
  435. return nil, true, fmt.Errorf("wrong number of arguments to %s()", name)
  436. }
  437. if args[0] == nil {
  438. return nil, true, nil
  439. }
  440. mode := "set"
  441. if strings.Contains(upper, "INSERT") {
  442. mode = "insert"
  443. } else if strings.Contains(upper, "REPLACE") {
  444. mode = "replace"
  445. }
  446. node, err := jsonInput(args[0])
  447. if err != nil {
  448. return nil, true, err
  449. }
  450. for i := 1; i+1 < len(args); i += 2 {
  451. path, ok := jsonPathArg(args[i])
  452. if !ok {
  453. return nil, true, fmt.Errorf("JSON path error")
  454. }
  455. segs, perr := parseJSONPath(path)
  456. if perr != nil {
  457. return nil, true, perr
  458. }
  459. node, err = jsonApplyPatch(node, segs, jsonArgToNode(args[i+1]), mode)
  460. if err != nil {
  461. return nil, true, err
  462. }
  463. }
  464. s, err := jsonEncode(node)
  465. if err != nil {
  466. return nil, true, err
  467. }
  468. return jsonText(s), true, nil
  469. case "JSON_REMOVE", "JSONB_REMOVE":
  470. if len(args) < 2 {
  471. return nil, true, fmt.Errorf("wrong number of arguments to json_remove()")
  472. }
  473. if args[0] == nil {
  474. return nil, true, nil
  475. }
  476. node, err := jsonInput(args[0])
  477. if err != nil {
  478. return nil, true, err
  479. }
  480. for _, p := range args[1:] {
  481. path, ok := jsonPathArg(p)
  482. if !ok {
  483. return nil, true, fmt.Errorf("JSON path error")
  484. }
  485. segs, perr := parseJSONPath(path)
  486. if perr != nil {
  487. return nil, true, perr
  488. }
  489. node, err = jsonRemove(node, segs)
  490. if err != nil {
  491. return nil, true, err
  492. }
  493. }
  494. s, err := jsonEncode(node)
  495. if err != nil {
  496. return nil, true, err
  497. }
  498. return jsonText(s), true, nil
  499. case "JSON_ARRAY", "JSONB_ARRAY":
  500. arr := make([]interface{}, len(args))
  501. for i, a := range args {
  502. arr[i] = jsonArgToNode(a)
  503. }
  504. s, err := jsonEncode(arr)
  505. if err != nil {
  506. return nil, true, err
  507. }
  508. return jsonText(s), true, nil
  509. case "JSON_OBJECT", "JSONB_OBJECT":
  510. if len(args)%2 != 0 {
  511. return nil, true, fmt.Errorf("json_object() requires an even number of arguments")
  512. }
  513. obj := make(map[string]interface{}, len(args)/2)
  514. for i := 0; i+1 < len(args); i += 2 {
  515. key, ok := args[i].(string)
  516. if !ok {
  517. if jt, is := args[i].(jsonText); is {
  518. key = string(jt)
  519. } else {
  520. return nil, true, fmt.Errorf("json_object() labels must be TEXT")
  521. }
  522. }
  523. obj[key] = jsonArgToNode(args[i+1])
  524. }
  525. s, err := jsonEncode(obj)
  526. if err != nil {
  527. return nil, true, err
  528. }
  529. return jsonText(s), true, nil
  530. case "JSON_QUOTE", "JSONB_QUOTE":
  531. if len(args) != 1 {
  532. return nil, true, fmt.Errorf("json_quote() requires exactly one argument")
  533. }
  534. s, err := jsonEncode(jsonArgToNode(args[0]))
  535. if err != nil {
  536. return nil, true, err
  537. }
  538. return jsonText(s), true, nil
  539. }
  540. return nil, false, nil
  541. }
  542. // jsonPathArg extracts a path string from an argument.
  543. func jsonPathArg(v interface{}) (string, bool) {
  544. switch t := v.(type) {
  545. case string:
  546. return t, true
  547. case jsonText:
  548. return string(t), true
  549. default:
  550. return "", false
  551. }
  552. }
  553. // jsonRemove removes the node addressed by segs from a decoded tree.
  554. func jsonRemove(root interface{}, segs []jsonPathSeg) (interface{}, error) {
  555. if len(segs) == 0 {
  556. return root, nil
  557. }
  558. seg := segs[0]
  559. last := len(segs) == 1
  560. if seg.isIndex {
  561. arr, ok := root.([]interface{})
  562. if !ok {
  563. return root, nil
  564. }
  565. idx := seg.index
  566. if seg.fromEnd {
  567. idx = len(arr) + idx
  568. }
  569. if idx < 0 || idx >= len(arr) {
  570. return root, nil
  571. }
  572. if last {
  573. out := make([]interface{}, 0, len(arr)-1)
  574. out = append(out, arr[:idx]...)
  575. out = append(out, arr[idx+1:]...)
  576. return out, nil
  577. }
  578. child, err := jsonRemove(arr[idx], segs[1:])
  579. if err != nil {
  580. return nil, err
  581. }
  582. out := append([]interface{}(nil), arr...)
  583. out[idx] = child
  584. return out, nil
  585. }
  586. obj, ok := root.(map[string]interface{})
  587. if !ok {
  588. return root, nil
  589. }
  590. if last {
  591. out := make(map[string]interface{}, len(obj))
  592. for k, v := range obj {
  593. if k != seg.key {
  594. out[k] = v
  595. }
  596. }
  597. return out, nil
  598. }
  599. child, exists := obj[seg.key]
  600. if !exists {
  601. return root, nil
  602. }
  603. newChild, err := jsonRemove(child, segs[1:])
  604. if err != nil {
  605. return nil, err
  606. }
  607. out := make(map[string]interface{}, len(obj))
  608. for k, v := range obj {
  609. out[k] = v
  610. }
  611. out[seg.key] = newChild
  612. return out, nil
  613. }