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

development: strict types could be returned from handlers #1903

Closed
wants to merge 9 commits into from
Closed

development: strict types could be returned from handlers #1903

wants to merge 9 commits into from

Conversation

onokonem
Copy link
Contributor

to make code more safe

…e more safe

Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
generator/shared.go Outdated Show resolved Hide resolved
Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
@codecov
Copy link

codecov bot commented Mar 17, 2019

Codecov Report

Merging #1903 into master will decrease coverage by 0.05%.
The diff coverage is 76.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1903      +/-   ##
==========================================
- Coverage   80.74%   80.68%   -0.06%     
==========================================
  Files          38       38              
  Lines        7583     7669      +86     
==========================================
+ Hits         6123     6188      +65     
- Misses        981      994      +13     
- Partials      479      487       +8
Impacted Files Coverage Δ
generator/structs.go 74.76% <ø> (ø) ⬆️
generator/template_repo.go 87.96% <ø> (ø) ⬆️
generator/operation.go 89.14% <100%> (+0.01%) ⬆️
cmd/swagger/commands/generate/server.go 100% <100%> (ø) ⬆️
generator/shared.go 82.62% <40%> (-1.03%) ⬇️
generator/bindata.go 72.54% <83.33%> (+1.62%) ⬆️

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 873540c...b50d2a2. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 17, 2019

Codecov Report

Merging #1903 into master will decrease coverage by 0.25%.
The diff coverage is 76.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1903      +/-   ##
==========================================
- Coverage   80.93%   80.68%   -0.26%     
==========================================
  Files          38       38              
  Lines        7586     7669      +83     
==========================================
+ Hits         6140     6188      +48     
- Misses        964      994      +30     
- Partials      482      487       +5
Impacted Files Coverage Δ
generator/template_repo.go 87.96% <ø> (ø) ⬆️
generator/structs.go 74.76% <ø> (+1.13%) ⬆️
cmd/swagger/commands/generate/server.go 100% <100%> (ø) ⬆️
generator/operation.go 89.14% <100%> (+0.01%) ⬆️
generator/shared.go 82.62% <40%> (-2.77%) ⬇️
generator/bindata.go 72.54% <83.33%> (+1.62%) ⬆️
generator/types.go 90.88% <0%> (-0.96%) ⬇️

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 3123666...1c05f2b. Read the comment docs.

@casualjim
Copy link
Member

Do you have an example of what the end result looks like? Maybe in the examples folder?

@onokonem
Copy link
Contributor Author

of course I do :)

configure_issue_tracker.go

this is a file generated from examples/task-tracker. there are some more changes but they all are related to the changes in this file.

The main idea is to force the handler to return one of the replays predefined for this particular method.

@onokonem
Copy link
Contributor Author

and yes, I can create a strict (and build api) examples based on the existing ones. just tell me which one suits best

@casualjim
Copy link
Member

Either the todo list api or the issue tracker example would be great

Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
@onokonem
Copy link
Contributor Author

done

@onokonem
Copy link
Contributor Author

BTW, I need help adding tests for these options.

@casualjim
Copy link
Member

so essentially the change is it returns the more precise responder types instead of the interface?
And the builder changes because of that, including the not implemented error?

@onokonem
Copy link
Contributor Author

exactly

@casualjim
Copy link
Member

for tests we typically take a spec, generate code and verify that the files have the expected content through regex matchers etc.

Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
@casualjim
Copy link
Member

I suppose this is not ready for merge yet is that correct?

@onokonem
Copy link
Contributor Author

from my point of view it is ready to be merged

it is in use in prod in some place at the moment

the parts are missing:

  • tests to satisfy the coverage checker
  • tests to actual test the functionality

unfortunately it looks like I'll not complete the tests

@casualjim
Copy link
Member

Unfortunately we need tests to accept this contribution, so that it doesn't inadvertently breaks. The last year we spent increasing our test coverage and stability.

Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
@onokonem
Copy link
Contributor Author

Unfortunately we need tests to accept this contribution, so that it doesn't inadvertently breaks. The last year we spent increasing our test coverage and stability.

do you have your test system documented somewhere?

I might be able to assign somebody less busy/lazy to this task

@casualjim
Copy link
Member

I don't think we have it documented somewhere but it's pretty straight forward. You make a spec or take one of the pre-existing ones, and then call render. We have tons of tests that all look like this:

https://github.com/go-swagger/go-swagger/blob/master/generator/discriminators_test.go#L38-L65

Or more in line with this changeset:
https://github.com/go-swagger/go-swagger/blob/master/generator/server_test.go#L347-L379

@fredbi
Copy link
Contributor

fredbi commented Mar 31, 2019

@onokonem I've had a look at this work and was surprised about the way you are duplicating a lot of templates.
Is this change so hard on response, etc templates so as to require an entire copy of this source?

Couldn't the same be achieved with proper {{ if .Stricts }} ... {{ else }} ... {{ end }} with a single template?

@onokonem
Copy link
Contributor Author

Couldn't the same be achieved with proper {{ if .Stricts }} ... {{ else }} ... {{ end }} with a single template?

@fredbi , of course it could

But these templates are overcomplicated already.

@fredbi
Copy link
Contributor

fredbi commented Mar 31, 2019

But these templates are overcomplicated already.

So you think that duplicating the source would make the overall solution simpler to maintain and safer to use? I don't think anyone is going to apply their changes everywhere and maintain both sets of templates.

Another way to "fork out" templates is to add "contrib" templates, which live their own lives next to the main templates set.

@onokonem
Copy link
Contributor Author

Another way to "fork out" templates is to add "contrib" templates, which live their own lives next to the main templates set

I've tried that first, but found I need one more parameter passed to templates.

So I've added them

For my own task I've added the templates we are using to the project.

And yes, I'm ready to discuss how this has to be refactored to be added to the upstream, as it is a little pain to maintain the fork.

@@ -150,6 +150,13 @@ var assets = map[string][]byte{
"client/response.gotmpl": MustAsset("templates/client/response.gotmpl"),
"client/client.gotmpl": MustAsset("templates/client/client.gotmpl"),
"client/facade.gotmpl": MustAsset("templates/client/facade.gotmpl"),

"server/builder-strict.gotmpl": MustAsset("templates/server/builder-strict.gotmpl"),
"server/configureapi-strict.gotmpl": MustAsset("templates/server/configureapi-strict.gotmpl"),
Copy link
Contributor

Choose a reason for hiding this comment

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

This duplication of templates should be avoided. Parameters passed to templates are settable through the structs.go definitions and definitions in shared.go

@@ -432,15 +432,15 @@ func DefaultSectionOpts(gen *GenOpts) {
if gen.IncludeResponses {
ops = append(ops, TemplateOpts{
Name: "responses",
Source: "asset:serverResponses",
Source: "asset:serverResponses" + stringTernary(gen.Strict, "Strict", ""),
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra complexity that should be avoided.

@@ -580,6 +588,9 @@ type GenOpts struct {
CompatibilityMode string
ExistingModels string
Copyright string

Strict bool
Copy link
Contributor

Choose a reason for hiding this comment

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

GenOpts are available from the template, so there is no need to duplicate templates

@@ -1257,3 +1268,10 @@ func sharedValidationsFromSchema(v spec.Schema, isRequired bool) (sh sharedValid
}
return
}

func stringTernary(cond bool, ifTrue string, ifFalse string) string { // nolint: unparam
Copy link
Contributor

Choose a reason for hiding this comment

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

the // nolint: hint is in my opinion unnecessary. You could just declare this has: (cond bool, ifTrue, ifFalse string) string

@onokonem
Copy link
Contributor Author

onokonem commented Apr 3, 2019

great, will do on weekend

@onokonem
Copy link
Contributor Author

replaced by #2312

@onokonem onokonem closed this Jun 12, 2020
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.

4 participants