-
Notifications
You must be signed in to change notification settings - Fork 221
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
Add a content type type member to response encoders #329
Add a content type type member to response encoders #329
Conversation
Current coverage is
|
This looks cool! I have just one question. Should we change |
@vkostyukov Yeah, that makes sense, and should be pretty straightforward. I'm out for a bit but I'll update the PR when I get home. |
I'm not sure I understand |
Not really—the advantage is that it allows the compiler to select an implicit instance on the basis of the content string. I can ask for an instance for encoding strings as You could accomplish almost exactly the same thing by enumerating content types like this: class ContentType(val contentType: String)
case object ApplicationJson extends ContentType("application/json")
case object PlainText extends ContentType("text/plain") And then putting a |
Oh, I see. That makes sense. Thanks for the explanation @travisbrown! |
I started thinking a little more last night about how we want to make this new functionality available to users, and I'm not sure Instead as a short-term solution we could provide a In the longer term I think there might be a nice way to handle content negotiation along these lines, but that would be a bigger API change. So that's probably something to consider before 1.0, but for now it would be nice to have something for 0.8 that handles e.g. the TechEmpower benchmark use case cleanly. Any thoughts, @vkostyukov, @penland365, @rpless? |
I like this - the automatic preference to encode a plain string as JSON bit me last week ( easy fix though ). I do especially like the idea of a |
I had something in mind for a while (as part of Finch 1.0) that should give us an answer on how to do that. The fact that this issue's showed up gives me a hope that the version of Finch 1.0 I was imagining fits the real world pretty good so far. It was a pretty busy week, so I didn't manage to put a roadmap doc together. I will try to finish it this weekend. |
I'm fine with |
The ideas in this PR were merged in as part of #541. Thanks @travisbrown for the inspiration! |
This is a quick proof of concept to follow up on the conversation in #328 this afternoon. The issue is that it's currently easy to accidentally use an
EncodeResponse[String]
instance that's intended for encoding strings as JSON when you just want to encode a string as plain text.This change adds a type member to
EncodeResponse
that tracks the content type as a type-level string (this allows us to avoid enumerating content types as singleton objects or whatever).The behavior is still the same when you don't specify the content type:
But when you do specify the content type you'll get the instance you want:
Here
EncodeTextResponse
andEncodeJsonResponse
are just type aliases:(The
Witness
syntax is handy but kind of unpleasant looking.)Note that the usage of
EncodeResponse.apply
is changed up a bit in order to help Scala's type inference be a little more useful. Otherwise there are no changes to tests or examples.