在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):alexeyxo/protobuf-swift开源软件地址(OpenSource Url):https://github.com/alexeyxo/protobuf-swift开源编程语言(OpenSource Language):Swift 97.2%开源软件介绍(OpenSource Introduction):Protocol Buffers for SwiftAn implementation of Protocol Buffers in Swift. Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. This project is based on an implementation of Protocol Buffers from Google. See the Google protobuf project for more information. Required Protocol Buffers 3.0How To Install Protobuf Compiler on Linux(Ubuntu 14.04)1. 2. 3. 4. 5. 6. 7. 8. 9. How To Install Protobuf Compiler from Homebrew1. 2. How To Install Protobuf Compiler1. 2. 3. 4. 5. 6. Add CocoapodsPodfile:
Installation via CarthageCartfile:
Compile ".proto" files.protoc person.proto --swift_out="./" Serializingsyntax = "proto2";
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
} let personBuilder = Person.Builder()
personBuilder.id = 123
personBuilder.name = "Bob"
personBuilder.email = "[email protected]"
let person = try! personBuilder.build()
print(person)
person.data() //return NSData Chainingsyntax = "proto2";
message Perfomance
{
required int32 ints = 1;
required int64 ints64 = 2;
required double doubles = 3;
required float floats = 4;
optional string str = 5;
optional bytes bytes = 6;
optional string description = 7;
} var originalBuilder = ProtoPerfomance.Builder()
originalBuilder.setInts(Int32(32))
.setInts64(Int64(64))
.setDoubles(Double(12.12))
.setFloats(Float(123.123))
.setStr("string")
let original = originalBuilder.build() Sub Builderssyntax = "proto2";
message Foo {
optional int32 val = 1;
// some other fields.
}
message Bar {
optional Foo foo = 1;
// some other fields.
}
message Baz {
optional Bar bar = 1;
// some other fields.
} var builder = baz.toBuilder()
builder.getBarBuilder().getFooBuilder().setVal(10)
baz = builder.build() Maps(ProtocolBuffers 3.0)syntax = "proto3";
message MapMessageValue
{
int32 valueInMapMessage = 1;
}
message MessageContainsMap
{
enum EnumMapValue
{
FirstValueEnum = 0;
SecondValueEnum = 1;
}
map<int32,int32> map_int32_int32= 1;
map<int64,int64> map_int64_int64= 2;
map<string,string> map_string_string = 3;
map<string,bytes> map_string_bytes = 4;
map<string,MapMessageValue> map_string_message = 5;
map<int32,EnumMapValue> map_int32_enum = 6;
} final internal class MessageContainsMap : GeneratedMessage, GeneratedMessageProtocol, Hashable {
...
private(set) var mapInt32Int32:Dictionary<Int32,Int32> = Dictionary<Int32,Int32>()
private(set) var mapInt64Int64:Dictionary<Int64,Int64> = Dictionary<Int64,Int64>()
private(set) var mapStringString:Dictionary<String,String> = Dictionary<String,String>()
private(set) var mapStringBytes:Dictionary<String,NSData> = Dictionary<String,NSData>()
private(set) var mapInt32Enum:Dictionary<Int32,MessageContainsMap.EnumMapValue> = Dictionary<Int32,MessageContainsMap.EnumMapValue>()
...
} JSON(proto3)let personBuilder = Person.builder()
personBuilder.id = 123
personBuilder.name = "Bob"
personBuilder.email = "[email protected]"
let person = personBuilder.build()
let jsonData = person.toJSON() //return NSData
let jsonDictionaryObject:Dictionary<String,AnyObject> = person.encode()
let personFromJson = Person.fromJSON(jsonData) //Person Deserializingvar person = Person.parseFromData(bytes) // from NSData Using Oneofsyntax = "proto3";
message SubMessage {
string str = 1;
}
message SampleMessage {
oneof test_oneof {
string name = 4;
int32 id = 5;
SubMessage mes = 6;
}
} var sm = SampleMessage.Builder()
sm.name = "Alex"
sm.id = 123
println(ss.build()) //-> id: 123 Nested Typessyntax = "proto3";
message SearchResponse {
message Result {
string url = 1;
string title = 2;
repeated string snippets = 3;
}
repeated Result result = 1;
} var builderResult = SearchResponse.Result.Builder()
builderResult.url = "http://protobuf.axo.io"
builderResult.title = "Protocol Bufers Apple Swift"
var searchRespons = SearchResponse.builder()
searchRespons.result += [builderResult.build()]
println(searchRespons.build()) Packagessyntax = "proto2";
package FooBar;
message Perfomance
{
required int32 ints = 1;
required int64 ints64 = 2;
required double doubles = 3;
required float floats = 4;
optional string str = 5;
optional bytes bytes = 6;
optional string description = 7;
} public extension FooBar {
...
final public class Perfomance : GeneratedMessage, GeneratedMessageProtocol {
...
}
} Custom Optionsimport "google/protobuf/descriptor.proto";
package google.protobuf;
enum AccessControl {
InternalEntities = 0;
PublicEntities = 1;
}
message SwiftFileOptions {
optional string class_prefix = 1;
optional AccessControl entities_access_control = 2 [default = PublicEntities];
optional bool compile_for_framework = 3 [default = true];
}
message SwiftMessageOptions {
optional bool generate_error_type = 1 [default = false];
}
message SwiftEnumOptions {
optional bool generate_error_type = 1 [default = false];
}
extend google.protobuf.FileOptions {
optional SwiftFileOptions swift_file_options = 5092014;
}
extend google.protobuf.MessageOptions {
optional SwiftMessageOptions swift_message_options = 5092014;
}
extend google.protobuf.EnumOptions {
optional SwiftEnumOptions swift_enum_options = 5092015;
}
option (.google.protobuf.swift_file_options).compile_for_framework = false;
option (.google.protobuf.swift_file_options).entities_access_control = PublicEntities; At now protobuf-swift's compiler is supporting custom options.
If you have use custom options, you need to add: import 'google/protobuf/swift-descriptor.proto'; in your Class prefixThis option needs to generate class names with prefix. Example: import 'google/protobuf/swift-descriptor.proto';
option (.google.protobuf.swift_file_options).class_prefix = "Proto";
message NameWithPrefix
{
optional string str = 1;
} Generated class has a name: final internal class ProtoNameWithPrefix : GeneratedMessage Access controloption (.google.protobuf.swift_file_options).entities_access_control = PublicEntities; All generated classes marks as option (.google.protobuf.swift_file_options).entities_access_control = PublicEntities;
message MessageWithCustomOption
{
optional string str = 1;
} Generated class and all fields are marked a final public class MessageWithCustomOption : GeneratedMessage Generate enum/message conforming to "Error" protocoloption (.google.protobuf.swift_enum_options).generate_error_type = true; Exampleimport 'google/protobuf/swift-descriptor.proto';
enum ServiceError {
option (.google.protobuf.swift_enum_options).generate_error_type = true;
BadRequest = 0;
InternalServerError = 1;
}
message UserProfile {
message Request {
required string userId = 1;
}
message Response {
optional UserProfile profile = 1;
optional ServiceError error = 2;
optional Exception exception = 3;
}
message Exception {
option (.google.protobuf.swift_message_options).generate_error_type = true;
required int32 errorCode = 1;
required string errorDescription = 2;
}
optional string firstName = 1;
optional string lastName = 2;
optional string avatarUrl = 3;
} public enum ServiceError:Error, RawRepresentable, CustomDebugStringConvertible, CustomStringConvertible {
public typealias RawValue = Int32
case badRequest
case internalServerError
public init?(rawValue: RawValue) {
switch rawValue {
case 0: self = .badRequest
case 1: self = .internalServerError
default: return nil
}
}
public var rawValue: RawValue {
switch self {
case .badRequest: return 0
case .internalServerError: return 1
}
}
public func throwException() throws {
throw self
}
public var debugDescription:String { return getDescription() }
public var description:String { return getDescription() }
private func getDescription() -> String {
switch self {
case .badRequest: return ".badRequest"
case .internalServerError: return ".internalServerError"
}
}
} func generateException()throws {
let user = UserProfile.Response.Builder()
user.error = .internalServerError
let data = try user.build().data()
let userError = try UserProfile.Response.parseFrom(data:data)
if userError.hasError {
throw userError.error //userError.error.throwException()
}
}
do {
try generateException()
} catch let err as ServiceError where err == .internalServerError {
XCTAssertTrue(true)
} catch {
XCTAssertTrue(false)
}
func throwExceptionMessage() throws {
let exception = UserProfile.Exception.Builder()
exception.errorCode = 403
exception.errorDescription = "Bad Request"
let exc = try exception.build()
let data = try UserProfile.Response.Builder().setException(exc).build().data()
let userError = try UserProfile.Response.parseFrom(data:data)
if userError.hasException {
throw userError.exception
}
}
do {
try throwExceptionMessage()
} catch let err as UserProfile.Exception {
print(err)
XCTAssertTrue(true)
} catch {
XCTAssertTrue(false)
}
Compile for frameworkoption (.google.protobuf.swift_file_options).compile_for_framework = false; This option deletes the string If you will need some other options, write me. I will add them.Utilities (ProtocolBuffers 3.0)Added well-known type protos (any.proto, empty.proto, timestamp.proto, duration.proto, etc.). Users can import and use these protos just like regular proto files. Addtional runtime support will be added for them in future releases (in the form of utility helper functions, or having them replaced by language specific types in generated code).
全部评论
专题导读
上一篇:kelseyhightower/vault-on-google-kubernetes-engine: How to guide on running Hashi ...发布时间:2022-06-12下一篇:googleads/googleads-consent-sdk-android: Consent SDK发布时间:2022-06-12热门推荐
热门话题
阅读排行榜
|
请发表评论