What is Protocol Buffers?

Protocol buffers are Google’s language-neutral, platform-neutral, Extensible Mechanism for serializing structured data — think XML, but smaller, faster, And simpler. — Google Official Definition

In short, Protocol Buffers are a lightweight and efficient structured data exchange format that is language independent, platform independent, and extensible. To put it bluntly, “more powerful and future-oriented JSON”, we’ll take a look at Swift’s official implementation of Protobuf.

Swift Protobuf

From getting rid of floppy disks to getting rid of optical drives, from scrapping Flash to promoting HTML5, and now getting rid of the standard audio interface altogether, Apple has been so good at leading the way in technology that it can’t be left behind in future-ready data interchange formats, hence the Swift Protobuf.

Start experimenting

I would have liked to follow the official example, but this time I have a great example of both client and server that I can “do” and play with, including the Go language.

Clone ProtoBufExample locally

➜ git clone https://github.com/KyoheiG3/ProtobufExample.git
➜ cd ProtobufExampleCopy the code

Configuring the Client

➜ CD./ProtobufClient ➜ pod installCopy the code

Initialize the server

➜ CD./ProtobufServer ➜ swift build // Create a project file to edit ➜ Swift Package generate-Xcodeproj under XcodeCopy the code

Start the API

➜. /. Build/debug/APICopy the code

Configure and start the service with Go

➜ go get github.com/golang/protobuf/protoc-gen-go ➜ go run server/API. GoCopy the code
  • If you have to, firstdownloadInstall the Go locale and configure $GOPATH
    ➜ mkdir ~/go
    ➜ export GOPATH=~/go
    ➜ export PATH=$PATH:$GOPATH/binCopy the code

experience.proto

Install the protobuf

➜ brew install protobufCopy the code

Compile Protobuf with Swift

➜ CD./ProtobufServer ➜ swift build ➜ protoc --plugin=protoc-gen-swift=. Build /debug/protoc-gen-swift --swift_out=.. /protos --proto_path=.. /protos .. /protos/DataModel.protoCopy the code

At this point we can see the corresponding.pb.swift file just generated in the protos output directory.

/* * Generated by the protocol buffer compiler. * Source: DataModel.proto */

import Foundation
import SwiftProtobuf

public struct BookInfo: ProtobufGeneratedMessage {
  public var swiftClassName: String {return "BookInfo"}
  public var protoMessageName: String {return "BookInfo"}
  public var protoPackageName: String {return ""}
  public var jsonFieldNames: [String: Int] {return [
    "id": 1."title": 3."author": 2]},public var protoFieldNames: [String: Int] {return [
    "id": 1."title": 3."author": 2]},public var id: Int64 = 0

  public var title: String = ""

  public var author: String = ""

  public init() {}... . .if! keys.isEmpty {try visitor.visitMapField(fieldType: ProtobufMap<ProtobufString.ProtobufString>.self, value: keys, protoFieldNumber: 4, protoFieldName: "keys", jsonFieldName: "keys", swiftFieldName: "keys")}}public func _protoc_generated_isEqualTo(other: MyLibrary) -> Bool {
    ifid ! = other.id {return false}
    ifname ! = other.name {return false}
    ifbooks ! = other.books {return false}
    ifkeys ! = other.keys {return false}
    return true}}Copy the code

It also includes some JSON friendly compatibility, interested friends can play with their own.

Explore more

Google Protocol Buffers

Swift Protobuf

ProtobufExample – Github

Deep understanding ofProtoBuf

Use and principles of Google Protocol Buffer – IBM