gRPC で日付型を利用する
(=˘ ꒳ ˘=) gRPC の import 周りを整理したい...
という記事で go-proto-validators と grpc_cli をうまく組み合わせられないことに対して試行錯誤してました。
その問題の切り分けがてら、今回はもう少し基本的な日付型の利用を通して gRPC の import 周りを整理していきたいと思います。
- プロジェクト: https://github.com/asa-taka/hello-validated-grpc
- 日付型を組み込んだコミット: https://github.com/asa-taka/hello-validated-grpc/commit/4d3d2251f5da298f6d64534b0aa7fdc29da63914
hello.proto をベースに日付型をレスポンスのタイムスタンプとして追加しています。
syntax = "proto3"; package hello; import "google/protobuf/timestamp.proto"; service GreetingService { rpc Hello(GreetingRequest) returns (GreetingResponse); } message GreetingRequest { string name = 1; } message GreetingResponse { string message = 1; google.protobuf.Timestamp date = 2; }
注意点としては Timestamp
ではなくて google.protobuf.Timestamp
と指定するところくらいです。
サーバ側の実装は今回も Go で、日付型の変換に https://godoc.org/github.com/golang/protobuf/ptypes#TimestampProto を利用しています。
Go の time
パッケージの日付型はそのままだと gRPC 用に吐き出されたコードには利用できません。
詳しい実装は冒頭のコミットのリンクをご参照ください。
で、リフレクションサービスは組み込み済みで肝心の grpc_cli の動作確認ですが...
$ grpc ls -l localhost:10000 filename: grpc_reflection_v1alpha/reflection.proto package: grpc.reflection.v1alpha; service ServerReflection { rpc ServerReflectionInfo(stream grpc.reflection.v1alpha.ServerReflectionRequest) returns (stream grpc.reflection.v1alpha.ServerReflectionResponse) {} } filename: api/hello.proto package: hello; service GreetingService { rpc Hello(hello.GreetingRequest) returns (hello.GreetingResponse) {} } $ grpc call localhost:10000 Hello "name: 'asa-taka'" connecting to localhost:10000 message: "Hello, asa-taka." date { seconds: 1527964860 nanos: 432266000 } Rpc succeeded with OK status
なんか普通に動いてしまって拍子抜けです。
import パス周り、意外と単純なのか根が深いのか... gRPC と Protocol Buffer 難しい...