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
Allow non-array Codables in get without id #1176
Conversation
A few points; let's first take a look at the following code:
Let's now also look at the following code:
I am concerned that given the changes in this PR (i.e. not using |
It will make the API looser, but that is what is needed to support the use case unless we have a different function defined. If we have a different function it needs to have some explicit label or name difference to be worthwhile, otherwise it won't provide any extra type-safety. We'd need to think of a good name for the function/parameter label that isn't too confusing as well. |
Just implemented a quick prototype as well:
I also kept these aliases in place:
But I also kept the methods in the API that leverage
I then added this new API method (and corresponding getSafely method):
Then in my application code, I can now do this (which was the main goal for this change):
The benefit I see in this is that we are clearly spelling out in our API that there is a handler for processing an array of codable objects, while there is another one that for processing a single entity. I understand that we could get this same behavior by using only the SimpleCodableClosure… but it is not evident to the developer as it is if we have the two typealiases in place. This would allow us, in the framework code, to explicitly invoke array properties/methods on the instance that is provided to the respondWith() closure without having to cast it to an array in order to determine if it is an array. I know we are not doing this at the moment… but if at some point in the future we need to perform any operations on the object returned from the app to the framework via the respondWith() closure, we will then have the infrastructure in place if we keep both methods using separate handlers, |
@rolivieri Sure, I tried this also but my thought process was that it provided no extra type-safety and was more code. However, I have no problem doing that if it supports future changes and you think it is more clear in the API. I did already check that it will call the correct overload if you do this, so I'm happy with that also. |
@tunniclm Sounds good, yes, I also tried it in my prototype and, as you pointed out, it does call the correct overload. Yes, I’d prefer to take this approach of keeping both methods using separate handlers, CodableArrayClosure and SimpleCodableClosure. This way we have the infrastructure in place for type-safety in our own code (meaning inside the framework) and we can take advantage of it if needed at some point in the future (plus it makes the API clearer from my perspective). |
dc219e7
to
641373d
Compare
@rolivieri -- this is ready for a quick review. I have left the commits separate so you can see what has changed in case that is helpful. This should be squashed to one commit when merged. |
Codecov Report
@@ Coverage Diff @@
## master #1176 +/- ##
==========================================
- Coverage 87.16% 87.01% -0.16%
==========================================
Files 38 38
Lines 2229 2249 +20
==========================================
+ Hits 1943 1957 +14
- Misses 286 292 +6
Continue to review full report at Codecov.
|
Updated logging statements to differentiate between get single with identifier and get single without identifier.
Description
I have changed the old array-restricted
get
function to instead allow anyCodable
in the response callback. This means changing theCodableArrayClosure
for the newSimpleCodableClosure
which I plan to add toKituraContracts
in PR Kitura/KituraContracts#4.This closure type is looser than
CodableArrayClosure
.CodableArrayClosure
is castable toSimpleCodableClosure
so the change to the function is not breaking. We no longer needCodableArrayClosure
but removing it fromKituraContracts
would be a breaking change, so I have left it there for now.Motivation and Context
Currently it is not possible to register a Codable
get
handler that doesn't want an id but does respond with a single Codable object (see #1172).For example, you cannot currently do the following:
This was simply an oversight, and we do allow both of these:
and
In fact, that last example could have been satisfied automatically if we had implemented the single Codable object.
How Has This Been Tested?
Passes existing Kitura tests and I have added a new test so that both the
[Codable]
and the new singleCodable
work.Also tested against an updated KituraKit in development mode (using
swift package edit
).Checklist: