๐Ÿ“ฆ techouse / qs-net

๐Ÿ“„ Qs.cs ยท 282 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using QsNet.Enums;
using QsNet.Internal;
using QsNet.Models;
using Decoder = QsNet.Internal.Decoder;
using Encoder = QsNet.Internal.Encoder;

namespace QsNet;

/// <summary>
///     Provides static methods for encoding and decoding query strings and dictionaries.
///     Supports conversion between query strings and Dictionary&lt;object, object?&gt; objects
///     with configurable parsing and encoding options.
/// </summary>
public static class Qs
{
    /// <summary>
    ///     Decode a query string or a Dictionary into a Dictionary&lt;object, object?&gt;.
    /// </summary>
    /// <param name="input">The query string or Dictionary to decode</param>
    /// <param name="options">Optional decoder settings</param>
    /// <returns>The decoded Dictionary</returns>
    /// <exception cref="ArgumentException">If the input is not a string or Dictionary</exception>
    /// <exception cref="InvalidOperationException">If limits are exceeded and ThrowOnLimitExceeded is true</exception>
    public static Dictionary<string, object?> Decode(object? input, DecodeOptions? options = null)
    {
        var opts = options ?? new DecodeOptions();

        if (input is not string and not IDictionary and not null)
            throw new ArgumentException("The input must be a String or a Map<String, Any?>");

        if (input is null or string { Length: 0 } or IDictionary { Count: 0 })
            return new Dictionary<string, object?>();

        // parse the raw pairs (string-keyed)
        var tempObj = input switch
        {
            string qs => Decoder.ParseQueryStringValues(qs, opts),

            IEnumerable<KeyValuePair<string?, object?>> gen => gen.ToDictionary(
                kv => kv.Key ?? string.Empty,
                kv => Utils.ConvertNestedValues(kv.Value) // value-level walk
            ),

            IDictionary raw => Utils.ConvertNestedDictionary(raw),

            _ => null
        };

        var finalOptions = opts;
        if (opts is { ParseLists: true, ListLimit: > 0 } && (tempObj?.Count ?? 0) > opts.ListLimit)
            finalOptions = opts.CopyWith(parseLists: false);

        // keep internal work in object-keyed maps
        if (tempObj is not { Count: > 0 })
            return new Dictionary<string, object?>();

        var obj = new Dictionary<object, object?>(tempObj.Count);

#if NETSTANDARD2_0
        foreach (var kv in tempObj)
        {
            var key = kv.Key;
            var value = kv.Value;

            var parsed = Decoder.ParseKeys(key, value, finalOptions, input is string);
            if (parsed is null)
                continue;

            if (obj.Count == 0 && parsed is IDictionary first)
            {
                obj = Utils.ToObjectKeyedDictionary(first);
                continue;
            }

            var merged = Utils.Merge(obj, parsed, finalOptions) ?? obj;

            obj = merged switch
            {
                Dictionary<object, object?> d => d,
                IDictionary id => Utils.ToObjectKeyedDictionary(id),
                _ => obj
            };
        }
#else
        foreach (var (key, value) in tempObj)
        {
            var parsed = Decoder.ParseKeys(key, value, finalOptions, input is string);
            if (parsed is null)
                continue;

            if (obj.Count == 0 && parsed is IDictionary first)
            {
                obj = Utils.ToObjectKeyedDictionary(first);
                continue;
            }

            var merged = Utils.Merge(obj, parsed, finalOptions) ?? obj;

            obj = merged switch
            {
                Dictionary<object, object?> d => d,
                IDictionary id => Utils.ToObjectKeyedDictionary(id),
                _ => obj
            };
        }
#endif

        // compact (still object-keyed), then convert the whole tree to string-keyed
        var compacted = Utils.Compact(obj, opts.AllowSparseLists);
        return Utils.ToStringKeyDeepNonRecursive(compacted);
    }

    /// <summary>
    ///     Encode a Dictionary or IEnumerable into a query string.
    /// </summary>
    /// <param name="data">The data to encode</param>
    /// <param name="options">Optional encoder settings</param>
    /// <returns>The encoded query string</returns>
    /// <exception cref="InvalidOperationException">Thrown when options/limits are violated during encoding</exception>
    public static string Encode(object? data, EncodeOptions? options = null)
    {
        var opts = options ?? new EncodeOptions();

        if (data is null)
            return string.Empty;

        // Map/Iterable โ†’ Dictionary<string, object?>
        var obj = data switch
        {
            Dictionary<string, object?> dict => dict,
            IDictionary<string, object?> genericDict => new Dictionary<string, object?>(
                genericDict
            ),
            IDictionary map => Utils.ConvertDictionaryToStringKeyed(map),
            IEnumerable en and not string => CreateIndexDictionary(en),
            _ => new Dictionary<string, object?>()
        };

        if (obj.Count == 0)
            return string.Empty;

        // Optional top-level filter: function can replace the object; iterable can supply key order
        List<object?>? objKeys = null;
        switch (opts.Filter)
        {
            case FunctionFilter ff:
                try
                {
                    var filtered = ff.Function(string.Empty, obj);
                    obj = filtered switch
                    {
                        IDictionary<string, object?> genericFiltered => new Dictionary<
                            string,
                            object?
                        >(genericFiltered),
                        IDictionary m => Utils.ConvertDictionaryToStringKeyed(m),
                        _ => obj
                    };
                }
                catch
                {
                    // swallow filter exceptions like Kotlin code
                }

                break;

            case IterableFilter wl:
                objKeys = wl.Iterable.Cast<object?>().ToList();
                break;
        }

        // Default keys if filter didn't provide
        if (objKeys is null)
        {
            objKeys = new List<object?>(obj.Count);
            foreach (var k in obj.Keys)
                objKeys.Add(k);
        }

        // Optional sort
        if (opts.Sort != null)
            objKeys.Sort(Comparer<object?>.Create(opts.Sort));

        // Root side-channel frame (mirrors WeakHashMap chain in Kotlin)
        var sideChannel = new SideChannelFrame();

        // Collect "key=value" parts
        var parts = new List<string>(objKeys.Count);

        for (var i = 0; i < objKeys.Count; i++)
        {
            var keyObj = objKeys[i];

            if (keyObj is not string key)
                continue;

            var hasKey = obj.TryGetValue(key, out var value);

            if (!hasKey && opts.SkipNulls)
                continue;
            if (value is null && opts.SkipNulls)
                continue;

            var encoded = Encoder.Encode(
                value,
                !hasKey,
                sideChannel,
                key,
                opts.ListFormat?.GetGenerator(),
                opts is { ListFormat: ListFormat.Comma, CommaRoundTrip: true },
                opts is { ListFormat: ListFormat.Comma, CommaCompactNulls: true },
                opts.AllowEmptyLists,
                opts.StrictNullHandling,
                opts.SkipNulls,
                opts.EncodeDotInKeys,
                opts.Encode ? opts.GetEncoder : null,
                opts.GetDateSerializer,
                opts.Sort,
                opts.Filter,
                opts.AllowDots,
                opts.Format,
                opts.Formatter,
                opts.EncodeValuesOnly,
                opts.Charset,
                opts.AddQueryPrefix
            );

            switch (encoded)
            {
                case IEnumerable en and not string:
                    {
                        foreach (var p in en)
                            if (p is not null)
                                parts.Add(p.ToString()!);
                        break;
                    }
                case string { Length: > 0 } s:
                    parts.Add(s);
                    break;
            }
        }

        var joined = string.Join(opts.Delimiter, parts);

        // Build final output
        var sb = new StringBuilder(joined.Length + 16);

        if (opts.AddQueryPrefix)
            sb.Append('?');

        if (opts.CharsetSentinel)
        {
            // encodeURIComponent('&#10003;') and encodeURIComponent('โœ“')
            if (opts.Charset.WebName.Equals("iso-8859-1", StringComparison.OrdinalIgnoreCase))
                sb.Append(Sentinel.Iso.GetEncoded()).Append(joined.Length > 0 ? "&" : "");
            else if (opts.Charset.WebName.Equals("utf-8", StringComparison.OrdinalIgnoreCase))
                sb.Append(Sentinel.Charset.GetEncoded()).Append(joined.Length > 0 ? "&" : "");
        }

        if (joined.Length > 0)
            sb.Append(joined);

        return sb.ToString();

        static Dictionary<string, object?> CreateIndexDictionary(IEnumerable en)
        {
            var initial = en is ICollection col ? col.Count : 0;
            var dict = new Dictionary<string, object?>(initial);
            var i = 0;
            foreach (var v in en)
                dict.Add(i++.ToString(CultureInfo.InvariantCulture), v);
            return dict;
        }
    }
}