add API to expose AnyResolver implementations backed by a DescriptorSource (#102)
This commit is contained in:
parent
09c3d1d69e
commit
e5b4fc6cc0
20
format.go
20
format.go
|
|
@ -195,6 +195,22 @@ const (
|
||||||
FormatText = Format("text")
|
FormatText = Format("text")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AnyResolverFromDescriptorSource returns an AnyResolver that will search for
|
||||||
|
// types using the given descriptor source.
|
||||||
|
func AnyResolverFromDescriptorSource(source DescriptorSource) jsonpb.AnyResolver {
|
||||||
|
return &anyResolver{source: source}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AnyResolverFromDescriptorSourceWithFallback returns an AnyResolver that will
|
||||||
|
// search for types using the given descriptor source and then fallback to a
|
||||||
|
// special message if the type is not found. The fallback type will render to
|
||||||
|
// JSON with a "@type" property, just like an Any message, but also with a
|
||||||
|
// custom "@value" property that includes the binary encoded payload.
|
||||||
|
func AnyResolverFromDescriptorSourceWithFallback(source DescriptorSource) jsonpb.AnyResolver {
|
||||||
|
res := anyResolver{source: source}
|
||||||
|
return &anyResolverWithFallback{AnyResolver: &res}
|
||||||
|
}
|
||||||
|
|
||||||
type anyResolver struct {
|
type anyResolver struct {
|
||||||
source DescriptorSource
|
source DescriptorSource
|
||||||
|
|
||||||
|
|
@ -334,8 +350,8 @@ var _ proto.Message = (*unknownAny)(nil)
|
||||||
func RequestParserAndFormatterFor(format Format, descSource DescriptorSource, emitJSONDefaultFields, includeTextSeparator bool, in io.Reader) (RequestParser, Formatter, error) {
|
func RequestParserAndFormatterFor(format Format, descSource DescriptorSource, emitJSONDefaultFields, includeTextSeparator bool, in io.Reader) (RequestParser, Formatter, error) {
|
||||||
switch format {
|
switch format {
|
||||||
case FormatJSON:
|
case FormatJSON:
|
||||||
resolver := anyResolver{source: descSource}
|
resolver := AnyResolverFromDescriptorSource(descSource)
|
||||||
return NewJSONRequestParser(in, &resolver), NewJSONFormatter(emitJSONDefaultFields, anyResolverWithFallback{AnyResolver: &resolver}), nil
|
return NewJSONRequestParser(in, resolver), NewJSONFormatter(emitJSONDefaultFields, anyResolverWithFallback{AnyResolver: resolver}), nil
|
||||||
case FormatText:
|
case FormatText:
|
||||||
return NewTextRequestParser(in), NewTextFormatter(includeTextSeparator), nil
|
return NewTextRequestParser(in), NewTextFormatter(includeTextSeparator), nil
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue