Working js grpc generation (same as official output)
This commit is contained in:
		
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @@ -9,4 +9,4 @@ install: | |||||||
| .PHONY: test | .PHONY: test | ||||||
| test:	build | test:	build | ||||||
| 	cd examples/dummy && make | 	cd examples/dummy && make | ||||||
| 	cd examples/go-kit && make | 	cd examples/js-grpc && make | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								examples/js-grpc/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								examples/js-grpc/Makefile
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
							
								
								
									
										160
									
								
								examples/js-grpc/output/user/user_grpc_pb.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								examples/js-grpc/output/user/user_grpc_pb.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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); | ||||||
|  |  | ||||||
							
								
								
									
										66
									
								
								examples/js-grpc/protos/user.proto
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								examples/js-grpc/protos/user.proto
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  | } | ||||||
| @@ -1,56 +1,58 @@ | |||||||
|  | {{$Package:=.File.Package}} | ||||||
|  | {{$Service:=.Service}} | ||||||
|  |  | ||||||
| // GENERATED CODE -- DO NOT EDIT! | // GENERATED CODE -- DO NOT EDIT! | ||||||
|  |  | ||||||
| 'use strict'; | 'use strict'; | ||||||
| {{$file:=.File}}{{$service:=.Service}} |  | ||||||
|  |  | ||||||
| var {{$file.Package}}_pb = require('../{{$file.Package}}/{{$file.Package}}_pb.js'); |  | ||||||
| var grpc = require('grpc'); | 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) { | {{range .File.Service}} | ||||||
|     return base64.fromByteArray(req) | {{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) { | function deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}(buffer_arg) { | ||||||
|     return new Uint8Array(base64.toByteArray(b64)) |   return {{$Package}}_{{$Package}}_pb.{{.InputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); | ||||||
| } |  | ||||||
| {{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_{{$file.Package}}_{{.Name}}Request(base64) { | function serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(arg) { | ||||||
|     return {{$file.Package}}_pb.{{.Name}}Request.deserializeBinary(deserializeFromBase64(base64)); |   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) { | function deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(buffer_arg) { | ||||||
|     if (!(arg instanceof {{$file.Package}}_pb.{{.Name}}Reply)) { |   return {{$Package}}_{{$Package}}_pb.{{.OutputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg)); | ||||||
|         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)); |  | ||||||
| } | } | ||||||
| {{end}} | {{end}} | ||||||
| var TestServiceService = { | {{end}} | ||||||
|     {{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}} |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| exports.TestServiceClient = grpc.makeGenericClientConstructor({{.Service.Name}}Service); | 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}} | ||||||
|   | |||||||
							
								
								
									
										11
									
								
								helpers.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								helpers.go
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ package main | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
|  | 	"strings" | ||||||
| 	"text/template" | 	"text/template" | ||||||
|  |  | ||||||
| 	"github.com/Masterminds/sprig" | 	"github.com/Masterminds/sprig" | ||||||
| @@ -21,12 +22,18 @@ var ProtoHelpersFuncMap = template.FuncMap{ | |||||||
| 		a, _ := json.MarshalIndent(v, "", "  ") | 		a, _ := json.MarshalIndent(v, "", "  ") | ||||||
| 		return string(a) | 		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] | 		return a[0] | ||||||
| 	}, | 	}, | ||||||
| 	"last": func(a []interface{}) interface{} { | 	"last": func(a []string) string { | ||||||
| 		return a[len(a)-1] | 		return a[len(a)-1] | ||||||
| 	}, | 	}, | ||||||
|  | 	"lowerFirst": func(s string) string { | ||||||
|  | 		return strings.ToLower(s[:1]) + s[1:] | ||||||
|  | 	}, | ||||||
| } | } | ||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user