Skip to content
This repository has been archived by the owner on Mar 31, 2019. It is now read-only.

Allow any type for range query parameters, preserve numeric strings #65

Closed
wants to merge 3 commits into from
Closed

Conversation

vzsg
Copy link
Contributor

@vzsg vzsg commented Nov 18, 2016

This PR has a breaking change to how query parameters StartAt, EndAt and EqualTo work.

Previously these functions took a string as a parameter, tried to parse it into integer or boolean, and if none succeeded, escaped it with quotes.

This behavior is incorrect in my opinion, as:

  • It's possible that a string contains a valid number, but still should be escaped;
  • especially since Firebase has a strict precedence of types when performing range queries, e.g. "true" != true and "10231232" != 10231232, leaving the user of the library baffled why nothing is being returned*.
  • Floating point numbers are numbers too.

Instead, I modified the functions to take an empty interface instead, and use a type switch to decide if it should be quoted or just printed to string without quotes. The TestEscapeParameter test shows all the expected conversions.


  • This is actually how I found this library, trying to find the answer to one such issue on the firebase-community Slack. 馃槈

@@ -56,7 +56,7 @@ func (fb *Firebase) EndAt(value string) *Firebase {
func (fb *Firebase) OrderBy(value string) *Firebase {
c := fb.copy()
if value != "" {
c.params.Set(orderByParam, escapeString(value))
c.params.Set(orderByParam, fmt.Sprintf(`%q`, strings.Trim(value, `"`)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the escapeParameter function already does this, can we call that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about this, but as orderBy must take a string parameter, I thought there's not much point in going back and forth with types. (Note that I haven't changed the parameter type either.)

But sure, we can call that too. It would still work :)

@zabawaba99
Copy link
Owner

Thanks for making a PR! This particular logic was something that I had questioned for a while and I think your solution hits the nail right on the head! However, instead of changing the API, what do you think about adding another set of functions (StartAtVal, EndAtVal, EqualToVal [or something like that]) that take an interface{}? Doing that will allow us to not break the API but still be able to get the correct behavior when marshaling something other than a string.

@vzsg
Copy link
Contributor Author

vzsg commented Nov 18, 2016

To be honest, I'm biased towards my version (with the original logic being fragile depending on the input). But my sneaky behavioral change is just as dangerous, as it doesn't cause any compilation errors, just potentially break things at runtime right away...

Not an easy choice, so I'll leave it to you 馃槈 as the lib is yours. If you opt for the separate functions, I'll bring them back in the morning (and apply your comment above).

@zabawaba99
Copy link
Owner

So I took the weekend to think about this a bit and I think I would like to go with having separate functions. While I agree with you that the behavior is a bit funky - we would potentially break other clients by changing it.

However... I've been wanting to create a v2 branch for a long time and this particular issue is motivation enough to get started. In the v2 branch we will go with the implementation you have provided.

@vzsg
Copy link
Contributor Author

vzsg commented Nov 21, 2016

Alright, thanks for the update. I'll juggle back the previous implementations and add mine as separate functions later today.

@zabawaba99
Copy link
Owner

@vzsg Can you update the PR with the changes we spoke about?

@vzsg
Copy link
Contributor Author

vzsg commented Jan 9, 2017

It's been almost two months already? Sure, I'll have a go at it this weekend. I'll start over, because reimplementing the old behavior is highly demotivating.

chiaen pushed a commit to chiaen/firego that referenced this pull request Jan 11, 2017
chiaen pushed a commit to chiaen/firego that referenced this pull request Jan 11, 2017
chiaen pushed a commit to chiaen/firego that referenced this pull request Jan 11, 2017
@vzsg
Copy link
Contributor Author

vzsg commented Jan 12, 2017

Closing in favor of #74.

@vzsg vzsg closed this Jan 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants