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:
parent
30fef12ac9
commit
d1a4237cb2
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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))
|
||||||
|
|
|
@ -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)) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user