Skip to content

Commit

Permalink
Merge pull request #909 from k1LoW/use-protoresolv
Browse files Browse the repository at this point in the history
Introduce protoresolv so that protos under it are automatically loaded simply by specifying importPaths.
  • Loading branch information
k1LoW authored May 4, 2024
2 parents b6eae9b + 3e0d15e commit b81ba86
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,11 @@ runners:
cert: path/to/cert.pem
key: path/to/key.pem
# skipVerify: false
# importPaths:
# - protobuf/proto
# protos:
# - general/health.proto
# - myapp/**/*.proto
# importPaths:
# - protobuf/proto
```

See [testdata/book/grpc.yml](testdata/book/grpc.yml).
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/k1LoW/grpcstub v0.19.0
github.com/k1LoW/grpcurlreq v0.2.1
github.com/k1LoW/httpstub v0.16.0
github.com/k1LoW/protoresolv v0.1.0
github.com/k1LoW/repin v0.3.4
github.com/k1LoW/sshc/v4 v4.2.0
github.com/k1LoW/stopw v0.9.0
Expand Down Expand Up @@ -68,7 +69,7 @@ require (
golang.org/x/sync v0.7.0
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002
google.golang.org/protobuf v1.34.0
gopkg.in/yaml.v2 v2.4.0
modernc.org/sqlite v1.29.8
)
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ github.com/k1LoW/grpcurlreq v0.2.1 h1:vV5XPcudceX5+DvnEvyDY11E/MkJ01RmYoHO3/8aNj
github.com/k1LoW/grpcurlreq v0.2.1/go.mod h1:fYHtOo6/5ans9ub1CDRIQQfvo4NyTGwPud+FH/Rnx90=
github.com/k1LoW/httpstub v0.16.0 h1:ey0ODSMoSqSzcCeqGv05fXPUvzOIce5suB+0Pcy43Mg=
github.com/k1LoW/httpstub v0.16.0/go.mod h1:pc/0IQnxLufPvmHPib6oJ376GpDfxne1RY8NuPL9q8w=
github.com/k1LoW/protoresolv v0.1.0 h1:5Or1g0EB8o6VSJSxUk8WEVoYZFzGQQMlkk8ezb8/mdA=
github.com/k1LoW/protoresolv v0.1.0/go.mod h1:J3RVl3l/fNgwEYEhwTsCCwxXYsvTIAYU69jUXFQHEbQ=
github.com/k1LoW/repin v0.3.4 h1:xcNuBBc/ISHUNBzjXNTCux4OYZND5ZMiyz4SrRtpDhg=
github.com/k1LoW/repin v0.3.4/go.mod h1:1abQMGdYFegTCsxbhZ3O5P8aKENYS37UhQKwKYKiYkg=
github.com/k1LoW/sshc/v4 v4.2.0 h1:Bvq2L4q9u25N8Rti70sKC0BuGOcfFMylvWVz7JIdeGM=
Expand Down Expand Up @@ -595,8 +597,8 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002 h1:V7Da7qt0MkY3noVANIMVBk28nOnijADeOR3i5Hcvpj4=
google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
44 changes: 5 additions & 39 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
Expand All @@ -18,6 +18,7 @@ import (
"github.com/goccy/go-json"
"github.com/jhump/protoreflect/v2/grpcreflect"
"github.com/k1LoW/bufresolv"
"github.com/k1LoW/protoresolv"
"github.com/k1LoW/runn/version"
"github.com/mitchellh/copystructure"
"google.golang.org/grpc"
Expand Down Expand Up @@ -775,7 +776,7 @@ func (rnr *grpcRunner) resolveAllMethodsUsingProtos(ctx context.Context) error {
if err != nil {
return err
}
importPaths, protos, err := resolvePaths(rnr.importPaths, protos...)
pr, err := protoresolv.New(rnr.importPaths, protoresolv.Proto(protos...))
if err != nil {
return err
}
Expand All @@ -794,16 +795,12 @@ func (rnr *grpcRunner) resolveAllMethodsUsingProtos(ctx context.Context) error {
if err != nil {
return err
}

comp := protocompile.Compiler{
Resolver: protocompile.WithStandardImports(protocompile.CompositeResolver([]protocompile.Resolver{
&protocompile.SourceResolver{
ImportPaths: importPaths,
},
br,
pr, br,
})),
}
protos = unique(append(protos, br.Paths()...))
protos = unique(slices.Concat(pr.Paths(), br.Paths()))
fds, err := comp.Compile(ctx, protos...)
if err != nil {
return err
Expand Down Expand Up @@ -903,34 +900,3 @@ func rangeTopLevelDescriptors(fd protoreflect.FileDescriptor, f func(protoreflec
f(sds.Get(i))
}
}

func resolvePaths(importPaths []string, protos ...string) ([]string, []string, error) {
const sep = string(filepath.Separator)
if len(importPaths) == 0 {
return importPaths, protos, nil
}
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)
}
resolvedIPaths = unique(resolvedIPaths)
var resolvedProtos []string
for _, p := range protos {
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))
}
}
}
resolvedProtos = unique(resolvedProtos)
return resolvedIPaths, resolvedProtos, nil
}

0 comments on commit b81ba86

Please sign in to comment.