<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Rex,<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 10, 2017, at 12:48 PM, Rex Fenley via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Forgive me if I'm missing something, this is a very large proposal and a lot to parse through. I like how everything is designed as far as I can tell except I'm not sure from this proposal how one would compose multiple different types of Decoders against the same output type. For example, with Location we have.</div><div class=""><br class=""></div><div class="">// Continuing examples from before; below is automatically generated by the compiler if no customization is needed.</div><div class="">public struct Location : Codable {</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>private enum CodingKeys : CodingKey {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; case latitude</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; case longitude</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; public init(from decoder: Decoder) throws {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; let container = try decoder.container(keyedBy: CodingKeys.self)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; latitude = try container.decode(Double.self, forKey: .latitude)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; longitude = try container.decode(Double.self, forKey: .longitude)</div><div class="">&nbsp; &nbsp; }</div><div class="">}</div><div class=""><br class=""></div><div class="">However, this initializer seems strictly tied to the `CodingKeys` set of coding keys. From what it appears, if this was used to decode from JSON the format would have to always be:</div><div class=""><br class=""></div><div class="">{</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>"latitude" : 20.0</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>"longitude" : 20.0</div><div class="">}</div><div class=""><br class=""></div><div class="">I have a use case we're on my client we began the process of switching from one version of an API to another. In one version we had a payload similar to</div><div class=""><br class=""></div><div class="">{</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>"user" : {</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>"uuid" : "uuid string..."</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>"can_send_message" : true</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>"can_delete_message" : false</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}</div><div class=""><br class=""></div><div class="">this would result in the following Codable (from "user") from what I'm following</div><div class=""><br class=""></div><div class="">public struct User : Codable {</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>private enum CodingKeys : CodingKey {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; case uuid</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; case can_send_message</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; case can_delete_message</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; public init(from decoder: Decoder) throws {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; let container = try decoder.container(keyedBy: CodingKeys.self)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; uuid = try container.decode(Double.self, forKey: .uuid)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; canSendMessage = try container.decode(Bool.self, forKey: .can_send_message)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; canDeleteMessage = try container.decode(Bool.self, forKey: .can_delete_message)</div><div class="">&nbsp; &nbsp; }</div><div class="">}</div><div class=""><br class=""></div><div class="">when we began switching over to the new api the payload was returned as</div><div class=""><br class=""></div><div class="">{</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>user {</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>"uuid" : "uuid string..."</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>"permissions" : {</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>"can_send_message" : true</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                        </span>"can_delete_message" : false</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>}</div><div class=""><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>}</div><div class="">}</div><div class=""><br class=""></div><div class="">Here with "permissions" we have a new internal container with a separate set of coding keys. Issue is in my use case I still need to maintain a way to decode the old version simultaneously; we guard all new changes behind feature flags in case something goes awry and we need to roll back our changes.</div><div class=""><br class=""></div><div class="">How would one go about expressing this new Decoding and the previous Decoding simultaneously on the same User type?</div></div></div></blockquote><div><br class=""></div>You would fall out of the automatically-generated scenario here, but this use case is exactly what the ‘container’ API is for. You can create a container with a new set of keys, then decode from it.</div><div><br class=""></div><div>Another approach would be to create a nested type that conforms with Codable which represents the “permissions” as a type of its own.</div><div><br class=""></div><div>- Tony</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><span class=""><div style="line-height: 1.15; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size:16px;font-family:arial;background-color:transparent;font-style:italic;vertical-align:baseline;white-space:pre-wrap" class="">Rex Fenley</span><span style="font-size:16px;font-family:arial;background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class=""> &nbsp;</span><span style="font-size:11px;font-family:arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class="">|</span><span style="line-height:1.15;font-family:arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class=""> &nbsp;</span><span style="font-size:11px;font-family:arial;color:rgb(153,153,153);background-color:transparent;vertical-align:baseline;white-space:pre-wrap" class="">IOS DEVELOPER</span><br class=""></div></span><span class=""><br class=""><div style="line-height: 1.15; margin-top: 0pt; margin-bottom: 0pt;" class=""><span style="font-size:11px;font-family:arial;color:rgb(153,153,153);vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""><img src="https://lh5.googleusercontent.com/xMgzw3JkFL3DLkdwyq0WxJzKs_XP57gVVCaBMvgi1FKCjSeue0xdx3JZeCWBlxN4KRHhHOfdvJbc1N-AjTwXcKIq4cjJg9H7iaFpQ8WbO4N3c9Y5dzi19cPOs_owPquuqw" width="250px;" height="53px;" style="border: none;" class=""></span></div><div style="line-height: 1.15; margin-top: 0pt; margin-bottom: 0pt;" class=""><a href="https://www.remind.com/" style="text-decoration:none" target="_blank" class=""><span style="font-size:11px;font-family:arial;color:rgb(17,85,204);font-weight:bold;text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">Remind.com</span></a><span style="font-family:arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""> </span><span style="font-size:11px;font-family:arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">| &nbsp;</span><a href="http://blog.remind.com/" style="text-decoration:none" target="_blank" class=""><span style="font-size:11px;font-family:arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">BLOG</span></a><span style="font-size:11px;font-family:arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""> &nbsp;| &nbsp;</span><a href="https://twitter.com/remindhq" style="text-decoration:none" target="_blank" class=""><span style="font-size:11px;font-family:arial;color:rgb(17,85,204);text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">FOLLOW US</span></a><span style="font-size:11px;font-family:arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""> &nbsp;| </span><span style="font-family:arial;vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class="">&nbsp;</span><span style="text-decoration:underline;font-size:11px;font-family:arial;color:rgb(17,85,204);vertical-align:baseline;white-space:pre-wrap;background-color:transparent" class=""><a href="https://www.facebook.com/remindhq" style="text-decoration:none" target="_blank" class="">LIKE US</a></span></div></span></div></div>
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>