handle statement import of proto file
This commit is contained in:
parent
c73a4b8c46
commit
91ebb6e521
1
Makefile
1
Makefile
@ -8,6 +8,7 @@ install:
|
||||
|
||||
.PHONY: test
|
||||
test: build
|
||||
cd examples/import && make
|
||||
cd examples/dummy && make
|
||||
cd examples/flow && make
|
||||
cd examples/concat && make
|
||||
|
18
examples/import/Makefile
Normal file
18
examples/import/Makefile
Normal file
@ -0,0 +1,18 @@
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p output
|
||||
|
||||
# generate pb.go inluding imported proto
|
||||
protoc --go_out=Mproto/common.proto=github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common:./output proto/article.proto
|
||||
protoc --go_out=,plugins=grpc:./output proto/common.proto
|
||||
|
||||
# build our go file based on our template
|
||||
protoc -I. --gotemplate_out=template_dir=templates,debug=true:output proto/article.proto
|
||||
|
||||
.PHONY: re
|
||||
re: clean build
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf output
|
113
examples/import/output/models/article/article.pb.go
Normal file
113
examples/import/output/models/article/article.pb.go
Normal file
@ -0,0 +1,113 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: proto/article.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package article is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
proto/article.proto
|
||||
|
||||
It has these top-level messages:
|
||||
GetArticleRequest
|
||||
GetArticleResponse
|
||||
Article
|
||||
*/
|
||||
package article
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
import common "github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common"
|
||||
|
||||
// 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 GetArticleRequest struct {
|
||||
Getarticle *common.GetArticle `protobuf:"bytes,1,opt,name=getarticle" json:"getarticle,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetArticleRequest) Reset() { *m = GetArticleRequest{} }
|
||||
func (m *GetArticleRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetArticleRequest) ProtoMessage() {}
|
||||
func (*GetArticleRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *GetArticleRequest) GetGetarticle() *common.GetArticle {
|
||||
if m != nil {
|
||||
return m.Getarticle
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type GetArticleResponse struct {
|
||||
Article *Article `protobuf:"bytes,1,opt,name=article" json:"article,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetArticleResponse) Reset() { *m = GetArticleResponse{} }
|
||||
func (m *GetArticleResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetArticleResponse) ProtoMessage() {}
|
||||
func (*GetArticleResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *GetArticleResponse) GetArticle() *Article {
|
||||
if m != nil {
|
||||
return m.Article
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Article struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Article) Reset() { *m = Article{} }
|
||||
func (m *Article) String() string { return proto.CompactTextString(m) }
|
||||
func (*Article) ProtoMessage() {}
|
||||
func (*Article) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
|
||||
|
||||
func (m *Article) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Article) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*GetArticleRequest)(nil), "article.GetArticleRequest")
|
||||
proto.RegisterType((*GetArticleResponse)(nil), "article.GetArticleResponse")
|
||||
proto.RegisterType((*Article)(nil), "article.Article")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("proto/article.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 208 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0xca, 0x2f,
|
||||
0xc9, 0xd7, 0x4f, 0x2c, 0x2a, 0xc9, 0x4c, 0xce, 0x49, 0xd5, 0x03, 0xf3, 0x84, 0xd8, 0xa1, 0x5c,
|
||||
0x29, 0x21, 0x88, 0x6c, 0x72, 0x7e, 0x6e, 0x6e, 0x7e, 0x1e, 0x44, 0x52, 0xc9, 0x9d, 0x4b, 0xd0,
|
||||
0x3d, 0xb5, 0xc4, 0x11, 0xa2, 0x22, 0x28, 0xb5, 0xb0, 0x34, 0xb5, 0xb8, 0x44, 0xc8, 0x88, 0x8b,
|
||||
0x2b, 0x3d, 0xb5, 0x04, 0xaa, 0x4d, 0x82, 0x51, 0x81, 0x51, 0x83, 0xdb, 0x48, 0x48, 0x0f, 0xaa,
|
||||
0x0f, 0x49, 0x39, 0x92, 0x2a, 0x25, 0x07, 0x2e, 0x21, 0x64, 0x83, 0x8a, 0x0b, 0xf2, 0xf3, 0x8a,
|
||||
0x53, 0x85, 0xb4, 0xb8, 0xd8, 0x51, 0x8d, 0x11, 0xd0, 0x83, 0x39, 0x0e, 0xa6, 0x14, 0xa6, 0x40,
|
||||
0x49, 0x97, 0x8b, 0x1d, 0x2a, 0x26, 0xc4, 0xc7, 0xc5, 0x94, 0x99, 0x02, 0xd6, 0xc1, 0x19, 0xc4,
|
||||
0x94, 0x99, 0x22, 0x24, 0xc4, 0xc5, 0x92, 0x97, 0x98, 0x9b, 0x2a, 0xc1, 0x04, 0x16, 0x01, 0xb3,
|
||||
0x8d, 0x42, 0xb9, 0xb8, 0xa0, 0x3a, 0x8b, 0xcb, 0x92, 0x85, 0xdc, 0xb9, 0xb8, 0x10, 0xd6, 0x0b,
|
||||
0x49, 0xc1, 0x6d, 0xc1, 0xf0, 0x9c, 0x94, 0x34, 0x56, 0x39, 0x88, 0x7b, 0x95, 0x18, 0x9c, 0x24,
|
||||
0xa2, 0xc4, 0x72, 0xf3, 0x53, 0x52, 0x73, 0x8a, 0x61, 0xa1, 0x68, 0x0d, 0xa5, 0x93, 0xd8, 0xc0,
|
||||
0x21, 0x66, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x16, 0x18, 0x87, 0xc4, 0x65, 0x01, 0x00, 0x00,
|
||||
}
|
70
examples/import/output/models/common/common.pb.go
Normal file
70
examples/import/output/models/common/common.pb.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: proto/common.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package common is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
proto/common.proto
|
||||
|
||||
It has these top-level messages:
|
||||
GetArticle
|
||||
*/
|
||||
package common
|
||||
|
||||
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 GetArticle struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||
Tenant string `protobuf:"bytes,2,opt,name=tenant" json:"tenant,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetArticle) Reset() { *m = GetArticle{} }
|
||||
func (m *GetArticle) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetArticle) ProtoMessage() {}
|
||||
func (*GetArticle) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *GetArticle) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *GetArticle) GetTenant() string {
|
||||
if m != nil {
|
||||
return m.Tenant
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*GetArticle)(nil), "common.GetArticle")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("proto/common.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 110 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2a, 0x28, 0xca, 0x2f,
|
||||
0xc9, 0xd7, 0x4f, 0xce, 0xcf, 0xcd, 0xcd, 0xcf, 0xd3, 0x03, 0x73, 0x84, 0xd8, 0x20, 0x3c, 0x25,
|
||||
0x13, 0x2e, 0x2e, 0xf7, 0xd4, 0x12, 0xc7, 0xa2, 0x92, 0xcc, 0xe4, 0x9c, 0x54, 0x21, 0x3e, 0x2e,
|
||||
0xa6, 0xcc, 0x14, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, 0xa6, 0xcc, 0x14, 0x21, 0x31, 0x2e,
|
||||
0xb6, 0x92, 0xd4, 0xbc, 0xc4, 0xbc, 0x12, 0x09, 0x26, 0xb0, 0x18, 0x94, 0xe7, 0x24, 0x16, 0x25,
|
||||
0x92, 0x9b, 0x9f, 0x92, 0x9a, 0x53, 0x0c, 0x35, 0xd4, 0x1a, 0x42, 0x25, 0xb1, 0x81, 0x0d, 0x37,
|
||||
0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x89, 0x5a, 0x1d, 0x73, 0x72, 0x00, 0x00, 0x00,
|
||||
}
|
11
examples/import/output/output.go
Normal file
11
examples/import/output/output.go
Normal file
@ -0,0 +1,11 @@
|
||||
// Code generated by protoc-gen-gotemplate
|
||||
package article
|
||||
|
||||
import (
|
||||
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/article"
|
||||
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
GetArticle(getarticle *common.GetArticle ) (*article.Article, error)
|
||||
}
|
19
examples/import/proto/article.proto
Normal file
19
examples/import/proto/article.proto
Normal file
@ -0,0 +1,19 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package article;
|
||||
|
||||
option go_package = "models/article;article";
|
||||
|
||||
import "proto/common.proto";
|
||||
|
||||
message GetArticleRequest { common.GetArticle getarticle = 1;}
|
||||
message GetArticleResponse { Article article = 1;}
|
||||
|
||||
message Article{
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
}
|
||||
|
||||
service articlesvc {
|
||||
rpc GetArticle (GetArticleRequest) returns (GetArticleResponse){}
|
||||
}
|
10
examples/import/proto/common.proto
Normal file
10
examples/import/proto/common.proto
Normal file
@ -0,0 +1,10 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package common;
|
||||
|
||||
option go_package = "models/common;common";
|
||||
|
||||
message GetArticle{
|
||||
string id = 1;
|
||||
string tenant = 2;
|
||||
}
|
11
examples/import/templates/output.go.tmpl
Normal file
11
examples/import/templates/output.go.tmpl
Normal file
@ -0,0 +1,11 @@
|
||||
// Code generated by protoc-gen-gotemplate
|
||||
package {{.File.Package}}
|
||||
|
||||
import (
|
||||
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/article"
|
||||
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
{{range $m := .Service.Method}}{{with $t := $m.InputType | getMessageType $.File}} {{$m.Name}}({{range $f := $t.Field}}{{$f.Name|lowerCamelCase}} {{$f| goTypeWithPackage }} {{end}}) ({{with $out := $m.OutputType | getMessageType $.File}}{{range $f := $out.Field}}{{$f | goTypeWithPackage}}, {{end}}{{end}} error){{end}}{{end}}
|
||||
}
|
13
helpers.go
13
helpers.go
@ -68,6 +68,7 @@ var ProtoHelpersFuncMap = template.FuncMap{
|
||||
"isFieldMessage": isFieldMessage,
|
||||
"isFieldRepeated": isFieldRepeated,
|
||||
"goType": goType,
|
||||
"goTypeWithPackage": goTypeWithPackage,
|
||||
"jsType": jsType,
|
||||
"jsSuffixReserved": jsSuffixReservedKeyword,
|
||||
"namespacedFlowType": namespacedFlowType,
|
||||
@ -113,6 +114,11 @@ func isFieldRepeated(f *descriptor.FieldDescriptorProto) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func goTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
|
||||
pkg := getPackageTypeName(*f.TypeName)
|
||||
return goType(pkg, f)
|
||||
}
|
||||
|
||||
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
||||
switch *f.Type {
|
||||
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
||||
@ -213,6 +219,13 @@ func jsSuffixReservedKeyword(s string) string {
|
||||
return jsReservedRe.ReplaceAllString(s, "${1}${2}_${3}")
|
||||
}
|
||||
|
||||
func getPackageTypeName(s string) string {
|
||||
if strings.Contains(s, ".") {
|
||||
return strings.Split(s, ".")[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func shortType(s string) string {
|
||||
t := strings.Split(s, ".")
|
||||
return t[len(t)-1]
|
||||
|
Loading…
x
Reference in New Issue
Block a user