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

Generated render/bind implementations #68

Open
zacharyburkett opened this issue Nov 24, 2021 · 4 comments
Open

Generated render/bind implementations #68

zacharyburkett opened this issue Nov 24, 2021 · 4 comments

Comments

@zacharyburkett
Copy link
Member

Currently we use https://github.com/go-chi/render interfaces for our generated types, simplifying request/response management.

I propose we implement our own alternative that is generated alongside server/type code.

A generated implementation removes the need for a dependency and allows us to easily modify it to suit any future needs (ie more content types).

@dustin-decker
Copy link
Contributor

Closed by #47 ?

@karitham
Copy link
Collaborator

karitham commented Jan 4, 2022

Not exactly,

This issue is about generating go-chi/render's functions directly alongside the generated code, avoiding the dependency.

Since we already generate the implementation of interfaces, we just need the actual render implementation, which is quite small

@zacharyburkett
Copy link
Member Author

Correct, the goal of this issue is to simplify our dependencies.

@diamondburned
Copy link
Contributor

diamondburned commented Mar 18, 2022

As a sidenote, I think we're better off changing the generated method signatures to return a *Response or (*Response, error) pair directly instead of having the user call the Render function, since we're already breaking the http.Handler signature. For example,

func (h handler) Login(w http.ResponseWriter, r *http.Request, params openapi.LoginParams) *openapi.Response {
	s, err := h.server.LoginServer().Login(
		r.Context(),
		params.Username, params.Password,
		foodtinder.LoginMetadata{
			UserAgent: r.Header.Get("User-Agent"),
		},
	)
	if err != nil {
		return openapi.LoginJSON400Response(openapi.Error{Error: err})
	}

	return openapi.LoginJSON200Response(openapi.Session{
		UserID: openapi.ID(s.UserID),
		Token:  s.Token,
		Expiry: s.Expiry,
		Metadata: openapi.LoginMetadata{
			UserAgent: optstr(s.Metadata.UserAgent),
		},
	})
}

(package openapi is autogenerated.)

Created PR #80 to implement this.

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

No branches or pull requests

4 participants