Skip to content

Commit

Permalink
Add WKT resolver for bufcurl (#3333)
Browse files Browse the repository at this point in the history
Appends a WKT resolver to the list of schemas for bufcurl. This WKT resolver is used on request and responses when marshalling to JSON. Fixes resolving types within a `google.protobuf.Any` type.
  • Loading branch information
emcfarlane authored Sep 23, 2024
1 parent 829f797 commit 1e73ead
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
33 changes: 33 additions & 0 deletions private/buf/bufcurl/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
package bufcurl

import (
"context"

"github.com/bufbuild/buf/private/bufpkg/bufimage"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/gen/data/datawkt"
"github.com/bufbuild/buf/private/pkg/protoencoding"
"github.com/bufbuild/buf/private/pkg/tracing"
"google.golang.org/protobuf/reflect/protoreflect"
)

Expand Down Expand Up @@ -96,3 +101,31 @@ func (c *combinedResolver) ListServices() ([]protoreflect.FullName, error) {
}
return serviceNames, nil
}

// NewWKTResolver returns a Resolver that can resolve all well-known types.
func NewWKTResolver(ctx context.Context, tracer tracing.Tracer) (Resolver, error) {
moduleSet, err := bufmodule.NewModuleSetBuilder(
ctx,
tracer,
bufmodule.NopModuleDataProvider,
bufmodule.NopCommitProvider,
).AddLocalModule(
datawkt.ReadBucket,
".",
true,
).Build()
if err != nil {
return nil, err
}
module := bufmodule.ModuleSetToModuleReadBucketWithOnlyProtoFiles(moduleSet)
image, err := bufimage.BuildImage(
ctx,
tracer,
module,
bufimage.WithExcludeSourceCodeInfo(),
)
if err != nil {
return nil, err
}
return ResolverForImage(image), nil
}
11 changes: 9 additions & 2 deletions private/buf/cmd/buf/command/curl/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/bufbuild/buf/private/pkg/app/appext"
"github.com/bufbuild/buf/private/pkg/netrc"
"github.com/bufbuild/buf/private/pkg/stringutil"
"github.com/bufbuild/buf/private/pkg/tracing"
"github.com/bufbuild/buf/private/pkg/verbose"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
Expand Down Expand Up @@ -965,7 +966,7 @@ func run(ctx context.Context, container appext.Container, f *flags) (err error)
}
}

resolvers := make([]bufcurl.Resolver, 0, len(f.Schemas)+1)
resolvers := make([]bufcurl.Resolver, 0, len(f.Schemas)+2)
if f.Reflect {
reflectHeaders, _, err := bufcurl.LoadHeaders(f.ReflectHeaders, "", requestHeaders)
if err != nil {
Expand Down Expand Up @@ -1010,7 +1011,13 @@ func run(ctx context.Context, container appext.Container, f *flags) (err error)
}
resolvers = append(resolvers, bufcurl.ResolverForImage(image))
}
res := bufcurl.CombineResolvers(resolvers...)
// Add a WKT resolver to the end of the end of the list. This is used
// for printing a WKT encoded in a "google.protobuf.Any" type as JSON.
wktResolver, err := bufcurl.NewWKTResolver(ctx, tracing.NewTracer(container.Tracer()))
if err != nil {
return err
}
res := bufcurl.CombineResolvers(append(resolvers, wktResolver)...)

switch {
case f.ListServices || f.ListMethods:
Expand Down

0 comments on commit 1e73ead

Please sign in to comment.