From fcc2520451f65d8a32d1ef2bb8d357c857561338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A9sar=20de=20Oliveira?= Date: Wed, 15 May 2019 09:19:11 -0300 Subject: [PATCH 1/5] Allow to customize service interface name --- generator/add_transport.go | 12 ++++++------ generator/generate_client.go | 6 +++--- generator/generate_middleware.go | 2 +- generator/generate_service.go | 12 ++++++------ generator/new_service.go | 2 +- main.go | 1 + utils/utils.go | 9 +++++++++ 7 files changed, 27 insertions(+), 17 deletions(-) diff --git a/generator/add_transport.go b/generator/add_transport.go index 42b58c7..ba7c7db 100644 --- a/generator/add_transport.go +++ b/generator/add_transport.go @@ -44,7 +44,7 @@ func NewGenerateTransport(name string, gorillaMux bool, transport string, method i := &GenerateTransport{ name: name, gorillaMux: gorillaMux, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(name)), methods: methods, } @@ -196,7 +196,7 @@ func newGenerateHTTPTransport(name string, gorillaMux bool, serviceInterface par t := &generateHTTPTransport{ name: name, methods: methods, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_http_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, gorillaMux: gorillaMux, @@ -536,7 +536,7 @@ func newGenerateHTTPTransportBase(name string, gorillaMux bool, serviceInterface methods: methods, gorillaMux: gorillaMux, allMethods: allMethods, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_http_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, } @@ -647,7 +647,7 @@ func newGenerateGRPCTransportProto(name string, serviceInterface parser.Interfac t := &generateGRPCTransportProto{ name: name, methods: methods, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_grpc_pb_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, } @@ -870,7 +870,7 @@ func newGenerateGRPCTransportBase(name string, serviceInterface parser.Interface name: name, methods: methods, allMethods: allMethods, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_grpc_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, } @@ -973,7 +973,7 @@ func newGenerateGRPCTransport(name string, serviceInterface parser.Interface, me t := &generateGRPCTransport{ name: name, methods: methods, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_grpc_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, } diff --git a/generator/generate_client.go b/generator/generate_client.go index 0acb283..2179222 100644 --- a/generator/generate_client.go +++ b/generator/generate_client.go @@ -33,7 +33,7 @@ type GenerateClient struct { func NewGenerateClient(name string, transport string) Gen { i := &GenerateClient{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_client_cmd_path_format"), utils.ToLowerSnakeCase(name)), serviceDestPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(name)), transport: transport, @@ -143,7 +143,7 @@ type generateHTTPClient struct { func newGenerateHTTPClient(name string, serviceInterface parser.Interface, serviceFile *parser.File) Gen { i := &generateHTTPClient{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_http_client_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, serviceFile: serviceFile, @@ -347,7 +347,7 @@ type generateGRPCClient struct { func newGenerateGRPCClient(name string, serviceInterface parser.Interface, serviceFile *parser.File) Gen { i := &generateGRPCClient{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_grpc_client_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, serviceFile: serviceFile, diff --git a/generator/generate_middleware.go b/generator/generate_middleware.go index 0d1bf66..6d24ea6 100644 --- a/generator/generate_middleware.go +++ b/generator/generate_middleware.go @@ -37,7 +37,7 @@ func NewGenerateMiddleware(name, serviceName string, ep bool) Gen { name: name, serviceName: serviceName, isEndpointMiddleware: ep, - interfaceName: utils.ToCamelCase(serviceName + "Service"), + interfaceName: utils.GetServiceInterfaceName(serviceName), destPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(serviceName)), } i.filePath = path.Join(i.destPath, viper.GetString("gk_service_file_name")) diff --git a/generator/generate_service.go b/generator/generate_service.go index cc0db5e..8c8e9f9 100644 --- a/generator/generate_service.go +++ b/generator/generate_service.go @@ -36,7 +36,7 @@ type GenerateService struct { func NewGenerateService(name, transport string, sMiddleware, gorillaMux, eMiddleware bool, methods []string) Gen { i := &GenerateService{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(name)), sMiddleware: sMiddleware, eMiddleware: eMiddleware, @@ -285,7 +285,7 @@ func newGenerateServiceMiddleware(name string, serviceFile *parser.File, serviceInterface parser.Interface, generateDefaults bool) Gen { gsm := &generateServiceMiddleware{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, serviceFile: serviceFile, @@ -499,7 +499,7 @@ func newGenerateServiceEndpoints(name string, imports []parser.NamedTypeValue, serviceInterface parser.Interface, generateDefaults bool) Gen { gsm := &generateServiceEndpoints{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_endpoint_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, serviceImports: imports, @@ -895,7 +895,7 @@ type generateServiceEndpointsBase struct { func newGenerateServiceEndpointsBase(name string, serviceInterface parser.Interface) Gen { gsm := &generateServiceEndpointsBase{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_endpoint_path_format"), utils.ToLowerSnakeCase(name)), serviceInterface: serviceInterface, } @@ -980,7 +980,7 @@ type generateEndpointMiddleware struct { func newGenerateEndpointMiddleware(name string) Gen { gsm := &generateEndpointMiddleware{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_endpoint_path_format"), utils.ToLowerSnakeCase(name)), } gsm.filePath = path.Join(gsm.destPath, viper.GetString("gk_endpoint_middleware_file_name")) @@ -1490,7 +1490,7 @@ func newGenerateCmd(name string, serviceInterface parser.Interface, t := &generateCmd{ name: name, methods: methods, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_cmd_service_path_format"), utils.ToLowerSnakeCase(name)), httpDestPath: fmt.Sprintf(viper.GetString("gk_http_path_format"), utils.ToLowerSnakeCase(name)), grpcDestPath: fmt.Sprintf(viper.GetString("gk_grpc_path_format"), utils.ToLowerSnakeCase(name)), diff --git a/generator/new_service.go b/generator/new_service.go index 37510da..bbdbe67 100644 --- a/generator/new_service.go +++ b/generator/new_service.go @@ -27,7 +27,7 @@ type NewService struct { func NewNewService(name string) Gen { gs := &NewService{ name: name, - interfaceName: utils.ToCamelCase(name + "Service"), + interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_service_path_format"), utils.ToLowerSnakeCase(name)), } gs.filePath = path.Join(gs.destPath, viper.GetString("gk_service_file_name")) diff --git a/main.go b/main.go index e273478..4fe157a 100644 --- a/main.go +++ b/main.go @@ -72,5 +72,6 @@ func setDefaults() { viper.SetDefault("gk_grpc_compile_file_name", "compile.sh") } viper.SetDefault("gk_service_struct_prefix", "basic") + viper.SetDefault("gk_service_interface_name", "%sService") } diff --git a/utils/utils.go b/utils/utils.go index dee0f9d..0347012 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -215,3 +215,12 @@ func defaultGOPATH() string { } return "" } + +// GetServiceInterfaceName returns the service interface name +func GetServiceInterfaceName(name string) string { + format := viper.GetString("gk_service_interface_name") + if format == "" { + format = "%sService" + } + return fmt.Sprintf(format, ToCamelCase(name)) +} From fc009988b586ec4d225bab9459332e25fc045ec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A9sar=20de=20Oliveira?= Date: Wed, 15 May 2019 09:58:08 -0300 Subject: [PATCH 2/5] Allow to customize protobuf service name --- generator/add_transport.go | 4 ++-- main.go | 1 + utils/utils.go | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/generator/add_transport.go b/generator/add_transport.go index ba7c7db..1679ab3 100644 --- a/generator/add_transport.go +++ b/generator/add_transport.go @@ -645,7 +645,7 @@ type generateGRPCTransportProto struct { func newGenerateGRPCTransportProto(name string, serviceInterface parser.Interface, methods []string) Gen { t := &generateGRPCTransportProto{ - name: name, + name: utils.GetProtoServiceName(name), methods: methods, interfaceName: utils.GetServiceInterfaceName(name), destPath: fmt.Sprintf(viper.GetString("gk_grpc_pb_path_format"), utils.ToLowerSnakeCase(name)), @@ -948,7 +948,7 @@ func (g *generateGRPCTransportBase) Generate() (err error) { jen.Id("options").Map(jen.String()).Index().Qual("github.com/go-kit/kit/transport/grpc", "ServerOption"), }, []jen.Code{ - jen.Qual(pbImport, utils.ToCamelCase(g.name)+"Server"), + jen.Qual(pbImport, utils.ToCamelCase(utils.GetProtoServiceName(g.name)+"Server")), }, "", jen.Return(jen.Id("&grpcServer").Values(vl)), diff --git a/main.go b/main.go index 4fe157a..fb8e099 100644 --- a/main.go +++ b/main.go @@ -73,5 +73,6 @@ func setDefaults() { } viper.SetDefault("gk_service_struct_prefix", "basic") viper.SetDefault("gk_service_interface_name", "%sService") + viper.SetDefault("gk_proto_service_name", "%s") } diff --git a/utils/utils.go b/utils/utils.go index 0347012..45a31f7 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -224,3 +224,12 @@ func GetServiceInterfaceName(name string) string { } return fmt.Sprintf(format, ToCamelCase(name)) } + +// GetProtoServiceName returns the protobuf service name +func GetProtoServiceName(name string) string { + format := viper.GetString("gk_proto_service_name") + if format == "" { + format = "%s" + } + return fmt.Sprintf(format, name) +} From a7ebbeafb02eb0c0e5491d2d50d3f7924489eaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A9sar=20de=20Oliveira?= Date: Wed, 15 May 2019 10:33:09 -0300 Subject: [PATCH 3/5] Allow to customize protobuf package name --- generator/add_transport.go | 6 +++++- main.go | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/generator/add_transport.go b/generator/add_transport.go index 1679ab3..15e0a9f 100644 --- a/generator/add_transport.go +++ b/generator/add_transport.go @@ -689,13 +689,17 @@ func (g *generateGRPCTransportProto) Generate() (err error) { } if g.generateFirstTime { g.getServiceRPC(svc) + pkg := viper.GetString("gk_proto_package_name") + if pkg == "" { + pkg = "pb" + } g.protoSrc.Elements = append( g.protoSrc.Elements, &proto.Syntax{ Value: "proto3", }, &proto.Package{ - Name: "pb", + Name: pkg, }, svc, ) diff --git a/main.go b/main.go index fb8e099..0852119 100644 --- a/main.go +++ b/main.go @@ -74,5 +74,6 @@ func setDefaults() { viper.SetDefault("gk_service_struct_prefix", "basic") viper.SetDefault("gk_service_interface_name", "%sService") viper.SetDefault("gk_proto_service_name", "%s") + viper.SetDefault("gk_proto_package_name", "%s") } From 45669653f7cd5cdc33cf32100a25028ee5c78af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A9sar=20de=20Oliveira?= Date: Thu, 16 May 2019 16:52:56 -0300 Subject: [PATCH 4/5] Allow to customize protobuf package and service name in grpc client --- generator/add_transport.go | 8 ++------ generator/generate_client.go | 2 +- utils/utils.go | 11 ++++++++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/generator/add_transport.go b/generator/add_transport.go index 15e0a9f..4c6e014 100644 --- a/generator/add_transport.go +++ b/generator/add_transport.go @@ -689,17 +689,13 @@ func (g *generateGRPCTransportProto) Generate() (err error) { } if g.generateFirstTime { g.getServiceRPC(svc) - pkg := viper.GetString("gk_proto_package_name") - if pkg == "" { - pkg = "pb" - } g.protoSrc.Elements = append( g.protoSrc.Elements, &proto.Syntax{ Value: "proto3", }, &proto.Package{ - Name: pkg, + Name: utils.GetProtoPackageName(), }, svc, ) @@ -952,7 +948,7 @@ func (g *generateGRPCTransportBase) Generate() (err error) { jen.Id("options").Map(jen.String()).Index().Qual("github.com/go-kit/kit/transport/grpc", "ServerOption"), }, []jen.Code{ - jen.Qual(pbImport, utils.ToCamelCase(utils.GetProtoServiceName(g.name)+"Server")), + jen.Qual(pbImport, utils.GetProtoServiceName(g.name)+"Server"), }, "", jen.Return(jen.Id("&grpcServer").Values(vl)), diff --git a/generator/generate_client.go b/generator/generate_client.go index 2179222..d42d3d0 100644 --- a/generator/generate_client.go +++ b/generator/generate_client.go @@ -395,7 +395,7 @@ func (g *generateGRPCClient) Generate() (err error) { "NewClient", ).Call( jen.Id("conn"), - jen.Lit("pb."+utils.ToCamelCase(g.name)), + jen.Lit(utils.GetProtoPackageName()+"."+utils.GetProtoServiceName(g.name)), jen.Lit(m.Name), jen.Id(fmt.Sprintf("encode%sRequest", m.Name)), jen.Id(fmt.Sprintf("decode%sResponse", m.Name)), diff --git a/utils/utils.go b/utils/utils.go index 45a31f7..411bab9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -231,5 +231,14 @@ func GetProtoServiceName(name string) string { if format == "" { format = "%s" } - return fmt.Sprintf(format, name) + return ToCamelCase(fmt.Sprintf(format, name)) +} + +// GetProtoPackageName returns the protobuf package name +func GetProtoPackageName() string { + format := viper.GetString("gk_proto_package_name") + if format == "" { + return "pb" + } + return fmt.Sprintf(format, "pb") } From b4bf8a06aff2793719a8f9d1983b429ddccc82ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A9sar=20de=20Oliveira?= Date: Tue, 21 May 2019 19:19:28 -0300 Subject: [PATCH 5/5] Add support to custom protobuf service name on service generation --- generator/generate_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/generate_service.go b/generator/generate_service.go index 8c8e9f9..4b01109 100644 --- a/generator/generate_service.go +++ b/generator/generate_service.go @@ -1987,7 +1987,7 @@ func (g *generateCmd) generateInitGRPC() (err error) { jen.Id("*grpcAddr"), ), jen.Id("baseServer").Op(":=").Qual("google.golang.org/grpc", "NewServer").Call(), - jen.Qual(pbImport, fmt.Sprintf("Register%sServer", utils.ToCamelCase(g.name))).Call( + jen.Qual(pbImport, fmt.Sprintf("Register%sServer", utils.GetProtoServiceName(g.name))).Call( jen.Id("baseServer"), jen.Id("grpcServer"), ),