VaporPB
简便的将SwiftProtobuf集成进Vapor项目中
引用项目中
.package(url: "git@github.com:skeyboy/SwiftVaporPB.git", from: "0.0.1")
dependencies[..."SwiftVaporPB"]
使用
import SwiftVaporPB
router.get("pb") { (req:Request) -> EventLoopFuture<PB<BookInfo>> in
let bookInfo = BookInfo.with({ ( bookInfo:inout BookInfo) in
bookInfo.id = 123
bookInfo.author = "Jack"
bookInfo.title = "Hello"
})
return try req.makePB(value: bookInfo)
}
返回格式
struct PB<T> : Content where T: Message{
var value: Data
init(_ data: T) throws {
self.value = try data.serializedData()
}
}
从我们的定一种可以看出,我们只是进行一个简单的包装
结果参考:
tag:0.0.2
添加命名空间
func testPBBase(){
XCTAssert(pb?.pb.entry != nil, "测试失败")
XCTAssert(pb?.pb.textFormatString() == bookInfo?.textFormatString(), "测试失败")
XCTAssert(pb?.pb.textString == pb?.textString, "测试失败")
}
网友评论