Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WKT resolver for bufcurl #3333

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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