Improve imports support (imports lookup)
* Add `--single-package-mode option` * Add `getMessageType` helper * Add `getProtoFile` helper * Add `examples/single-package-mode` example
This commit is contained in:
parent
5448f25fd6
commit
4e25b6e83a
1
Makefile
1
Makefile
@ -16,6 +16,7 @@ test: install
|
||||
cd examples/flow && make
|
||||
cd examples/sitemap && make
|
||||
cd examples/go-generate && make
|
||||
cd examples/single-package-mode && make
|
||||
# cd examples/go-kit && make
|
||||
|
||||
.PHONY: docker.build
|
||||
|
35
README.md
35
README.md
@ -39,15 +39,21 @@ input.proto templates/doc.txt.tmpl templates/config.json.tmpl
|
||||
doc.txt config.json
|
||||
```
|
||||
|
||||
---
|
||||
### Options
|
||||
|
||||
You can specify a custom `template_dir` or enable `debug`:
|
||||
You can specify custom options, as follow:
|
||||
|
||||
```console
|
||||
$> protoc --gotemplate_out=debug=true,template_dir=/path/to/template/directory:. input.proto
|
||||
```
|
||||
|
||||
---
|
||||
| Option | Default Value | Accepted Values | Description
|
||||
|-----------------------|---------------|---------------------------|-----------------------
|
||||
| `template_dir` | `./template` | absolute or relative path | path to look for templates
|
||||
| `destination_dir` | `.` | absolute or relative path | base path to write output
|
||||
| `single-package-mode` | *false* | `true` or `false` | if *true*, `protoc` won't accept multiple packages to be compiled at once (*!= from `all`*), but will support `Message` lookup across the imported protobuf dependencies
|
||||
| `debug` | *false* | `true` or `false` | if *true*, `protoc` will generate a more verbose output
|
||||
| `all` | *false* | `true` or `false` | if *true*, protobuf files without `Service` will also be parsed
|
||||
|
||||
##### Hints
|
||||
|
||||
@ -61,13 +67,34 @@ See [examples](./examples).
|
||||
|
||||
This project uses [Masterminds/sprig](https://github.com/Masterminds/sprig) library and additional functions to extend the builtin [text/template](https://golang.org/pkg/text/template) helpers.
|
||||
|
||||
Non-exhaustive list of new helpers:
|
||||
Non-exhaustive list of new helpers:s
|
||||
|
||||
* **all the functions from [sprig](https://github.com/Masterminds/sprig)**
|
||||
* `json`
|
||||
* `prettyjson`
|
||||
* `first`
|
||||
* `last`
|
||||
* `splitArray`
|
||||
* `upperFirst`
|
||||
* `lowerFirst`
|
||||
* `camelCase`
|
||||
* `lowerCamelCase`
|
||||
* `kebabCase`
|
||||
* `snakeCase`
|
||||
* `getProtoFile`
|
||||
* `getMessageType`
|
||||
* `getEnumValue`
|
||||
* `isFieldMessage`
|
||||
* `isFieldRepeated`
|
||||
* `goType`
|
||||
* `goTypeWithPackage`
|
||||
* `jsType`
|
||||
* `jsSuffixReserved`
|
||||
* `namespacedFlowType`
|
||||
* `httpVerb`
|
||||
* `httpPath`
|
||||
* `shortType`
|
||||
* `urlHasVarsFromMessage`
|
||||
|
||||
See the project helpers for the complete list.
|
||||
|
||||
|
26
examples/single-package-mode/Makefile
Normal file
26
examples/single-package-mode/Makefile
Normal file
@ -0,0 +1,26 @@
|
||||
.PHONY: re
|
||||
re: clean build test
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
@mkdir -p output
|
||||
|
||||
@# proto-gen-go
|
||||
protoc -I./proto --go_out=plugins=grpc:output proto/aaa/aaa.proto
|
||||
protoc -I./proto --go_out=plugins=grpc:output proto/bbb/bbb.proto
|
||||
@rm -rf output/aaa output/bbb
|
||||
@mv output/github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/* output/
|
||||
@rm -rf output/github.com
|
||||
|
||||
@# protoc-gen-gotemplate
|
||||
protoc -I./proto --gotemplate_out=template_dir=templates,single-package-mode=true:output proto/bbb/bbb.proto
|
||||
gofmt -w .
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
go test -i ./output/...
|
||||
go test -v ./output/...
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf output
|
84
examples/single-package-mode/output/aaa/aaa.pb.go
Normal file
84
examples/single-package-mode/output/aaa/aaa.pb.go
Normal file
@ -0,0 +1,84 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: aaa/aaa.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package aaa is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
aaa/aaa.proto
|
||||
|
||||
It has these top-level messages:
|
||||
AaaRequest
|
||||
AaaReply
|
||||
*/
|
||||
package aaa
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type AaaRequest struct {
|
||||
Blah string `protobuf:"bytes,1,opt,name=blah" json:"blah,omitempty"`
|
||||
}
|
||||
|
||||
func (m *AaaRequest) Reset() { *m = AaaRequest{} }
|
||||
func (m *AaaRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*AaaRequest) ProtoMessage() {}
|
||||
func (*AaaRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *AaaRequest) GetBlah() string {
|
||||
if m != nil {
|
||||
return m.Blah
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type AaaReply struct {
|
||||
Error string `protobuf:"bytes,1,opt,name=error" json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (m *AaaReply) Reset() { *m = AaaReply{} }
|
||||
func (m *AaaReply) String() string { return proto.CompactTextString(m) }
|
||||
func (*AaaReply) ProtoMessage() {}
|
||||
func (*AaaReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *AaaReply) GetError() string {
|
||||
if m != nil {
|
||||
return m.Error
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*AaaRequest)(nil), "the.aaa.package.AaaRequest")
|
||||
proto.RegisterType((*AaaReply)(nil), "the.aaa.package.AaaReply")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("aaa/aaa.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 172 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x2c, 0x8e, 0x31, 0x0b, 0xc2, 0x30,
|
||||
0x10, 0x46, 0x29, 0xa8, 0x68, 0x40, 0x84, 0xe0, 0xe0, 0x58, 0x3a, 0xb9, 0xa4, 0x19, 0xfc, 0x05,
|
||||
0xba, 0x8b, 0xd0, 0xd1, 0xed, 0x5a, 0x8f, 0xb4, 0x78, 0xe9, 0xc5, 0xf4, 0x02, 0xfa, 0xef, 0xc5,
|
||||
0xd8, 0xed, 0x1e, 0xbc, 0xe3, 0x7b, 0x6a, 0x0b, 0x00, 0x16, 0x00, 0xea, 0x10, 0x59, 0x58, 0xef,
|
||||
0xa4, 0xc7, 0x3a, 0x23, 0x74, 0x4f, 0x70, 0x58, 0x95, 0x4a, 0x9d, 0x01, 0x1a, 0x7c, 0x25, 0x9c,
|
||||
0x44, 0x6b, 0xb5, 0x68, 0x09, 0xfa, 0x43, 0x51, 0x16, 0xc7, 0x4d, 0x93, 0xef, 0xaa, 0x54, 0xeb,
|
||||
0x6c, 0x04, 0xfa, 0xe8, 0xbd, 0x5a, 0x62, 0x8c, 0x1c, 0x67, 0xe1, 0x0f, 0x97, 0xdb, 0xfd, 0xea,
|
||||
0x06, 0xe9, 0x53, 0x5b, 0x77, 0xec, 0xad, 0xe7, 0x44, 0x36, 0xaf, 0x75, 0xc6, 0xe1, 0x68, 0x1c,
|
||||
0x0b, 0xfa, 0x40, 0x20, 0x68, 0xf1, 0x0d, 0x3e, 0x10, 0x4e, 0x76, 0x1a, 0x46, 0x47, 0x68, 0xe6,
|
||||
0x08, 0xe3, 0xf9, 0x81, 0x96, 0x93, 0x84, 0x24, 0xbf, 0xd6, 0x76, 0x95, 0xdf, 0x4f, 0xdf, 0x00,
|
||||
0x00, 0x00, 0xff, 0xff, 0x93, 0x24, 0x48, 0x9c, 0xbd, 0x00, 0x00, 0x00,
|
||||
}
|
200
examples/single-package-mode/output/bbb/bbb.pb.go
Normal file
200
examples/single-package-mode/output/bbb/bbb.pb.go
Normal file
@ -0,0 +1,200 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: bbb/bbb.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package bbb is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
bbb/bbb.proto
|
||||
|
||||
It has these top-level messages:
|
||||
BbbRequest
|
||||
BbbReply
|
||||
*/
|
||||
package bbb
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
import the_aaa_package "github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/aaa"
|
||||
|
||||
import (
|
||||
context "golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type BbbRequest struct {
|
||||
Enable bool `protobuf:"varint,1,opt,name=enable" json:"enable,omitempty"`
|
||||
}
|
||||
|
||||
func (m *BbbRequest) Reset() { *m = BbbRequest{} }
|
||||
func (m *BbbRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*BbbRequest) ProtoMessage() {}
|
||||
func (*BbbRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *BbbRequest) GetEnable() bool {
|
||||
if m != nil {
|
||||
return m.Enable
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type BbbReply struct {
|
||||
Done bool `protobuf:"varint,1,opt,name=done" json:"done,omitempty"`
|
||||
}
|
||||
|
||||
func (m *BbbReply) Reset() { *m = BbbReply{} }
|
||||
func (m *BbbReply) String() string { return proto.CompactTextString(m) }
|
||||
func (*BbbReply) ProtoMessage() {}
|
||||
func (*BbbReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *BbbReply) GetDone() bool {
|
||||
if m != nil {
|
||||
return m.Done
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*BbbRequest)(nil), "the.bbb.package.BbbRequest")
|
||||
proto.RegisterType((*BbbReply)(nil), "the.bbb.package.BbbReply")
|
||||
}
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ context.Context
|
||||
var _ grpc.ClientConn
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
const _ = grpc.SupportPackageIsVersion4
|
||||
|
||||
// Client API for BbbService service
|
||||
|
||||
type BbbServiceClient interface {
|
||||
Aaa(ctx context.Context, in *the_aaa_package.AaaRequest, opts ...grpc.CallOption) (*the_aaa_package.AaaReply, error)
|
||||
Bbb(ctx context.Context, in *BbbRequest, opts ...grpc.CallOption) (*BbbReply, error)
|
||||
}
|
||||
|
||||
type bbbServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewBbbServiceClient(cc *grpc.ClientConn) BbbServiceClient {
|
||||
return &bbbServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *bbbServiceClient) Aaa(ctx context.Context, in *the_aaa_package.AaaRequest, opts ...grpc.CallOption) (*the_aaa_package.AaaReply, error) {
|
||||
out := new(the_aaa_package.AaaReply)
|
||||
err := grpc.Invoke(ctx, "/the.bbb.package.BbbService/Aaa", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *bbbServiceClient) Bbb(ctx context.Context, in *BbbRequest, opts ...grpc.CallOption) (*BbbReply, error) {
|
||||
out := new(BbbReply)
|
||||
err := grpc.Invoke(ctx, "/the.bbb.package.BbbService/Bbb", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for BbbService service
|
||||
|
||||
type BbbServiceServer interface {
|
||||
Aaa(context.Context, *the_aaa_package.AaaRequest) (*the_aaa_package.AaaReply, error)
|
||||
Bbb(context.Context, *BbbRequest) (*BbbReply, error)
|
||||
}
|
||||
|
||||
func RegisterBbbServiceServer(s *grpc.Server, srv BbbServiceServer) {
|
||||
s.RegisterService(&_BbbService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _BbbService_Aaa_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(the_aaa_package.AaaRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(BbbServiceServer).Aaa(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/the.bbb.package.BbbService/Aaa",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(BbbServiceServer).Aaa(ctx, req.(*the_aaa_package.AaaRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _BbbService_Bbb_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(BbbRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(BbbServiceServer).Bbb(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/the.bbb.package.BbbService/Bbb",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(BbbServiceServer).Bbb(ctx, req.(*BbbRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _BbbService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "the.bbb.package.BbbService",
|
||||
HandlerType: (*BbbServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Aaa",
|
||||
Handler: _BbbService_Aaa_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Bbb",
|
||||
Handler: _BbbService_Bbb_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "bbb/bbb.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("bbb/bbb.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 242 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x41, 0x4b, 0x03, 0x31,
|
||||
0x10, 0x85, 0x59, 0x2a, 0xa5, 0x04, 0x8a, 0x90, 0x83, 0x68, 0x05, 0x91, 0xe2, 0xc1, 0xcb, 0x26,
|
||||
0xa0, 0xe7, 0x1e, 0xba, 0x77, 0x11, 0xea, 0xcd, 0xdb, 0xcc, 0x76, 0x48, 0x17, 0x93, 0x9d, 0xd8,
|
||||
0x9d, 0x88, 0xfd, 0x0b, 0xfe, 0x6a, 0xd9, 0x34, 0xa2, 0x60, 0x6f, 0x19, 0xde, 0xbc, 0xf7, 0xbd,
|
||||
0x8c, 0x9a, 0x23, 0xa2, 0x45, 0x44, 0x13, 0xf7, 0x2c, 0xac, 0xcf, 0x65, 0x47, 0x26, 0x8f, 0xd0,
|
||||
0xbe, 0x81, 0xa3, 0xc5, 0x1c, 0x00, 0x2c, 0x00, 0x1c, 0xf5, 0xe5, 0x9d, 0x52, 0x0d, 0xe2, 0x86,
|
||||
0xde, 0x13, 0x0d, 0xa2, 0x2f, 0xd4, 0x94, 0x7a, 0x40, 0x4f, 0x97, 0xd5, 0x6d, 0x75, 0x3f, 0xdb,
|
||||
0x94, 0x69, 0x79, 0xa3, 0x66, 0x79, 0x2b, 0xfa, 0x83, 0xd6, 0xea, 0x6c, 0xcb, 0xfd, 0xcf, 0x46,
|
||||
0x7e, 0x3f, 0x7c, 0x55, 0x39, 0xe6, 0x85, 0xf6, 0x1f, 0x5d, 0x4b, 0x7a, 0xa5, 0x26, 0x6b, 0x00,
|
||||
0x7d, 0x6d, 0x46, 0x78, 0x66, 0x1d, 0xe1, 0x66, 0x0d, 0x50, 0x50, 0x8b, 0xab, 0xd3, 0xe2, 0x48,
|
||||
0x58, 0xa9, 0x49, 0x83, 0x58, 0xec, 0x7f, 0xba, 0x9b, 0xdf, 0xa6, 0xc5, 0xfe, 0x4f, 0x8c, 0xfe,
|
||||
0xd0, 0x3c, 0xbf, 0x3e, 0xb9, 0x4e, 0x76, 0x09, 0x4d, 0xcb, 0xc1, 0x06, 0x4e, 0xde, 0xe6, 0xbf,
|
||||
0xb6, 0xb5, 0xa3, 0xbe, 0x76, 0x2c, 0x14, 0xa2, 0x07, 0x21, 0x4b, 0x9f, 0x10, 0xa2, 0xa7, 0xc1,
|
||||
0x0e, 0x5d, 0xef, 0x3c, 0xd5, 0x25, 0xa9, 0x0e, 0xbc, 0x25, 0xcb, 0x49, 0x62, 0x92, 0xf1, 0x92,
|
||||
0x38, 0xcd, 0xf6, 0xc7, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe0, 0xe1, 0x26, 0xcb, 0x5b, 0x01,
|
||||
0x00, 0x00,
|
||||
}
|
19
examples/single-package-mode/output/bbb/service.go
Normal file
19
examples/single-package-mode/output/bbb/service.go
Normal file
@ -0,0 +1,19 @@
|
||||
// file generated with protoc-gen-gotemplate
|
||||
package bbb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/aaa"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func (service Service) Aaa(ctx context.Context, input *aaa.AaaRequest) (*aaa.AaaReply, error) {
|
||||
return nil, fmt.Errorf("method not implemented")
|
||||
}
|
||||
|
||||
func (service Service) Bbb(ctx context.Context, input *BbbRequest) (*BbbReply, error) {
|
||||
return nil, fmt.Errorf("method not implemented")
|
||||
}
|
12
examples/single-package-mode/proto/aaa/aaa.proto
Normal file
12
examples/single-package-mode/proto/aaa/aaa.proto
Normal file
@ -0,0 +1,12 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option go_package = "github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/aaa";
|
||||
|
||||
package the.aaa.package;
|
||||
|
||||
message AaaRequest {
|
||||
string blah = 1;
|
||||
}
|
||||
message AaaReply {
|
||||
string error = 1;
|
||||
}
|
19
examples/single-package-mode/proto/bbb/bbb.proto
Normal file
19
examples/single-package-mode/proto/bbb/bbb.proto
Normal file
@ -0,0 +1,19 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package the.bbb.package;
|
||||
|
||||
option go_package = "github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/bbb";
|
||||
|
||||
import "aaa/aaa.proto";
|
||||
|
||||
service BbbService {
|
||||
rpc Aaa(the.aaa.package.AaaRequest) returns (the.aaa.package.AaaReply);
|
||||
rpc Bbb(BbbRequest) returns (BbbReply);
|
||||
}
|
||||
|
||||
message BbbRequest {
|
||||
bool enable = 1;
|
||||
}
|
||||
message BbbReply {
|
||||
bool done = 1;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
// file generated with protoc-gen-gotemplate
|
||||
package {{.File.Name | dir}}
|
||||
|
||||
{{- $file := .File}}
|
||||
{{- $currentFile := $file.Name | getProtoFile}}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
{{- range .File.Dependency}}
|
||||
{{- $dependency := . | getProtoFile}}
|
||||
{{$dependency.GoPkg}}
|
||||
{{end}}
|
||||
)
|
||||
|
||||
type Service struct {}
|
||||
|
||||
{{- range .Service.Method}}
|
||||
{{- $in := .InputType | getMessageType $file}}
|
||||
{{- $out := .OutputType | getMessageType $file}}
|
||||
func (service Service) {{.Name}}(ctx context.Context, input *{{$in.GoType $currentFile.GoPkg.Path}}) (*{{$out.GoType $currentFile.GoPkg.Path}}, error) {
|
||||
return nil, fmt.Errorf("method not implemented")
|
||||
}
|
||||
{{end}}
|
31
helpers.go
31
helpers.go
@ -8,10 +8,10 @@ import (
|
||||
"text/template"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/huandu/xstrings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
||||
"github.com/huandu/xstrings"
|
||||
options "google.golang.org/genproto/googleapis/api/annotations"
|
||||
)
|
||||
|
||||
@ -64,6 +64,7 @@ var ProtoHelpersFuncMap = template.FuncMap{
|
||||
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
||||
},
|
||||
"snakeCase": xstrings.ToSnakeCase,
|
||||
"getProtoFile": getProtoFile,
|
||||
"getMessageType": getMessageType,
|
||||
"getEnumValue": getEnumValue,
|
||||
"isFieldMessage": isFieldMessage,
|
||||
@ -85,17 +86,37 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto {
|
||||
func getProtoFile(name string) *ggdescriptor.File {
|
||||
if registry == nil {
|
||||
return nil
|
||||
}
|
||||
file, err := registry.LookupFile(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
func getMessageType(f *descriptor.FileDescriptorProto, name string) *ggdescriptor.Message {
|
||||
if registry != nil {
|
||||
msg, err := registry.LookupMsg(".", name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// name is in the form .packageName.MessageTypeName.InnerMessageTypeName...
|
||||
// e.g. .article.ProductTag
|
||||
splits := strings.Split(name, ".")
|
||||
target := splits[len(splits)-1]
|
||||
for _, m := range f.MessageType {
|
||||
if target == *m.Name {
|
||||
return m
|
||||
return &ggdescriptor.Message{
|
||||
DescriptorProto: m,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
43
main.go
43
main.go
@ -9,6 +9,11 @@ import (
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/protoc-gen-go/generator"
|
||||
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
||||
)
|
||||
|
||||
var (
|
||||
registry *ggdescriptor.Registry // some helpers need access to registry
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -30,10 +35,13 @@ func main() {
|
||||
g.CommandLineParameters(g.Request.GetParameter())
|
||||
|
||||
// Parse parameters
|
||||
templateDir := "./templates"
|
||||
destinationDir := "."
|
||||
debug := false
|
||||
all := false
|
||||
var (
|
||||
templateDir = "./templates"
|
||||
destinationDir = "."
|
||||
debug = false
|
||||
all = false
|
||||
singlePackageMode = false
|
||||
)
|
||||
if parameter := g.Request.GetParameter(); parameter != "" {
|
||||
for _, param := range strings.Split(parameter, ",") {
|
||||
parts := strings.Split(param, "=")
|
||||
@ -48,6 +56,15 @@ func main() {
|
||||
case "destination_dir":
|
||||
destinationDir = parts[1]
|
||||
break
|
||||
case "single-package-mode":
|
||||
switch strings.ToLower(parts[1]) {
|
||||
case "true", "t":
|
||||
singlePackageMode = true
|
||||
case "false", "f":
|
||||
default:
|
||||
log.Printf("Err: invalid value for single-package-mode: %q", parts[1])
|
||||
}
|
||||
break
|
||||
case "debug":
|
||||
switch strings.ToLower(parts[1]) {
|
||||
case "true", "t":
|
||||
@ -74,17 +91,29 @@ func main() {
|
||||
|
||||
tmplMap := make(map[string]*plugin_go.CodeGeneratorResponse_File)
|
||||
concatOrAppend := func(file *plugin_go.CodeGeneratorResponse_File) {
|
||||
if val, ok := tmplMap[*file.Name]; ok {
|
||||
*val.Content += *file.Content
|
||||
if val, ok := tmplMap[file.GetName()]; ok {
|
||||
*val.Content += file.GetContent()
|
||||
} else {
|
||||
tmplMap[*file.Name] = file
|
||||
tmplMap[file.GetName()] = file
|
||||
g.Response.File = append(g.Response.File, file)
|
||||
}
|
||||
}
|
||||
|
||||
if singlePackageMode {
|
||||
registry = ggdescriptor.NewRegistry()
|
||||
if err := registry.Load(g.Request); err != nil {
|
||||
g.Error(err, "registry: failed to load the request")
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the encoders
|
||||
for _, file := range g.Request.GetProtoFile() {
|
||||
if all {
|
||||
if singlePackageMode {
|
||||
if _, err := registry.LookupFile(file.GetName()); err != nil {
|
||||
g.Error(err, "registry: failed to lookup file %q", file.GetName())
|
||||
}
|
||||
}
|
||||
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
|
||||
for _, tmpl := range encoder.Files() {
|
||||
concatOrAppend(tmpl)
|
||||
|
Loading…
x
Reference in New Issue
Block a user