flexJSONToString accepts a raw JSON value and returns it as a canonical JSON-encoded string. Acceptable inbound shapes: - absent / empty / null → nil (caller leaves the field unchanged) - JSON-encoded string whose INNER content is either empty (the legacy empty-string sentinel handled downstream by
(raw json.RawMessage, expectedStart byte, errInvalid error)
| 1880 | // is supposed to add. The pre-existing ItemUpdate fields/tags path |
| 1881 | // inherits the same tightening because it routes through this helper. |
| 1882 | func flexJSONToString(raw json.RawMessage, expectedStart byte, errInvalid error) (*string, error) { |
| 1883 | trimmed := bytes.TrimSpace(raw) |
| 1884 | if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) { |
| 1885 | return nil, nil |
| 1886 | } |
| 1887 | switch trimmed[0] { |
| 1888 | case '"': |
| 1889 | // JSON-encoded string envelope — unmarshal to the underlying Go |
| 1890 | // string and then validate the inner content's shape matches |
| 1891 | // expectedStart. Subsequent code that does |
| 1892 | // json.Unmarshal([]byte(*s), ...) sees the inner JSON, not a |
| 1893 | // re-quoted string. |
| 1894 | var s string |
| 1895 | if err := json.Unmarshal(trimmed, &s); err != nil { |
| 1896 | return nil, errInvalid |
| 1897 | } |
| 1898 | // Empty inner string is the empty-string sentinel; store-layer |
| 1899 | // coercion handles it (IDEA-1486). Don't reject here so callers |
| 1900 | // retain the legacy "" → default normalization shape. |
| 1901 | innerTrimmed := bytes.TrimSpace([]byte(s)) |
| 1902 | if len(innerTrimmed) == 0 { |
| 1903 | return &s, nil |
| 1904 | } |
| 1905 | if innerTrimmed[0] != expectedStart { |
| 1906 | return nil, errInvalid |
| 1907 | } |
| 1908 | // Confirm the inner content actually parses as JSON of the |
| 1909 | // expected shape. Catches strings that start with the right |
| 1910 | // brace but are otherwise garbage (e.g. `"{not valid"`). |
| 1911 | var inner any |
| 1912 | if err := json.Unmarshal(innerTrimmed, &inner); err != nil { |
| 1913 | return nil, errInvalid |
| 1914 | } |
| 1915 | return &s, nil |
| 1916 | case expectedStart: |
| 1917 | // Object or array — re-marshal to a canonical JSON string so |
| 1918 | // the downstream string-typed pipeline can json.Unmarshal it |
| 1919 | // back to a map/slice exactly as if the caller had stringified. |
| 1920 | // Decoded keeping number literals (BUG-3202): this re-marshal is |
| 1921 | // what a caller sending `fields` as an object gets stored, and a |
| 1922 | // float64 round trip rounded every integer above 2^53 in it. |
| 1923 | var v any |
| 1924 | if err := DecodeJSONKeepingNumbers(trimmed, &v); err != nil { |
| 1925 | return nil, errInvalid |
| 1926 | } |
| 1927 | b, err := json.Marshal(v) |
| 1928 | if err != nil { |
| 1929 | return nil, errInvalid |
| 1930 | } |
| 1931 | s := string(b) |
| 1932 | return &s, nil |
| 1933 | default: |
| 1934 | return nil, errInvalid |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | // FieldDateWindow is ItemListParams.FieldDateWindow. |
| 1939 | type FieldDateWindow struct { |
no test coverage detected