From abbd30d34c803c553c0cb6a6eb2ba813f16ee516 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Fri, 16 Dec 2016 16:08:37 +0100 Subject: [PATCH] Working js grpc generation (same as official output) --- Makefile | 2 +- examples/js-grpc/Makefile | 13 ++ examples/js-grpc/output/user/user_grpc_pb.js | 160 ++++++++++++++++++ examples/js-grpc/protos/user.proto | 66 ++++++++ .../{{.File.Package}}_grpc_pb.js.tmpl | 84 ++++----- helpers.go | 11 +- 6 files changed, 292 insertions(+), 44 deletions(-) create mode 100644 examples/js-grpc/Makefile create mode 100644 examples/js-grpc/output/user/user_grpc_pb.js create mode 100644 examples/js-grpc/protos/user.proto diff --git a/Makefile b/Makefile index ba9e627..c68adf7 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,4 @@ install: .PHONY: test test: build cd examples/dummy && make - cd examples/go-kit && make + cd examples/js-grpc && make diff --git a/examples/js-grpc/Makefile b/examples/js-grpc/Makefile new file mode 100644 index 0000000..2d37994 --- /dev/null +++ b/examples/js-grpc/Makefile @@ -0,0 +1,13 @@ +.PHONY: build +build: + mkdir -p output + protoc -I. --gotemplate_out=template_dir=templates,debug=true:output ./protos/*.proto + + +.PHONY: re +re: clean build + + +.PHONY: clean +clean: + rm -rf output diff --git a/examples/js-grpc/output/user/user_grpc_pb.js b/examples/js-grpc/output/user/user_grpc_pb.js new file mode 100644 index 0000000..46f3240 --- /dev/null +++ b/examples/js-grpc/output/user/user_grpc_pb.js @@ -0,0 +1,160 @@ + + + +// GENERATED CODE -- DO NOT EDIT! + +'use strict'; +var grpc = require('grpc'); +var user_user_pb = require('../user/user_pb.js'); +var google_api_annotations_pb = require('../google/api/annotations_pb.js'); + + + +function serialize_user_RegisterRequest(arg) { + if (!(arg instanceof user_user_pb.RegisterRequest)) { + throw new Error('Expected argument of type user.RegisterRequest'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_RegisterRequest(buffer_arg) { + return user_user_pb.RegisterRequest.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_RegisterReply(arg) { + if (!(arg instanceof user_user_pb.RegisterReply)) { + throw new Error('Expected argument of type user.RegisterReply'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_RegisterReply(buffer_arg) { + return user_user_pb.RegisterReply.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_GetUserProfileRequest(arg) { + if (!(arg instanceof user_user_pb.GetUserProfileRequest)) { + throw new Error('Expected argument of type user.GetUserProfileRequest'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_GetUserProfileRequest(buffer_arg) { + return user_user_pb.GetUserProfileRequest.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_GetUserProfileReply(arg) { + if (!(arg instanceof user_user_pb.GetUserProfileReply)) { + throw new Error('Expected argument of type user.GetUserProfileReply'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_GetUserProfileReply(buffer_arg) { + return user_user_pb.GetUserProfileReply.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_UpdateProfileRequest(arg) { + if (!(arg instanceof user_user_pb.UpdateProfileRequest)) { + throw new Error('Expected argument of type user.UpdateProfileRequest'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_UpdateProfileRequest(buffer_arg) { + return user_user_pb.UpdateProfileRequest.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_UpdateProfileReply(arg) { + if (!(arg instanceof user_user_pb.UpdateProfileReply)) { + throw new Error('Expected argument of type user.UpdateProfileReply'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_UpdateProfileReply(buffer_arg) { + return user_user_pb.UpdateProfileReply.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_UploadDocumentRequest(arg) { + if (!(arg instanceof user_user_pb.UploadDocumentRequest)) { + throw new Error('Expected argument of type user.UploadDocumentRequest'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_UploadDocumentRequest(buffer_arg) { + return user_user_pb.UploadDocumentRequest.deserializeBinary(new Uint8Array(buffer_arg)); +} + +function serialize_user_UploadDocumentReply(arg) { + if (!(arg instanceof user_user_pb.UploadDocumentReply)) { + throw new Error('Expected argument of type user.UploadDocumentReply'); + } + return new Buffer(arg.serializeBinary()); +} + +function deserialize_user_UploadDocumentReply(buffer_arg) { + return user_user_pb.UploadDocumentReply.deserializeBinary(new Uint8Array(buffer_arg)); +} + + + +exports.TestServiceClient = grpc.makeGenericClientConstructor(UserServiceService); + + +var UserServiceService = exports.UserServiceService = { + + + register: { + path: '/user.UserService/Register', + requestStream: false, + responseStream: false, + requestType: user_user_pb.RegisterRequest, + responseType: user_user_pb.RegisterReply, + requestSerialize: serialize_user_RegisterRequest, + requestDeserialize: deserialize_user_RegisterRequest, + responseSerialize: serialize_user_RegisterReply, + responseDeserialize: deserialize_user_RegisterReply, + }, + + getUserProfile: { + path: '/user.UserService/GetUserProfile', + requestStream: false, + responseStream: false, + requestType: user_user_pb.GetUserProfileRequest, + responseType: user_user_pb.GetUserProfileReply, + requestSerialize: serialize_user_GetUserProfileRequest, + requestDeserialize: deserialize_user_GetUserProfileRequest, + responseSerialize: serialize_user_GetUserProfileReply, + responseDeserialize: deserialize_user_GetUserProfileReply, + }, + + updateProfile: { + path: '/user.UserService/UpdateProfile', + requestStream: false, + responseStream: false, + requestType: user_user_pb.UpdateProfileRequest, + responseType: user_user_pb.UpdateProfileReply, + requestSerialize: serialize_user_UpdateProfileRequest, + requestDeserialize: deserialize_user_UpdateProfileRequest, + responseSerialize: serialize_user_UpdateProfileReply, + responseDeserialize: deserialize_user_UpdateProfileReply, + }, + + uploadDocument: { + path: '/user.UserService/UploadDocument', + requestStream: false, + responseStream: false, + requestType: user_user_pb.UploadDocumentRequest, + responseType: user_user_pb.UploadDocumentReply, + requestSerialize: serialize_user_UploadDocumentRequest, + requestDeserialize: deserialize_user_UploadDocumentRequest, + responseSerialize: serialize_user_UploadDocumentReply, + responseDeserialize: deserialize_user_UploadDocumentReply, + }, + +} + +exports.UserServiceClient = grpc.makeGenericClientConstructor(UserServiceService); + diff --git a/examples/js-grpc/protos/user.proto b/examples/js-grpc/protos/user.proto new file mode 100644 index 0000000..96e70db --- /dev/null +++ b/examples/js-grpc/protos/user.proto @@ -0,0 +1,66 @@ +syntax = "proto3"; + +package user; + +option go_package = "github.com/united-drivers/models/go/user;userpb"; + +// import "google/api/annotations.proto"; + +// service UserService { +// rpc Register(RegisterRequest) returns (RegisterReply) {option (google.api.http).post = "/user/register";} +// rpc GetUserProfile(GetUserProfileRequest) returns (GetUserProfileReply) {option (google.api.http).post = "/user/get-user-profile";} +// rpc UpdateProfile(UpdateProfileRequest) returns (UpdateProfileReply) {option (google.api.http).post = "/user/update-profile";} +// rpc UploadDocument(UploadDocumentRequest) returns (UploadDocumentReply) {option (google.api.http).post = "/user/upload-document";} +// } + +service UserService { + rpc Register(RegisterRequest) returns (RegisterReply); + rpc GetUserProfile(GetUserProfileRequest) returns (GetUserProfileReply); + rpc UpdateProfile(UpdateProfileRequest) returns (UpdateProfileReply); + rpc UploadDocument(UploadDocumentRequest) returns (UploadDocumentReply); +} + +message UpdateProfileRequest { + Profile profile = 1; +} + +message UpdateProfileReply { + string err_msg = 1; +} + +message UploadDocumentRequest { + string path = 1; + string document = 2; +} + +message UploadDocumentReply { + string err_msg = 1; +} + +message Profile { + string id = 1; + string email = 2; + string first_name = 3; + string last_name = 4; + string picture = 5; + string promo_code = 6; + string phone_number = 7; +} + +message RegisterRequest { + string first_name = 1; + string last_name = 2; + string email = 3; + string phone_number = 4; +} + +message RegisterReply { + string err_msg = 1; +} + +message GetUserProfileRequest {} + +message GetUserProfileReply { + Profile profile = 1; + string err_msg = 2; +} diff --git a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_pb.js.tmpl b/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_pb.js.tmpl index e5bd06e..e3099e4 100644 --- a/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_pb.js.tmpl +++ b/examples/js-grpc/templates/{{.File.Package}}/{{.File.Package}}_grpc_pb.js.tmpl @@ -1,56 +1,58 @@ +{{$Package:=.File.Package}} +{{$Service:=.Service}} + // GENERATED CODE -- DO NOT EDIT! 'use strict'; -{{$file:=.File}}{{$service:=.Service}} - -var {{$file.Package}}_pb = require('../{{$file.Package}}/{{$file.Package}}_pb.js'); var grpc = require('grpc'); -var base64 = require('base64-js'); +var {{$Package}}_{{$Package}}_pb = require('../{{$Package}}/{{$Package}}_pb.js'); +var google_api_annotations_pb = require('../google/api/annotations_pb.js'); -function serializeToBase64(req) { - return base64.fromByteArray(req) +{{range .File.Service}} +{{range .Method}} +function serialize_{{$Package}}_{{.InputType | splitArray "." | last}}(arg) { + if (!(arg instanceof {{$Package}}_{{$Package}}_pb.{{.InputType | splitArray "." | last}})) { + throw new Error('Expected argument of type {{$Package}}.{{.InputType | splitArray "." | last}}'); + } + return new Buffer(arg.serializeBinary()); } -function deserializeFromBase64(b64) { - return new Uint8Array(base64.toByteArray(b64)) -} -{{range .Service.Method}} -function serialize_{{$file.Package}}_{{.Name}}Request(arg) { - if (!(arg instanceof {{$file.Package}}_pb.{{.Name}}Request)) { - throw new Error('Expected argument of type {{$file.Package}}.{{.Name}}Request'); - } - return serializeToBase64(arg.serializeBinary()); +function deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}(buffer_arg) { + return {{$Package}}_{{$Package}}_pb.{{.InputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); } -function deserialize_{{$file.Package}}_{{.Name}}Request(base64) { - return {{$file.Package}}_pb.{{.Name}}Request.deserializeBinary(deserializeFromBase64(base64)); +function serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(arg) { + if (!(arg instanceof {{$Package}}_{{$Package}}_pb.{{.OutputType | splitArray "." | last}})) { + throw new Error('Expected argument of type {{$Package}}.{{.OutputType | splitArray "." | last}}'); + } + return new Buffer(arg.serializeBinary()); } -function serialize_{{$file.Package}}_{{.Name}}Reply(arg) { - if (!(arg instanceof {{$file.Package}}_pb.{{.Name}}Reply)) { - throw new Error('Expected argument of type {{$file.Package}}.{{.Name}}Reply'); - } - return serializeToBase64(arg.serializeBinary()); -} - -function deserialize_{{$file.Package}}_{{.Name}}Reply(base64) { - return {{$file.Package}}_pb.{{.Name}}Reply.deserializeBinary(deserializeFromBase64(base64)); +function deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(buffer_arg) { + return {{$Package}}_{{$Package}}_pb.{{.OutputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); } {{end}} -var TestServiceService = { - {{range .Service.Method}} - {{.Name}}: { - path: '/{{$file.Package}}.{{$service.Name}}/{{.Name}}', - requestStream: {{if .ClientStreaming}}true{{else}}false{{end}}, - responseStream: {{if .ServerStreaming}}true{{else}}false{{end}}, - requestType: {{$file.Package}}_pb.{{.Name}}Request, - responseType: {{$file.Package}}_pb.{{.Name}}Reply, - requestSerialize: serialize_{{$file.Package}}_{{.Name}}Request, - requestDeserialize: deserialize_{{$file.Package}}_{{.Name}}Request, - responseSerialize: serialize_{{$file.Package}}_{{.Name}}Reply, - responseDeserialize: deserialize_{{$file.Package}}_{{.Name}}Reply, - }, - {{end}} -}; +{{end}} exports.TestServiceClient = grpc.makeGenericClientConstructor({{.Service.Name}}Service); + +{{range .File.Service}} +var {{.Name}}Service = exports.{{.Name}}Service = { +{{$serviceName:=.Name}} +{{range .Method}} + {{.Name | lowerFirst}}: { + path: '/{{$Package}}.{{$serviceName}}/{{.Name}}', + requestStream: {{.ClientStreaming | default "false"}}, + responseStream: {{.ServerStreaming | default "false"}}, + requestType: {{$Package}}_{{$Package}}_pb.{{.InputType | splitArray "." | last}}, + responseType: {{$Package}}_{{$Package}}_pb.{{.OutputType | splitArray "." | last}}, + requestSerialize: serialize_{{$Package}}_{{.InputType | splitArray "." | last}}, + requestDeserialize: deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}, + responseSerialize: serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}, + responseDeserialize: deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}, + }, +{{end}} +} + +exports.{{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service); +{{end}} diff --git a/helpers.go b/helpers.go index 3492b04..320b912 100644 --- a/helpers.go +++ b/helpers.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "strings" "text/template" "github.com/Masterminds/sprig" @@ -21,12 +22,18 @@ var ProtoHelpersFuncMap = template.FuncMap{ a, _ := json.MarshalIndent(v, "", " ") return string(a) }, - "first": func(a []interface{}) interface{} { + "splitArray": func(sep string, s string) []string { + return strings.Split(s, sep) + }, + "first": func(a []string) string { return a[0] }, - "last": func(a []interface{}) interface{} { + "last": func(a []string) string { return a[len(a)-1] }, + "lowerFirst": func(s string) string { + return strings.ToLower(s[:1]) + s[1:] + }, } func init() {