sjson.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // Package sjson provides setting json values.
  2. package yu_json
  3. import (
  4. jsongo "encoding/json"
  5. yu_fast "gogs.qqck.cn/s/tools/fast"
  6. yu_strconv "gogs.qqck.cn/s/tools/strconv"
  7. "sort"
  8. "strconv"
  9. )
  10. type errorType struct {
  11. msg string
  12. }
  13. func (err *errorType) Error() string {
  14. return err.msg
  15. }
  16. // Options represents additional options for the Set and Delete functions.
  17. type Options struct {
  18. // Optimistic is a hint that the value likely exists which
  19. // allows for the sjson to perform a fast-track search and replace.
  20. Optimistic bool
  21. // ReplaceInPlace is a hint to replace the input json rather than
  22. // allocate a new json byte slice. When this field is specified
  23. // the input json will not longer be valid and it should not be used
  24. // In the case when the destination slice doesn't have enough free
  25. // bytes to replace the data in place, a new bytes slice will be
  26. // created under the hood.
  27. // The Optimistic flag must be set to true and the input must be a
  28. // byte slice in order to use this field.
  29. ReplaceInPlace bool
  30. }
  31. type pathResult struct {
  32. part string // current key part
  33. gpart string // gjson get part
  34. path string // remaining path
  35. force bool // force a string key
  36. more bool // there is more path to parse
  37. }
  38. func isSimpleChar(ch byte) bool {
  39. switch ch {
  40. case '|', '#', '@', '*', '?':
  41. return false
  42. default:
  43. return true
  44. }
  45. }
  46. func parsePath(path string) (res pathResult, simple bool) {
  47. var r pathResult
  48. if len(path) > 0 && path[0] == ':' {
  49. r.force = true
  50. path = path[1:]
  51. }
  52. for i := 0; i < len(path); i++ {
  53. if path[i] == '.' {
  54. r.part = path[:i]
  55. r.gpart = path[:i]
  56. r.path = path[i+1:]
  57. r.more = true
  58. return r, true
  59. }
  60. if !isSimpleChar(path[i]) {
  61. return r, false
  62. }
  63. if path[i] == '\\' {
  64. // go into escape mode. this is a slower path that
  65. // strips off the escape character from the part.
  66. epart := []byte(path[:i])
  67. gpart := []byte(path[:i+1])
  68. i++
  69. if i < len(path) {
  70. epart = append(epart, path[i])
  71. gpart = append(gpart, path[i])
  72. i++
  73. for ; i < len(path); i++ {
  74. if path[i] == '\\' {
  75. gpart = append(gpart, '\\')
  76. i++
  77. if i < len(path) {
  78. epart = append(epart, path[i])
  79. gpart = append(gpart, path[i])
  80. }
  81. continue
  82. } else if path[i] == '.' {
  83. r.part = string(epart)
  84. r.gpart = string(gpart)
  85. r.path = path[i+1:]
  86. r.more = true
  87. return r, true
  88. } else if !isSimpleChar(path[i]) {
  89. return r, false
  90. }
  91. epart = append(epart, path[i])
  92. gpart = append(gpart, path[i])
  93. }
  94. }
  95. // append the last part
  96. r.part = string(epart)
  97. r.gpart = string(gpart)
  98. return r, true
  99. }
  100. }
  101. r.part = path
  102. r.gpart = path
  103. return r, true
  104. }
  105. func mustMarshalString(s string) bool {
  106. for i := 0; i < len(s); i++ {
  107. if s[i] < ' ' || s[i] > 0x7f || s[i] == '"' || s[i] == '\\' {
  108. return true
  109. }
  110. }
  111. return false
  112. }
  113. // appendStringify makes a json string and appends to buf.
  114. func appendStringify(buf []byte, s string) []byte {
  115. if mustMarshalString(s) {
  116. b, _ := jsongo.Marshal(s)
  117. return append(buf, b...)
  118. }
  119. buf = append(buf, '"')
  120. buf = append(buf, s...)
  121. buf = append(buf, '"')
  122. return buf
  123. }
  124. // appendBuild builds a json block from a json path.
  125. func appendBuild(buf []byte, array bool, paths []pathResult, raw string,
  126. stringify bool) []byte {
  127. if !array {
  128. buf = appendStringify(buf, paths[0].part)
  129. buf = append(buf, ':')
  130. }
  131. if len(paths) > 1 {
  132. n, numeric := atoui(paths[1])
  133. if numeric || (!paths[1].force && paths[1].part == "-1") {
  134. buf = append(buf, '[')
  135. buf = appendRepeat(buf, "null,", n)
  136. buf = appendBuild(buf, true, paths[1:], raw, stringify)
  137. buf = append(buf, ']')
  138. } else {
  139. buf = append(buf, '{')
  140. buf = appendBuild(buf, false, paths[1:], raw, stringify)
  141. buf = append(buf, '}')
  142. }
  143. } else {
  144. if stringify {
  145. buf = appendStringify(buf, raw)
  146. } else {
  147. buf = append(buf, raw...)
  148. }
  149. }
  150. return buf
  151. }
  152. // atoui does a rip conversion of string -> unigned int.
  153. func atoui(r pathResult) (n int, ok bool) {
  154. if r.force {
  155. return 0, false
  156. }
  157. for i := 0; i < len(r.part); i++ {
  158. if r.part[i] < '0' || r.part[i] > '9' {
  159. return 0, false
  160. }
  161. n = n*10 + int(r.part[i]-'0')
  162. }
  163. return n, true
  164. }
  165. // appendRepeat repeats string "n" times and appends to buf.
  166. func appendRepeat(buf []byte, s string, n int) []byte {
  167. for i := 0; i < n; i++ {
  168. buf = append(buf, s...)
  169. }
  170. return buf
  171. }
  172. // deleteTailItem deletes the previous key or comma.
  173. func deleteTailItem(buf []byte) ([]byte, bool) {
  174. loop:
  175. for i := len(buf) - 1; i >= 0; i-- {
  176. // look for either a ',',':','['
  177. switch buf[i] {
  178. case '[':
  179. return buf, true
  180. case ',':
  181. return buf[:i], false
  182. case ':':
  183. // delete tail string
  184. i--
  185. for ; i >= 0; i-- {
  186. if buf[i] == '"' {
  187. i--
  188. for ; i >= 0; i-- {
  189. if buf[i] == '"' {
  190. i--
  191. if i >= 0 && buf[i] == '\\' {
  192. i--
  193. continue
  194. }
  195. for ; i >= 0; i-- {
  196. // look for either a ',','{'
  197. switch buf[i] {
  198. case '{':
  199. return buf[:i+1], true
  200. case ',':
  201. return buf[:i], false
  202. }
  203. }
  204. }
  205. }
  206. break
  207. }
  208. }
  209. break loop
  210. }
  211. }
  212. return buf, false
  213. }
  214. var errNoChange = &errorType{"no change"}
  215. func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string, stringify, del bool) ([]byte, error) {
  216. var err error
  217. var res Result
  218. var found bool
  219. if del {
  220. if paths[0].part == "-1" && !paths[0].force {
  221. res = Get(jstr, "#")
  222. if res.Int() > 0 {
  223. res = Get(jstr, yu_strconv.FormatInt64(res.Int()-1))
  224. found = true
  225. }
  226. }
  227. }
  228. if !found {
  229. res = Get(jstr, paths[0].gpart)
  230. }
  231. if res.Index > 0 {
  232. if len(paths) > 1 {
  233. buf = append(buf, jstr[:res.Index]...)
  234. buf, err = appendRawPaths(buf, res.Raw, paths[1:], raw, stringify, del)
  235. if err != nil {
  236. return nil, err
  237. }
  238. buf = append(buf, jstr[res.Index+len(res.Raw):]...)
  239. return buf, nil
  240. }
  241. buf = append(buf, jstr[:res.Index]...)
  242. var exidx int // additional forward stripping
  243. if del {
  244. var delNextComma bool
  245. buf, delNextComma = deleteTailItem(buf)
  246. if delNextComma {
  247. i, j := res.Index+len(res.Raw), 0
  248. for ; i < len(jstr); i, j = i+1, j+1 {
  249. if jstr[i] <= ' ' {
  250. continue
  251. }
  252. if jstr[i] == ',' {
  253. exidx = j + 1
  254. }
  255. break
  256. }
  257. }
  258. } else {
  259. if stringify {
  260. buf = appendStringify(buf, raw)
  261. } else {
  262. buf = append(buf, raw...)
  263. }
  264. }
  265. buf = append(buf, jstr[res.Index+len(res.Raw)+exidx:]...)
  266. return buf, nil
  267. }
  268. if del {
  269. return nil, errNoChange
  270. }
  271. n, numeric := atoui(paths[0])
  272. isempty := true
  273. for i := 0; i < len(jstr); i++ {
  274. if jstr[i] > ' ' {
  275. isempty = false
  276. break
  277. }
  278. }
  279. if isempty {
  280. if numeric {
  281. jstr = "[]"
  282. } else {
  283. jstr = "{}"
  284. }
  285. }
  286. jsres := Parse(jstr)
  287. if jsres.Type != JSON {
  288. if numeric {
  289. jstr = "[]"
  290. } else {
  291. jstr = "{}"
  292. }
  293. jsres = Parse(jstr)
  294. }
  295. var comma bool
  296. for i := 1; i < len(jsres.Raw); i++ {
  297. if jsres.Raw[i] <= ' ' {
  298. continue
  299. }
  300. if jsres.Raw[i] == '}' || jsres.Raw[i] == ']' {
  301. break
  302. }
  303. comma = true
  304. break
  305. }
  306. switch jsres.Raw[0] {
  307. default:
  308. return nil, &errorType{"json must be an object or array"}
  309. case '{':
  310. end := len(jsres.Raw) - 1
  311. for ; end > 0; end-- {
  312. if jsres.Raw[end] == '}' {
  313. break
  314. }
  315. }
  316. buf = append(buf, jsres.Raw[:end]...)
  317. if comma {
  318. buf = append(buf, ',')
  319. }
  320. buf = appendBuild(buf, false, paths, raw, stringify)
  321. buf = append(buf, '}')
  322. return buf, nil
  323. case '[':
  324. var appendit bool
  325. if !numeric {
  326. if paths[0].part == "-1" && !paths[0].force {
  327. appendit = true
  328. } else {
  329. return nil, &errorType{
  330. "cannot set array element for non-numeric key '" +
  331. paths[0].part + "'"}
  332. }
  333. }
  334. if appendit {
  335. njson := trim(jsres.Raw)
  336. if njson[len(njson)-1] == ']' {
  337. njson = njson[:len(njson)-1]
  338. }
  339. buf = append(buf, njson...)
  340. if comma {
  341. buf = append(buf, ',')
  342. }
  343. buf = appendBuild(buf, true, paths, raw, stringify)
  344. buf = append(buf, ']')
  345. return buf, nil
  346. }
  347. buf = append(buf, '[')
  348. ress := jsres.Array()
  349. for i := 0; i < len(ress); i++ {
  350. if i > 0 {
  351. buf = append(buf, ',')
  352. }
  353. buf = append(buf, ress[i].Raw...)
  354. }
  355. if len(ress) == 0 {
  356. buf = appendRepeat(buf, "null,", n-len(ress))
  357. } else {
  358. buf = appendRepeat(buf, ",null", n-len(ress))
  359. if comma {
  360. buf = append(buf, ',')
  361. }
  362. }
  363. buf = appendBuild(buf, true, paths, raw, stringify)
  364. buf = append(buf, ']')
  365. return buf, nil
  366. }
  367. }
  368. func isOptimisticPath(path string) bool {
  369. for i := 0; i < len(path); i++ {
  370. if path[i] < '.' || path[i] > 'z' {
  371. return false
  372. }
  373. if path[i] > '9' && path[i] < 'A' {
  374. return false
  375. }
  376. if path[i] > 'z' {
  377. return false
  378. }
  379. }
  380. return true
  381. }
  382. // Set sets a json value for the specified path.
  383. // A path is in dot syntax, such as "name.last" or "age".
  384. // This function expects that the json is well-formed, and does not validate.
  385. // Invalid json will not panic, but it may return back unexpected results.
  386. // An error is returned if the path is not valid.
  387. //
  388. // A path is a series of keys separated by a dot.
  389. //
  390. // {
  391. // "name": {"first": "Tom", "last": "Anderson"},
  392. // "age":37,
  393. // "children": ["Sara","Alex","Jack"],
  394. // "friends": [
  395. // {"first": "James", "last": "Murphy"},
  396. // {"first": "Roger", "last": "Craig"}
  397. // ]
  398. // }
  399. // "name.last" >> "Anderson"
  400. // "age" >> 37
  401. // "children.1" >> "Alex"
  402. func Set(json, path string, value interface{}) (v string) {
  403. v, _ = SetOptions(json, path, value, nil)
  404. return
  405. }
  406. // SetErr sets a json value for the specified path.
  407. // A path is in dot syntax, such as "name.last" or "age".
  408. // This function expects that the json is well-formed, and does not validate.
  409. // Invalid json will not panic, but it may return back unexpected results.
  410. // An error is returned if the path is not valid.
  411. //
  412. // A path is a series of keys separated by a dot.
  413. //
  414. // {
  415. // "name": {"first": "Tom", "last": "Anderson"},
  416. // "age":37,
  417. // "children": ["Sara","Alex","Jack"],
  418. // "friends": [
  419. // {"first": "James", "last": "Murphy"},
  420. // {"first": "Roger", "last": "Craig"}
  421. // ]
  422. // }
  423. // "name.last" >> "Anderson"
  424. // "age" >> 37
  425. // "children.1" >> "Alex"
  426. func SetErr(json, path string, value interface{}) (string, error) {
  427. return SetOptions(json, path, value, nil)
  428. }
  429. // SetBytes sets a json value for the specified path.
  430. func SetBytes(json []byte, path string, value interface{}) (v []byte) {
  431. v, _ = SetBytesOptions(json, path, value, nil)
  432. return
  433. }
  434. // SetBytesErr sets a json value for the specified path.
  435. func SetBytesErr(json []byte, path string, value interface{}) ([]byte, error) {
  436. return SetBytesOptions(json, path, value, nil)
  437. }
  438. // SetRaw sets a raw json value for the specified path.
  439. // This function works the same as Set except that the value is set as a
  440. // raw block of json. This allows for setting premarshalled json objects.
  441. func SetRaw(json, path, value string) (v string) {
  442. v, _ = SetRawOptions(json, path, value, nil)
  443. return
  444. }
  445. // SetRawErr sets a raw json value for the specified path.
  446. // This function works the same as Set except that the value is set as a
  447. // raw block of json. This allows for setting premarshalled json objects.
  448. func SetRawErr(json, path, value string) (string, error) {
  449. return SetRawOptions(json, path, value, nil)
  450. }
  451. // SetRawOptions sets a raw json value for the specified path with options.
  452. // This furnction works the same as SetOptions except that the value is set
  453. // as a raw block of json. This allows for setting premarshalled json objects.
  454. func SetRawOptions(json, path, value string, opts *Options) (string, error) {
  455. var optimistic bool
  456. if opts != nil {
  457. optimistic = opts.Optimistic
  458. }
  459. res, err := set(json, path, value, false, false, optimistic, false)
  460. if err == errNoChange {
  461. return json, nil
  462. }
  463. return string(res), err
  464. }
  465. // SetRawBytes sets a raw json value for the specified path.
  466. // If working with bytes, this method preferred over
  467. // SetRaw(string(data), path, value)
  468. func SetRawBytes(json []byte, path string, value []byte) (v []byte) {
  469. v, _ = SetRawBytesOptions(json, path, value, nil)
  470. return
  471. }
  472. // SetRawBytesErr sets a raw json value for the specified path.
  473. // If working with bytes, this method preferred over
  474. // SetRawErr(string(data), path, value)
  475. func SetRawBytesErr(json []byte, path string, value []byte) ([]byte, error) {
  476. return SetRawBytesOptions(json, path, value, nil)
  477. }
  478. type dtype struct{}
  479. // Delete deletes a value from json for the specified path.
  480. func Delete(json, path string) (string, error) {
  481. return SetErr(json, path, dtype{})
  482. }
  483. // DeleteBytes deletes a value from json for the specified path.
  484. func DeleteBytes(json []byte, path string) ([]byte, error) {
  485. return SetBytesErr(json, path, dtype{})
  486. }
  487. func set(jstr, path, raw string, stringify, del, optimistic, inplace bool) ([]byte, error) {
  488. if path == "" {
  489. if inplace {
  490. return yu_fast.S2B(jstr), &errorType{"path cannot be empty"}
  491. }
  492. return []byte(jstr), &errorType{"path cannot be empty"}
  493. }
  494. if !del && optimistic && isOptimisticPath(path) {
  495. res := Get(jstr, path)
  496. if res.Exists() && res.Index > 0 {
  497. sz := len(jstr) - len(res.Raw) + len(raw)
  498. if stringify {
  499. sz += 2
  500. }
  501. if inplace && sz <= len(jstr) {
  502. if !stringify || !mustMarshalString(raw) {
  503. jbytes := yu_fast.S2B(jstr)
  504. if stringify {
  505. jbytes[res.Index] = '"'
  506. copy(jbytes[res.Index+1:], raw)
  507. jbytes[res.Index+1+len(raw)] = '"'
  508. copy(jbytes[res.Index+1+len(raw)+1:], jbytes[res.Index+len(res.Raw):])
  509. } else {
  510. copy(jbytes[res.Index:], raw)
  511. copy(jbytes[res.Index+len(raw):], jbytes[res.Index+len(res.Raw):])
  512. }
  513. return jbytes[:sz], nil
  514. }
  515. return []byte(jstr), nil
  516. }
  517. buf := make([]byte, 0, sz)
  518. buf = append(buf, jstr[:res.Index]...)
  519. if stringify {
  520. buf = appendStringify(buf, raw)
  521. } else {
  522. buf = append(buf, raw...)
  523. }
  524. buf = append(buf, jstr[res.Index+len(res.Raw):]...)
  525. return buf, nil
  526. }
  527. }
  528. var paths []pathResult
  529. r, simple := parsePath(path)
  530. if simple {
  531. paths = append(paths, r)
  532. for r.more {
  533. r, simple = parsePath(r.path)
  534. if !simple {
  535. break
  536. }
  537. paths = append(paths, r)
  538. }
  539. }
  540. if !simple {
  541. if del {
  542. if inplace {
  543. return yu_fast.S2B(jstr), &errorType{"cannot delete value from a complex path"}
  544. }
  545. return []byte(jstr), &errorType{"cannot delete value from a complex path"}
  546. }
  547. return setComplexPath(jstr, path, raw, stringify)
  548. }
  549. njson, err := appendRawPaths(nil, jstr, paths, raw, stringify, del)
  550. if err != nil {
  551. if inplace {
  552. return yu_fast.S2B(jstr), err
  553. }
  554. return []byte(jstr), err
  555. }
  556. return njson, nil
  557. }
  558. func setComplexPath(jstr, path, raw string, stringify bool) ([]byte, error) {
  559. res := Get(jstr, path)
  560. if !res.Exists() || !(res.Index != 0 || len(res.Indexes) != 0) {
  561. return []byte(jstr), errNoChange
  562. }
  563. if res.Index != 0 {
  564. njson := []byte(jstr[:res.Index])
  565. if stringify {
  566. njson = appendStringify(njson, raw)
  567. } else {
  568. njson = append(njson, raw...)
  569. }
  570. njson = append(njson, jstr[res.Index+len(res.Raw):]...)
  571. jstr = string(njson)
  572. }
  573. if len(res.Indexes) > 0 {
  574. type val struct {
  575. index int
  576. res Result
  577. }
  578. vals := make([]val, 0, len(res.Indexes))
  579. res.ForEach(func(_, vres Result) bool {
  580. vals = append(vals, val{res: vres})
  581. return true
  582. })
  583. if len(res.Indexes) != len(vals) {
  584. return []byte(jstr), errNoChange
  585. }
  586. for i := 0; i < len(res.Indexes); i++ {
  587. vals[i].index = res.Indexes[i]
  588. }
  589. sort.SliceStable(vals, func(i, j int) bool {
  590. return vals[i].index > vals[j].index
  591. })
  592. for _, val := range vals {
  593. vres := val.res
  594. index := val.index
  595. njson := []byte(jstr[:index])
  596. if stringify {
  597. njson = appendStringify(njson, raw)
  598. } else {
  599. njson = append(njson, raw...)
  600. }
  601. njson = append(njson, jstr[index+len(vres.Raw):]...)
  602. jstr = string(njson)
  603. }
  604. }
  605. return []byte(jstr), nil
  606. }
  607. // SetOptions sets a json value for the specified path with options.
  608. // A path is in dot syntax, such as "name.last" or "age".
  609. // This function expects that the json is well-formed, and does not validate.
  610. // Invalid json will not panic, but it may return back unexpected results.
  611. // An error is returned if the path is not valid.
  612. func SetOptions(json, path string, value interface{}, opts *Options) (string, error) {
  613. if opts != nil {
  614. if opts.ReplaceInPlace {
  615. // it's not safe to replace bytes in-place for strings
  616. // copy the Options and set options.ReplaceInPlace to false.
  617. nopts := *opts
  618. opts = &nopts
  619. opts.ReplaceInPlace = false
  620. }
  621. }
  622. res, err := SetBytesOptions(yu_fast.S2B(json), path, value, opts)
  623. return string(res), err
  624. }
  625. // SetBytesOptions sets a json value for the specified path with options.
  626. // If working with bytes, this method preferred over
  627. // SetOptions(string(data), path, value)
  628. func SetBytesOptions(json []byte, path string, value interface{}, opts *Options) ([]byte, error) {
  629. var optimistic, inplace bool
  630. if opts != nil {
  631. optimistic = opts.Optimistic
  632. inplace = opts.ReplaceInPlace
  633. }
  634. jstr := yu_fast.B2S(json)
  635. var res []byte
  636. var err error
  637. switch v := value.(type) {
  638. default:
  639. b, merr := jsongo.Marshal(value)
  640. if merr != nil {
  641. return nil, merr
  642. }
  643. res, err = set(jstr, path, yu_fast.B2S(b), false, false, optimistic, inplace)
  644. case dtype:
  645. res, err = set(jstr, path, "", false, true, optimistic, inplace)
  646. case string:
  647. res, err = set(jstr, path, v, true, false, optimistic, inplace)
  648. case []byte:
  649. res, err = set(jstr, path, yu_fast.B2S(v), true, false, optimistic, inplace)
  650. case bool:
  651. if v {
  652. res, err = set(jstr, path, "true", false, false, optimistic, inplace)
  653. } else {
  654. res, err = set(jstr, path, "false", false, false, optimistic, inplace)
  655. }
  656. case int8:
  657. res, err = set(jstr, path, yu_strconv.FormatInt8(v),
  658. false, false, optimistic, inplace)
  659. case int16:
  660. res, err = set(jstr, path, yu_strconv.FormatInt16(v),
  661. false, false, optimistic, inplace)
  662. case int32:
  663. res, err = set(jstr, path, yu_strconv.FormatInt32(v),
  664. false, false, optimistic, inplace)
  665. case int64:
  666. res, err = set(jstr, path, yu_strconv.FormatInt64(v),
  667. false, false, optimistic, inplace)
  668. case uint8:
  669. res, err = set(jstr, path, yu_strconv.FormatUint8(v),
  670. false, false, optimistic, inplace)
  671. case uint16:
  672. res, err = set(jstr, path, yu_strconv.FormatUint16(v),
  673. false, false, optimistic, inplace)
  674. case uint32:
  675. res, err = set(jstr, path, yu_strconv.FormatUint32(v),
  676. false, false, optimistic, inplace)
  677. case uint64:
  678. res, err = set(jstr, path, yu_strconv.FormatUint64(v),
  679. false, false, optimistic, inplace)
  680. case float32:
  681. res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64),
  682. false, false, optimistic, inplace)
  683. case float64:
  684. res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64),
  685. false, false, optimistic, inplace)
  686. }
  687. if err == errNoChange {
  688. return json, nil
  689. }
  690. return res, err
  691. }
  692. // SetRawBytesOptions sets a raw json value for the specified path with options.
  693. // If working with bytes, this method preferred over
  694. // SetRawOptions(string(data), path, value, opts)
  695. func SetRawBytesOptions(json []byte, path string, value []byte, opts *Options) ([]byte, error) {
  696. var optimistic, inplace bool
  697. if opts != nil {
  698. optimistic = opts.Optimistic
  699. inplace = opts.ReplaceInPlace
  700. }
  701. res, err := set(yu_fast.B2S(json), path, yu_fast.B2S(value), false, false, optimistic, inplace)
  702. if err == errNoChange {
  703. return json, nil
  704. }
  705. return res, err
  706. }