-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
…e more safe Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
Do you have an example of what the end result looks like? Maybe in the examples folder? |
of course I do :) this is a file generated from The main idea is to force the handler to return one of the replays predefined for this particular method. |
and yes, I can create a strict (and build api) examples based on the existing ones. just tell me which one suits best |
Either the todo list api or the issue tracker example would be great |
Signed-off-by: Daniel Podolsky <onokonem@gmail.com>
done |
BTW, I need help adding tests for these options. |
so essentially the change is it returns the more precise responder types instead of the interface? |
exactly |
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>
I suppose this is not ready for merge yet is that correct? |
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:
unfortunately it looks like I'll not complete the tests |
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>
do you have your test system documented somewhere? I might be able to assign somebody less busy/lazy to this task |
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: |
@onokonem I've had a look at this work and was surprised about the way you are duplicating a lot of templates. 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. |
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. |
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"), |
There was a problem hiding this comment.
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", ""), |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
great, will do on weekend |
replaced by #2312 |
to make code more safe