Skip to content

Commit

Permalink
Merge pull request #53 from k1LoW/fix-resolve-paths
Browse files Browse the repository at this point in the history
Fix resolvePaths() to handle relative paths correctly
  • Loading branch information
k1LoW authored Sep 21, 2023
2 parents ebdfad9 + 024a489 commit 74242db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 3 additions & 3 deletions dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestResponseDynamicRepeated(t *testing.T) {
client := hello.NewGrpcTestServiceClient(ts.Conn())
res, err := client.Hello(ctx, &hello.HelloRequest{})
if err != nil {
t.Error(err)
t.Fatal(err)
}
if len(res.Hellos) == 0 {
t.Error("invalid repeated field value")
Expand All @@ -84,7 +84,7 @@ func TestResponseDynamicGenerated(t *testing.T) {
client := hello.NewGrpcTestServiceClient(ts.Conn())
res, err := client.Hello(ctx, &hello.HelloRequest{})
if err != nil {
t.Error(err)
t.Fatal(err)
}
if res.CreateTime.AsTime().UnixNano() != want.UnixNano() {
t.Errorf("got %v\nwant %v", res.CreateTime.AsTime().UnixNano(), want.UnixNano())
Expand All @@ -107,7 +107,7 @@ func TestResponseDynamicServer(t *testing.T) {
client := hello.NewGrpcTestServiceClient(ts.Conn())
res, err := client.Hello(ctx, &hello.HelloRequest{})
if err != nil {
t.Error(err)
t.Fatal(err)
}
if res.CreateTime.AsTime().UnixNano() != want.UnixNano() {
t.Errorf("got %v\nwant %v", res.CreateTime.AsTime().UnixNano(), want.UnixNano())
Expand Down
26 changes: 22 additions & 4 deletions grpcstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,12 +902,30 @@ func rangeTopLevelDescriptors(fd protoreflect.FileDescriptor, f func(protoreflec
}

func resolvePaths(importPaths []string, protos ...string) ([]string, []string, error) {
resolvedIPaths := importPaths
if len(importPaths) == 0 {
return importPaths, protos, nil
}
const sep = string(filepath.Separator)
importPaths = unique(importPaths)
var resolvedIPaths []string
for _, p := range importPaths {
abs, err := filepath.Abs(p)
if err != nil {
return nil, nil, err
}
resolvedIPaths = append(resolvedIPaths, abs)
}
resolvedProtos := []string{}
for _, p := range protos {
d, b := filepath.Split(p)
resolvedIPaths = append(resolvedIPaths, d)
resolvedProtos = append(resolvedProtos, b)
abs, err := filepath.Abs(p)
if err != nil {
return nil, nil, err
}
for _, ip := range resolvedIPaths {
if strings.HasPrefix(abs, ip+sep) {
resolvedProtos = append(resolvedProtos, strings.TrimPrefix(abs, ip+sep))
}
}
}
resolvedIPaths = unique(resolvedIPaths)
resolvedProtos = unique(resolvedProtos)
Expand Down
2 changes: 1 addition & 1 deletion grpcstub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestServerStreaming(t *testing.T) {
break
}
if err != nil {
t.Error(err)
t.Fatal(err)
}
switch c {
case 0:
Expand Down

0 comments on commit 74242db

Please sign in to comment.