1
0
mirror of https://github.com/gusaul/grpcox.git synced 2024-12-25 09:50:10 +00:00

Merge pull request #27 from davidgoitia/allow_protoset

allow protoset file
This commit is contained in:
Muhammad Auliya 2021-02-16 17:49:53 +07:00 committed by GitHub
commit c83858ebfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 14 deletions

View File

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

View File

@ -60,7 +60,7 @@ func Test_prepareImport(t *testing.T) {
}
for _, tt := range tests {
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",
string(got),
string(tt.want))

View File

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