之前写过namespace的问题不过后续都是没怎么使用,时下rx,snp…大家都应经不再陌生,也是比较常见的,今天我们结合struct 泛型 class一起看个综合的实例 通常我们使用命名空间都是基于一个具体的实例进行的二次封装(大家公认的)而封装的载体通常是struct,然后对struct进行extension 背景: 最经在搞protocol buffer来进行数据压缩,减轻网络流量,于是有了今天的文章
结构定义 在vapor中我们想简单的返回pb数据不是那么简答,因此对返回数据进行了一个包装
1 返回数据格式
public struct PB<T> : Content where T: Message{
var value: Data
init(_ data: T) throws {
self.value = try data.serializedData()
}
}
复制代码
2 request
extension Request{
public func makePB<T>(value: T) throws -> EventLoopFuture<PB<T>> where T: Message{
let result = self.eventLoop.newPromise(PB<T>.self)
let pb = try PB.init(value)
result.succeed(result: pb)
return result.futureResult
}
}
复制代码
有定义看到返回的是个基于Message的泛型struct,我们暂时放置于此,后续使用
namespace包装 1 格式定义
class PBBase<Base, T> where T: Message{
let base: PB<T>
init(_ base: Base) {
self.base = base as! PB<T>
}
}
复制代码
2 协议定义
protocol PBProtocol {
associatedtype PBType
associatedtype T: Message
var pb:PBType{get}
}
extension PBProtocol{
public var pb:PBBase<Self, T>{
return PBBase.init(self)
}
}
复制代码
完成
extension PB : PBProtocol where T: Message{}
复制代码
使用
func testPBBase(){
XCTAssert(pb?.pb.entry != nil, "测试失败")
XCTAssert(pb?.pb.textFormatString() == bookInfo?.textFormatString(), "测试失败")
XCTAssert(pb?.pb.textString == pb?.textString, "测试失败")
}
复制代码
我们再来回顾一下整个过程使用了什么 1 使用 where限定了PBProtocol的使用范围
2 protocol中associatedtype的综合使用
2.1 可以定义多个
2.2 可以定义一个具体的类型,也可定义个模糊的类型
复制代码
3 nameapce的本质是二次封装