-
Notifications
You must be signed in to change notification settings - Fork 674
EmbeddedChannel: use tryAs() not forceAs() #602
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
Conversation
Can one of the admins verify this patch? |
1 similar comment
Can one of the admins verify this patch? |
@ianpartridge that's a good question, I don't have a strong opinion. Downside is obviously that we can then no longer distinguish between empty and wrong type. The other question is if this is SemVer major... @normanmaurer / @Lukasa ? |
hmm... I think if we do this we should throw as otherwise it is impossible to tell if it was empty or not as @weissi stated. And yes I think thats a semver major |
I agree that throwing would be best, and this is SemVer major unless we did something like: public enum EmbeddedChannelReadInboundError: Error {
case wrongType
}
extension EmbeddedChannel {
public func tryReadInbound<T>() throws -> T? {
return try readFromBuffer(buffer: &channelcore.inboundBuffer)
}
private func readFromBuffer<T>(buffer: inout [NIOAny]) throws -> T? {
if buffer.isEmpty {
return nil
}
guard let t = buffer.removeFirst().tryAs(type: T.self) else {
throw EmbeddedChannelReadInboundError.wrongType
}
return t
}
} (uncompiled) |
Any thoughts on what you'd like to do with this? |
So, my inclination is that we should deprecate the old functionality and simply replace it with new, better behaviour. That allows us to defer the SemVer major function removal. Then we can use @ianpartridge's solution (though the separate internal |
@ianpartridge now we could take this for NIO 2. Let's just add the |
🙂 Let me page all this back in... You'd like to change the existing function? From: |
@ianpartridge I think |
and then we can use XCTAssertNoThrow(XCTAssertEqual(7, try channel.readInbound(as: Int.self))) |
OK, I guess make |
@ianpartridge I created #861 which should do all the test changes necessary, so after that, yours should just work pretty much as is |
Oh wow, thanks! It looks good to me. |
@ianpartridge my PR is now merged, so you can rebase this one I think |
Thanks, I will. What are we going to throw? Are we going to add public enum EmbeddedChannelReadInboundError: Error {
case wrongType
} |
I think we could even nest that type into extension EmbeddedChannel {
public enum Error {
case wrongType(expected: Any.Type, actual: Any.Type)
}
} or so? Or given that so far it's only one error and we can't extend anyway after NIO 2 extension EmbeddedChannel {
public struct WrongTypeError {
public init (expected: Any.Type, actual: Any.Type) {}
}
} @Lukasa wdyt? |
Certainly think we should embed it. Also a struct seems like the right thing to do too. |
WIP but I've pushed a commit with what I think you meant :) |
@ianpartridge thanks, that looks great to me! Mind writing a test or two in |
I pushed a few tests - let me know if you had something else in mind. |
Updated to conform |
@swift-nio-bot test this please |
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.
thanks very much @ianpartridge
@swift-nio-bot test this please |
This is really a question - I am wondering why
EmbeddedChannel
callsforceAs()
instead oftryAs()
when attempting to read from a[NIOAny]
. As the primary usecase ofEmbeddedChannel
is in testing why not returnnil
fromreadInbound()
(or throw) so the user can handle that in their test?