1
0
mirror of https://github.com/gusaul/grpcox.git synced 2024-12-26 02:40:10 +00:00

allow protoset file

This commit is contained in:
David Goitia 2020-10-21 21:57:51 +02:00
parent 30fef12ac9
commit d1a4237cb2
No known key found for this signature in database
GPG Key ID: 7C1B6DE586174DE9
3 changed files with 36 additions and 14 deletions

View File

@ -32,6 +32,7 @@ type Resource struct {
descSource grpcurl.DescriptorSource descSource grpcurl.DescriptorSource
refClient *grpcreflect.Client refClient *grpcreflect.Client
protos []Proto protos []Proto
protosets []Proto
headers []string headers []string
md metadata.MD md metadata.MD
@ -44,21 +45,31 @@ func (r *Resource) openDescriptor() error {
r.refClient = grpcreflect.NewClient(refCtx, reflectpb.NewServerReflectionClient(r.clientConn)) r.refClient = grpcreflect.NewClient(refCtx, reflectpb.NewServerReflectionClient(r.clientConn))
// if no protos available use server reflection // if no protos available use server reflection
if r.protos == nil { if r.protos == nil && r.protosets == nil {
r.descSource = grpcurl.DescriptorSourceFromServer(ctx, r.refClient) r.descSource = grpcurl.DescriptorSourceFromServer(ctx, r.refClient)
return nil return nil
} }
protoPath := filepath.Join(BasePath, r.clientConn.Target()) protoPath := filepath.Join(BasePath, r.clientConn.Target())
var err error
if len(r.protosets) > 0 {
// make list of protos name to be used as descriptor
protos := make([]string, 0, len(r.protosets))
for _, proto := range r.protosets {
protos = append(protos, filepath.Join(protoPath, proto.Name))
}
r.descSource, err = grpcurl.DescriptorSourceFromProtoSets(protos...)
} else {
// make list of protos name to be used as descriptor // make list of protos name to be used as descriptor
protos := make([]string, 0, len(r.protos)) protos := make([]string, 0, len(r.protos))
for _, proto := range r.protos { for _, proto := range r.protos {
protos = append(protos, proto.Name) protos = append(protos, proto.Name)
} }
var err error
r.descSource, err = grpcurl.DescriptorSourceFromProtoFiles([]string{protoPath}, protos...) r.descSource, err = grpcurl.DescriptorSourceFromProtoFiles([]string{protoPath}, protos...)
}
return err return err
} }
@ -297,16 +308,27 @@ func (r *Resource) AddProtos(protos []Proto) error {
return err return err
} }
var protoSlice, protosetSlice []Proto
for _, proto := range protos { for _, proto := range protos {
err := ioutil.WriteFile(filepath.Join(protoPath, "/", proto.Name), var err error
if strings.HasSuffix(proto.Name, ".protoset") {
protosetSlice = append(protosetSlice, proto)
err = ioutil.WriteFile(filepath.Join(protoPath, "/", proto.Name),
proto.Content,
0777)
} else {
protoSlice = append(protoSlice, proto)
err = ioutil.WriteFile(filepath.Join(protoPath, "/", proto.Name),
prepareImport(proto.Content), prepareImport(proto.Content),
0777) 0777)
}
if err != nil { if err != nil {
return err return err
} }
} }
r.protos = protos r.protos = protoSlice
r.protosets = protosetSlice
return nil return nil
} }

View File

@ -60,7 +60,7 @@ func Test_prepareImport(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := prepareImport(tt.args.proto); !reflect.DeepEqual(got, tt.want) { if got := prepareImport(tt.args.proto, ""); !reflect.DeepEqual(got, tt.want) {
t.Errorf("prepareImport() = %v, want %v", t.Errorf("prepareImport() = %v, want %v",
string(got), string(got),
string(tt.want)) string(tt.want))

View File

@ -74,7 +74,7 @@ function handleProtoUpload() {
const files = this.files; const files = this.files;
for (const file of files) { for (const file of files) {
if (!file.name.endsWith(".proto")) { if (!file.name.endsWith(".proto") && !file.name.endsWith(".protoset")) {
continue; continue;
} }
if (!protoMap.has(file.name)) { if (!protoMap.has(file.name)) {