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

Render: Presenter #47

Closed
wants to merge 13 commits into from
Closed

Render: Presenter #47

wants to merge 13 commits into from

Conversation

VojtechVitek
Copy link
Contributor

@VojtechVitek VojtechVitek commented Jun 13, 2016

Present your data models and/or create API object versioning!

func handler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
    obj := &Object{
        ID:       1,
        AuthData: "secret data for auth'd users only",
        Data:     []string{"one", "two", "three", "four"},
    }

    render.Respond(ctx, w, obj)
}
v2 := render.NewPresenter()
v2.Register(func(ctx context.Context, from *Object) (*v2.Object, error) {
    to := &v2.Object{
        Object: from,
        URL:    fmt.Sprintf("http://api.example.com/objects/%d", from.ID),
        Data:   map[string]bool{},
    }
    for _, item := range from.Data {
        to.Data[item] = true
    }
    return to, nil
})
r.Get("/", handler) // renders raw Object
r.Get("/v2", render.UsePresenter(v2), handler) // renders Object in version 2

}
}

func (p *presenter) RegisterFrom(presenter *presenter, presenters ...*presenter) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This copies Presenter conversion functions from other presenters.. Should we call it .Copy()?

or maybe pass it to NewPresenter() func:
version1 := NewPresenter(version2, versionLatest)

@pkieltyka pkieltyka mentioned this pull request Jun 16, 2016
10 tasks
)

// WithValue is a middleware that sets a given key/value in a context chain.
func WithValue(key interface{}, val interface{}) func(next chi.Handler) chi.Handler {
Copy link
Member

Choose a reason for hiding this comment

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

this is pretty cool, I like the short-hand

@VojtechVitek
Copy link
Contributor Author

Closing this PR and will open new one against v2.

@pkieltyka pkieltyka deleted the render branch July 26, 2016 17:35
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.

None yet

3 participants