How to generate grpc protobuf files

Issue #197

protoc

https://grpc.io/docs/quickstart/go.html

Install the protoc compiler that is used to generate gRPC service code. The simplest way to do this is to download pre-compiled binaries for your platform(protoc--.zip) from here: https://github.com/google/protobuf/releases

Unzip this file.
Update the environment variable PATH to include the path to the protoc binary file.

Go protoc plugin

https://github.com/golang/protobuf

1
2
3
go get -u github.com/golang/protobuf/protoc-gen-go
export PATH=$PATH:$GOPATH/bin
source ~/.zshrc

Swift protoc plugin

https://github.com/grpc/grpc-swift

The recommended way to use Swift gRPC is to first define an API using the
Protocol Buffer
language and then use the
Protocol Buffer Compiler
and the Swift Protobuf
and Swift gRPC plugins to
generate the necessary support code.

1
2
3
4
git clone https://github.com/grpc/grpc-swift.git
cd grpc-swift
make
sudo cp protoc-gen-swift protoc-gen-swiftgrpc /usr/local/bin

Generate

1
protoc --swift_out=MyApp/Api --swiftgrpc_out=Client=true,Server=false:MyApp/Api --go_out=plugins=grpc:server/api api.proto

In case we need to cd

1
2
3
cd MyApp/Library/Models/Proto

protoc --swift_out=../Generated --swiftgrpc_out=Client=true,Server=false:../Generated api.proto

Empty

If remote import is needed, then the workaround is to download the that proto locally, for example empty.proto https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/empty.proto

Inside SwiftProtobuf pod, there is generated empty.pb.swift

1
2
3
4
5
6
7
8
9
public struct Google_Protobuf_Empty {
// SwiftProtobuf.Message conformance is added in an extension below. See the
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
// methods supported on all messages.

public var unknownFields = SwiftProtobuf.UnknownStorage()

public init() {}
}

To consume, we can

1
2
3
import SwiftProtobuf

let empty = Google_Protobuf_Empty()

oneof mode

1
2
3
4
5
6
message Person {
oneof mode {
Human human = 1;
Superman superman = 2;
}
}

Cannot convert value of type ‘Server_Person.OneOf_Mode’ to expected argument type ‘Server_Human’

Need to assign the mode

1
2
var person = Person()
person.mode = Person.OneOf_Mode.Human()

Comments