-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add handling of arrays in form/query data like 'arr[]' #2247
Conversation
BTW, bikeshedding of |
The travis build failure seems unrelated, like a network failure. |
I'm going to start depending on this in my local project since I need it. Please let me know if there are any objections or changes that need to be made here. |
ping @s-ludwig |
Just an idea that I haven't fully thought through, but I actually wanted to implement this kind of array specification a while ago. My plan was to just detect the style used and keep supporting both ways under the same |
So there are decisions to be made with the API the way it currently works and adding in arbitrary appending to the array.
What I could do is change this PR to first search for the array using All of this also falls apart if you have an array of structs, as you can't use the non-indexed mechanism to specify an array of structs, I just want to make sure you agree with this. I'm willing to do all of this work, just let me know what you think is the best way, and will get merged :) Note, a way to get a range of elements that have the same name from Any other ideas, I'm game. I really need this functionality. |
Actually, looking at the DictionaryList, I can simply do byKeyValue and filter on the key name. So a range possibility already exists. I can switch to using that instead of the delegate. |
I'd generally say that the mixed case is somthing that is rather "nice to have", but not at all required (ignoring one of the two, or erroring out would be okay if there are gains in terms of performance or implementation complexity). Supporting the append array style for the underscore convention would also be "nice", but mainly if the changed implementation makes that trivial. Speaking of the implementation, to me it looks like the whole thing should really be turned upside down - iterating over the whole array using Anyway, following that |
Yeah, I think I will try that approach and see how it goes. It's bound to perform MUCH better, no more linear searches for each item, and I can probably avoid building strings to search with as well. |
ping @s-ludwig I have updated the implementation based on the discussion. This was a REALLY hard thing to get right. Please don't merge yet, I haven't tested it. I have to think about how to unittest this. I just wanted your review. I didn't update the struct-handling branch, it still searches the same way it did before for members. How it works now:
Unspecified slots are left as default values. So I also specified a max array length for indexes as 2^16-1. This is to prevent an attack of something like If an array element is specified twice, the second instance is ignored to preserve existing behavior. If an array element beyond a static array length is specified, it's ignored to preserve existing behavior. |
Ah, one thing I just realized I forgot -- the static array branch should return error if not all elements are filled in... But that's a minor change, will work on it maybe tomorrow. |
Will work on the compilation failures... |
I would probably have handled the mixed cases just the way that yields the shortest code, but if mixing the two styles is considered an actual use case, the new semantics are probably more useful, so I think that looks fine, too. Do you think that you'll be able to tackle fixing the compile error (+ add some tests) in the next few days? I'm planning to tag a new release and it would of course be nice to get this in (since the next stable one is probably a bit further off). |
I will work on this and try to get it done in the next couple days. Sorry, I've been busy with other work.
I can't tell what style is going to be used from the enum (I'm assuming you mean both append and indexed style), so I have to look for both. Plus you had said that it would be desirable to have sparse arrays, so I implemented that too. I suppose the easiest thing to do would be to just try the Note, the part I wrestled with the most is that when you have a simple type (one that is parsed from a single string), then you don't really want to recurse and search the array again. So I thought it would be good to split out the simple parsing and the nested parsing. But it was not as straightforward as I thought it would be. Anyway, will work on this soon. |
ping @s-ludwig I've fixed all the compilation issues (wow, it was bad. I should have tried testing before). Will monitor the CI to make sure everything works. Also added unit tests. |
@@ -1012,7 +1012,6 @@ final class HTTPServerRequest : HTTPRequest { | |||
|
|||
private void parseFormAndFiles() @safe { | |||
_form = FormFields.init; | |||
assert(!!bodyReader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moved to parseFormData
, because if the content type is not a form, bodyReader
isn't used.
Makes unit testing much easier, since I don't have to invent a null input stream (which is suspiciously missing).
The travis failure seems environment related. I don't understand the codecov report, I definitely am unit testing the code. |
I restarted the failing travis job, looks like it should pass then. I think the CodeCov results are looking strange because the branch needs a rebase. It seems to contain all changes on master since (presumably) January. |
Also TODO: need to update docs. |
…. Streamlined processing and removed some allocations.
Add rudimentary unit tests.
ac26af9
to
fb4db21
Compare
OK, I fixed the issues I had identified. Should be good to go. |
assert(result == ParamResult.ok); | ||
assert(arr2 == [[2,4],[3],[1]]); | ||
|
||
int[][2] staticarr2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified this to a 2-element array to test that the arr[2][]
field is ignored (found a bug in the process as well).
ping @s-ludwig this should be all set. |
Okay, looks good AFAICS. I'll merge and tag a beta release. |
See #2266 |
@s-ludwig I found a bug in this code (stupid operator precedence bug). Will submit a patch soon, don't release yet. |
Followup PR: #2270 |
Fixes #1984
A few notes about this:
@safe
delegate for getAll, but there's a lot of unsafe things within thewebConvTo
function, so I couldn't do it. TBH, I'm not sure its really the right approach to use the=void
, and use that very unsafe copy mechanism, but I'm assuming there's a good reason.Please double check the test I wrote for when things can't be serialized. I think I got it right, but I didn't test.