Skip to content

Auto wire api and backend #2510

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

Merged
merged 1 commit into from
Feb 9, 2021
Merged

Auto wire api and backend #2510

merged 1 commit into from
Feb 9, 2021

Conversation

youyuanwu
Copy link
Member

@youyuanwu youyuanwu commented Feb 1, 2021

Solving issue: #397

Configure.go can be regenerated, if cli option--implementation-package specified, then using another template.

The solution is suggested in the issue above. The implementation package must pre-exist and have certain functions implemented.

This is WIP, any suggestions are welcomed.

{{- end}}
{{ range .SecurityDefinitions }}
{{- if .IsBasicAuth }}
// Applies when the Authorization header is set with the Basic scheme
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good, although I'd have lower requirements on the API implementation regarding the presence of all technical methods (Auth, ConfigureFlags, etc.).

This is how I would do it. Maybe this is something to consider for a later improvement. Feel free to comment on these ideas.

type Implements{{ API name }}API interface {
  {{ ... all API handlers }} // attention! there is a CLI opt that changes the signature and requires specialized responders instead of the generic middleware.Responder
}

type Authable{{ API name}} interface {
   ... //  {{ ...}}Auth methods
}

type ConfigurableAPI interface {
... // ConfigureFlags, ConfigureServer, SetupMiddleware, etc...
   CustomConfigure(ImplementsSimpleToDoListAPIAPI)
}

func configureAPI(injected ImplementsSimpleToDoListAPIAPI) http.Handler {
 ...
 if authable, ok := injected.(Authable{{ API name}}) ; ok {
  api.{{ .ID }}Auth = authable.{{ .ID }}Auth
}
... etc you probably got the gist of it

This comment is not a blocker: we may proceed like this and improve it with another PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have some pending changes to displaying all the functions required for implementation obj in interfaces. I will push soon.

I thought about using interface assertion, and make this auto_configure_xxx.go easily compile, but the user should catch the unimplemented error in compile time, instead of runtime. If the interface assertion failed (in your authable) above, it is seems more appropriate to panic, instead of assigning a unimplemented runtime error.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added interfaces in the auto-configure template. If we think that type assertion on interfaces are preferred, I can add it in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just leave it as you first intended. Let's see how people react to this.

Signed-off-by: Youyuan Wu <youyuanwu@outlook.com>
@youyuanwu youyuanwu changed the title [WIP] Auto wire api and backend Auto wire api and backend Feb 9, 2021
@codecov
Copy link

codecov bot commented Feb 9, 2021

Codecov Report

Merging #2510 (92fa501) into master (f5667eb) will decrease coverage by 0.10%.
The diff coverage is 80.64%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2510      +/-   ##
==========================================
- Coverage   81.92%   81.82%   -0.11%     
==========================================
  Files          53       53              
  Lines        8376     8394      +18     
==========================================
+ Hits         6862     6868       +6     
- Misses       1022     1031       +9     
- Partials      492      495       +3     
Impacted Files Coverage Δ
generator/operation.go 94.23% <0.00%> (-0.13%) ⬇️
generator/structs.go 82.97% <ø> (ø)
generator/shared.go 87.52% <52.94%> (-1.35%) ⬇️
generator/support.go 84.91% <90.90%> (-0.75%) ⬇️
cmd/swagger/commands/generate/server.go 100.00% <100.00%> (ø)
generator/template_repo.go 88.99% <100.00%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f5667eb...92fa501. Read the comment docs.

@fredbi
Copy link
Contributor

fredbi commented Feb 9, 2021

Thank you @youyuanwu

@fredbi fredbi merged commit 6a96f03 into go-swagger:master Feb 9, 2021
@youyuanwu youyuanwu deleted the Auto-Wire-API-and-Backend branch March 29, 2021 01:48
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