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

collection: Support collecting posts #14

Closed
wants to merge 3 commits into from
Closed

collection: Support collecting posts #14

wants to merge 3 commits into from

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Dec 29, 2018

Note: This PR includes changes from #13 because the change being
proposed here relies on functionality added in #13. The change itself
can be reviewed in its own commit. The commit message for it is
repeated below.


This adds support for moving posts to a collection. This change relies
on #13 so that we can create and delete collections during tests.

Couple notes:

  • CollectPosts accepts a single struct rather than an alias and a list
    of structs. This makes it easy to add new optional parameters in the
    future.

  • We're passing []*CollectPost around rather than *[]CollectPost
    which is done in some of the existing APIs. The reasoning for this is

    • Idiomatically, slices are passed by-value ([]foo) rather than
      by-pointer (*[]foo) because slices are already reference types.
      Plus they're really cheap to copy since they're just a triple:
      pointer, length, and capacity (related blog post). We need
      pointers to slices only when we're trying to modify the original
      slice reference and don't have the ability to return a slice, like
      with json.Unmarshal (see also this post).

    • If we're trying to reduce copying, []*foo is better than []foo
      because otherwise foo will be copied when manipulating or
      accessing entries in the slice (like ranging over it).

    Existing APIs were left as-is to avoid breaking them. I can switch to
    *[]CollectPost if you'd prefer that but []*CollectPost is more
    idiomatic.

Resolves #4.

This adds support for deleting collections.

Resolves #1.
Rather than accepting a naked string, accept a DeleteCollectionParams
struct so that new optional parameters can be added in the future
without breaking the API.
This adds support for moving posts to a collection. This change relies
on #13 so that we can create and delete collections during tests.

Couple notes:

- CollectPosts accepts a single struct rather than an alias and a list
  of structs. This makes it easy to add new optional parameters in the
  future.

- We're passing `[]*CollectPost` around rather than `*[]CollectPost`
  which is done in some of the existing APIs. The reasoning for this is:

  - Idiomatically, slices are passed by-value (`[]foo`) rather than
    by-pointer (`*[]foo`) because slices are already reference types.
    Plus they're really cheap to copy since they're just a triple:
    pointer, length, and capacity ([related blog post][1]). We need
    pointers to slices only when we're trying to modify the original
    slice reference and don't have the ability to return a slice, like
    with `json.Unmarshal` (see also [this post][2]).

  - If we're trying to reduce copying, `[]*foo` is better than `[]foo`
    because otherwise `foo` will be copied when manipulating or
    accessing entries in the slice (like ranging over it).

  Existing APIs were left as-is to avoid breaking them. I can switch to
  `*[]CollectPost` if you'd prefer that but `[]*CollectPost` is more
  idiomatic.

[1]: https://blog.golang.org/go-slices-usage-and-internals#TOC_4.
[2]: https://blog.golang.org/slices#TOC_5.

Resolves #4.
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Hey @abhinav, thanks for this.

If your still interested in contributing this change there are a couple things we should change.

endpoint := "/collections/" + sp.Alias + "/collect"

var p []*CollectPostResult
env, err := c.post(endpoint, sp.Posts, &p)
Copy link

Choose a reason for hiding this comment

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

no need to pass the address here as slices are reference types already

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You need to pass a pointer for the JSON unmarshaler to fill it with a decoded value. So json.Unmarshal requires the result argument to always be a pointer.

https://play.golang.org/p/_eT_ICsK6dt

@thebaer
Copy link
Member

thebaer commented May 27, 2019

I want to hold off on anymore changes to this for now, as I implemented the same thing locally and would like to combine the best of both implementations.

@ghost
Copy link

ghost commented May 27, 2019

ok 👍 should we close this then?

@abhinav
Copy link
Contributor Author

abhinav commented May 27, 2019

@thebaer @robjloranger Feel free to close this PR.

@ghost
Copy link

ghost commented May 31, 2019

we can reopen if needed

@ghost ghost closed this May 31, 2019
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants