(buf []byte, x map[string]any)
| 858 | } |
| 859 | |
| 860 | func appendBinaryObject(buf []byte, x map[string]any) ([]byte, error) { |
| 861 | docOff := len(buf) |
| 862 | buf = appendUint32(buf, uint32(len(x))) |
| 863 | buf = appendZero(buf, dataSizeOff) |
| 864 | keyEntryBegin := len(buf) |
| 865 | buf = appendZero(buf, len(x)*keyEntrySize) |
| 866 | valEntryBegin := len(buf) |
| 867 | buf = appendZero(buf, len(x)*valEntrySize) |
| 868 | |
| 869 | fields := make([]field, 0, len(x)) |
| 870 | for key, val := range x { |
| 871 | fields = append(fields, field{key: key, val: val}) |
| 872 | } |
| 873 | slices.SortFunc(fields, func(i, j field) int { |
| 874 | return cmp.Compare(i.key, j.key) |
| 875 | }) |
| 876 | for i, field := range fields { |
| 877 | keyEntryOff := keyEntryBegin + i*keyEntrySize |
| 878 | keyOff := len(buf) - docOff |
| 879 | keyLen := uint32(len(field.key)) |
| 880 | if keyLen > math.MaxUint16 { |
| 881 | return nil, ErrJSONObjectKeyTooLong |
| 882 | } |
| 883 | jsonEndian.PutUint32(buf[keyEntryOff:], uint32(keyOff)) |
| 884 | jsonEndian.PutUint16(buf[keyEntryOff+keyLenOff:], uint16(keyLen)) |
| 885 | buf = append(buf, field.key...) |
| 886 | } |
| 887 | for i, field := range fields { |
| 888 | var err error |
| 889 | buf, err = appendBinaryValElem(buf, docOff, valEntryBegin+i*valEntrySize, field.val) |
| 890 | if err != nil { |
| 891 | return nil, errors.Trace(err) |
| 892 | } |
| 893 | } |
| 894 | docSize := len(buf) - docOff |
| 895 | jsonEndian.PutUint32(buf[docOff+dataSizeOff:], uint32(docSize)) |
| 896 | return buf, nil |
| 897 | } |
no test coverage detected