Created
January 7, 2021 10:05
-
-
Save yanfeng42/6440b6b7533d91602e73ce9ef2f7352a to your computer and use it in GitHub Desktop.
Swift 基于 Codable 来解决 grpc 自动将 int64 转为 String 带来的不便.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class SampleReply: Codable { | |
let code: Int32 // 状态码 | |
private let _pageToken: String | |
lazy var pageToken: Int64 = { return Int64(self._pageToken) ?? 0 }() | |
private enum CodingKeys: String, CodingKey { | |
case code | |
case _pageToken = "pageToken" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
相关讨论: protocolbuffers/protobuf#1823
简单说: 如果 Server 基于 grpc, 最终封装为 restful 接口对前端暴露时, Int64 为变为 String.
如果我是 Server, 我会扩展一个 自定义的 grpc -> json 的转换器. 并且支持 restful 接口访问者自己指定 是需要原始的
Int64
还是 需要包装为String
. (显而易见的东西, Server 拖了好几年, 可能真的有什么难度或者限制吧.)不过, 从 App 端, 也可以略微调整, 来解决这个问题.--- 不过,并不友好. 因为我这种写法, 需要明确列出所有的
CodingKeys
.当 Model 字端很多时, 写
CodingKeys
会更烦.结论就是: 我知道怎么从 App 端来兼容/解决这个问题, 当时我并不打算大范围地在App端兼容这个问题.