Skip to content
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

fix paging #192

Merged
merged 5 commits into from
Dec 12, 2017
Merged

fix paging #192

merged 5 commits into from
Dec 12, 2017

Conversation

tomerweller
Copy link
Contributor

apply query filters on paging links. closes #148

also:

  • modifies the Page struct to include a full url, rather than separate base url, path and query params.
  • introduces a support/url package and a URL struct for convenient url modifications.

Copy link
Contributor

@nikhilsaraf nikhilsaraf left a comment

Choose a reason for hiding this comment

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

looks solid!
added some comments/questions, nothing major.

if err != nil {
return
}
return URL(*gu), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

we should not be returning specific values if this function has named return values.


// String encodes all URL segments to a fully qualified URL string
func (u URL) String() string {
gu := gUrl.URL(u)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we not just call u.String()? -- i.e. maybe just leave off this method altogether?

if not -- return gUrl.URL(u).String(), doesn't need the passthrough var gu

Copy link
Contributor Author

@tomerweller tomerweller Dec 9, 2017

Choose a reason for hiding this comment

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

In order to do that, you'd need to convert the sURL into a gURL every-time you want to call it...

Adding a passthrough gives the following error:

# github.com/stellar/go/support/url
../../../support/url/main.go:24:21: cannot call pointer method on url.URL(*u)
../../../support/url/main.go:24:21: cannot take the address of url.URL(*u)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe @nullstyle or one of the golang experts can weigh in on this.

q := gu.Query()
q.Del(key)
q.Add(key, val)
gu.RawQuery = q.Encode()
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need to call q.Encode() here, will it still work if we just delete this line?
it seems hacky to override gu.RawQuery, almost like we're setting values on gu twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RawQuery is a string. The Query() function decodes current RawQuery string and returns a Values struct. I don't see another way to do this.

Copy link
Contributor

Choose a reason for hiding this comment

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

what I'm suggesting is -- is gu.RawQuery = q.Encode() a nop 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.

No, unfortunately this is how the URL struct is built

newUrl := action.BaseURL() // preserve scheme and host for the new url links
newUrl.RawQuery = action.R.URL.RawQuery //preserve query parameters
newUrl.Path = action.Path() //preserve path
newUrl := action.FullURL() // preserve scheme and host for the new url links
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for a cleaner code pattern

ht.Assert.Equal(("http://localhost" + kase.wantSelf), links.Self.Href)
ht.Assert.Equal(("http://localhost" + kase.wantPrevious), links.Prev.Href)
ht.Assert.Equal(("http://localhost" + kase.wantNext), links.Next.Href)
ht.Assert.EqualUrlStrings("http://localhost" + kase.wantSelf, links.Self.Href)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you have gofmt enabled on save? --that would have removed the " " around the + here, which is why I had the ()

@@ -88,5 +87,5 @@ func (action *AssetsAction) loadPage() {
if action.AssetIssuer != "" {
linkParams.Set("asset_issuer", action.AssetIssuer)
}
action.Page.PopulateLinksWithParams(linkParams)
Copy link
Contributor

Choose a reason for hiding this comment

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

you should also remove the linkParams var here since it's no longer used --your compiler should have issued warning about this?

return result
}

// BaseURL returns the base url for this request, defined as a url containing
// the Host and Scheme portions of the request uri.
func (action *Action) BaseURL() *url.URL {
Copy link
Contributor

Choose a reason for hiding this comment

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

if FullURL is the only consumer of BaseURL now then we should lowercase this method name.

@@ -39,37 +41,40 @@ type Links struct {
type Page struct {
Links Links `json:"_links"`
BasePage
BasePath string `json:"-"`
//BasePath string `json:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

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

delete instead of commenting out

rec := p.Embedded.Records

//verify paging params
selfUrl := sUrl.URL(*p.FullURL).
Copy link
Contributor

Choose a reason for hiding this comment

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

don't think you need the * here.

Copy link
Contributor

@nikhilsaraf nikhilsaraf left a comment

Choose a reason for hiding this comment

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

looks like there's still some comments you may be working through, let me know when to re-review.

q := gu.Query()
q.Del(key)
q.Add(key, val)
gu.RawQuery = q.Encode()
Copy link
Contributor

Choose a reason for hiding this comment

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

what I'm suggesting is -- is gu.RawQuery = q.Encode() a nop here?

Copy link
Contributor

@nikhilsaraf nikhilsaraf left a comment

Choose a reason for hiding this comment

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

LGTM!

@nikhilsaraf
Copy link
Contributor

See the one travis failure before merging.

@tomerweller tomerweller merged commit cb5972a into stellar:master Dec 12, 2017
@tomerweller
Copy link
Contributor Author

@nullstyle I went ahead and merged this. When your table clears you can take a look to see if you have some feedback from an idiomatic perspective. not urgent at all.

tomerweller added a commit to tomerweller/go that referenced this pull request Dec 12, 2017
jedmccaleb pushed a commit that referenced this pull request Dec 12, 2017
@@ -1,13 +1,15 @@
package hal

import (
sUrl "github.com/stellar/go/support/url"
Copy link
Contributor

Choose a reason for hiding this comment

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

package names should always be lower cased: https://blog.golang.org/package-names

We should do the same with our aliases, IMO

gu := gUrl.URL(u)
q := gu.Query()
q.Del(key)
q.Add(key, val)
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of using Del then Add, just use Set.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@nullstyle nullstyle left a comment

Choose a reason for hiding this comment

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

.

)

// URL wraps around the native golang URL struct to allow for custom methods
type URL gUrl.URL
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really happy with this whole type. Go isn't an object-oriented language and this smells too much like you're trying to derive a subtype of URL so you can override some methods. Instead of pretending this type is a URL lets call this type a URLBuilder and lets have it wrap a url.URL, not alias it.

See https://github.com/lann/builder for a pretty well worn implementation of the fluent builder pattern you are proposing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Paging should respect query filters
3 participants