SetBytesOptions sets a json value for the specified path with options. If working with bytes, this method preferred over SetOptions(string(data), path, value)
(json []byte, path string, value interface{},
opts *Options)
| 650 | // If working with bytes, this method preferred over |
| 651 | // SetOptions(string(data), path, value) |
| 652 | func SetBytesOptions(json []byte, path string, value interface{}, |
| 653 | opts *Options) ([]byte, error) { |
| 654 | var optimistic, inplace bool |
| 655 | if opts != nil { |
| 656 | optimistic = opts.Optimistic |
| 657 | inplace = opts.ReplaceInPlace |
| 658 | } |
| 659 | jstr := *(*string)(unsafe.Pointer(&json)) |
| 660 | var res []byte |
| 661 | var err error |
| 662 | switch v := value.(type) { |
| 663 | default: |
| 664 | b, merr := jsongo.Marshal(value) |
| 665 | if merr != nil { |
| 666 | return nil, merr |
| 667 | } |
| 668 | raw := *(*string)(unsafe.Pointer(&b)) |
| 669 | res, err = set(jstr, path, raw, false, false, optimistic, inplace) |
| 670 | case dtype: |
| 671 | res, err = set(jstr, path, "", false, true, optimistic, inplace) |
| 672 | case string: |
| 673 | res, err = set(jstr, path, v, true, false, optimistic, inplace) |
| 674 | case []byte: |
| 675 | raw := *(*string)(unsafe.Pointer(&v)) |
| 676 | res, err = set(jstr, path, raw, true, false, optimistic, inplace) |
| 677 | case bool: |
| 678 | if v { |
| 679 | res, err = set(jstr, path, "true", false, false, optimistic, inplace) |
| 680 | } else { |
| 681 | res, err = set(jstr, path, "false", false, false, optimistic, inplace) |
| 682 | } |
| 683 | case int8: |
| 684 | res, err = set(jstr, path, strconv.FormatInt(int64(v), 10), |
| 685 | false, false, optimistic, inplace) |
| 686 | case int16: |
| 687 | res, err = set(jstr, path, strconv.FormatInt(int64(v), 10), |
| 688 | false, false, optimistic, inplace) |
| 689 | case int32: |
| 690 | res, err = set(jstr, path, strconv.FormatInt(int64(v), 10), |
| 691 | false, false, optimistic, inplace) |
| 692 | case int64: |
| 693 | res, err = set(jstr, path, strconv.FormatInt(int64(v), 10), |
| 694 | false, false, optimistic, inplace) |
| 695 | case uint8: |
| 696 | res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10), |
| 697 | false, false, optimistic, inplace) |
| 698 | case uint16: |
| 699 | res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10), |
| 700 | false, false, optimistic, inplace) |
| 701 | case uint32: |
| 702 | res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10), |
| 703 | false, false, optimistic, inplace) |
| 704 | case uint64: |
| 705 | res, err = set(jstr, path, strconv.FormatUint(uint64(v), 10), |
| 706 | false, false, optimistic, inplace) |
| 707 | case float32: |
| 708 | res, err = set(jstr, path, strconv.FormatFloat(float64(v), 'f', -1, 64), |
| 709 | false, false, optimistic, inplace) |
no test coverage detected
searching dependent graphs…