Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize service interface name, and protobuf service and package name #39

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions generator/add_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -645,9 +645,9 @@ 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.ToCamelCase(name + "Service"),
interfaceName: utils.GetServiceInterfaceName(name),
destPath: fmt.Sprintf(viper.GetString("gk_grpc_pb_path_format"), utils.ToLowerSnakeCase(name)),
serviceInterface: serviceInterface,
}
Expand Down Expand Up @@ -695,7 +695,7 @@ func (g *generateGRPCTransportProto) Generate() (err error) {
Value: "proto3",
},
&proto.Package{
Name: "pb",
Name: utils.GetProtoPackageName(),
},
svc,
)
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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.GetProtoServiceName(g.name)+"Server"),
},
"",
jen.Return(jen.Id("&grpcServer").Values(vl)),
Expand All @@ -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,
}
Expand Down
8 changes: 4 additions & 4 deletions generator/generate_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
Expand Down
2 changes: 1 addition & 1 deletion generator/generate_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
14 changes: 7 additions & 7 deletions generator/generate_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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"),
),
Expand Down
2 changes: 1 addition & 1 deletion generator/new_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ 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")
viper.SetDefault("gk_proto_service_name", "%s")
viper.SetDefault("gk_proto_package_name", "%s")

}
27 changes: 27 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,30 @@ 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))
}

// GetProtoServiceName returns the protobuf service name
func GetProtoServiceName(name string) string {
format := viper.GetString("gk_proto_service_name")
if format == "" {
format = "%s"
}
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")
}