Replies: 1 comment
|
Implementado em #273 — |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Overview
Follow-up to #196 (implemented in #265). That proposal added
Payload(_:encoder:contentType:), which takes aPayloadEncoderper call site. This proposes a way to set a default encoder once, mirroring how.charset(_:)and.urlEncoder(_:)already work forPayload— so a request tree (or a sharedPropertywrapper reused across an app) can default to Protobuf/MessagePack without repeatingencoder:at everyPayload(...)call site.API
Usage:
Design note:
encoder:needs to stay non-defaultedThe obvious-looking shape is:
But giving
encodera= nildefault reopens overload resolution on the already-shippedPayload(_:encoder:contentType:)forEncodable:Payload(mock)— the single most common call site, used throughout every existing app — would become ambiguous between theEncodable + JSONEncoderinitializer and this one, since both would be fully satisfied by zero extra arguments. That breaks source compatibility for anyone callingPayload(_:)on aCodable & Sendablevalue today.Keeping
encoderrequired but nilable (no default value) avoids that:Payload(mock)— still resolves to the existingEncodableinitializer, unchanged.Payload(myProtoMessage, encoder: ProtobufPayloadEncoder())— explicit encoder, same as Feature Proposal: Payload Encoding #196.Payload(myProtoMessage, encoder: nil)— falls back to.payloadEncoder(_:)from the environment; throwsEncodingPayloadError(a new context, e.g..missingPayloadEncoder) if none was set.It costs one extra
, encoder: nilat the call site compared to the fully-implicit version, but every existingPayload(codableValue)call keeps compiling exactly as it does today — nothing about theEncodable/JSONEncoderinitializer changes.Where the fallback gets resolved
Same place
charset/urlEncoderalready get read off the environment:Payload._makeProperty(property:inputs:)has access toinputs.environment.PayloadEncoderFactorywould carry the explicit encoder when one is passed, and otherwise defer toinputs.environment.payloadEncoder— the same plumbingPayloadInput.charset/.urlEncoderalready use.Scope decisions
Payload(_:)— that is exactly the ambiguity case above. The call site has to writeencoder: nilto opt in explicitly.Encodable/JSONEncoderinitializer at all — JSON stays the unaffected default..file(URL)source, no chunked/streamedencode, compression/encryption out of scope.Milestone: 4.1.0, alongside #196.
All reactions