* update github.com/blevesearch/bleve v2.0.2 -> v2.0.3 * github.com/denisenkom/go-mssqldb v0.9.0 -> v0.10.0 * github.com/editorconfig/editorconfig-core-go v2.4.1 -> v2.4.2 * github.com/go-chi/cors v1.1.1 -> v1.2.0 * github.com/go-git/go-billy v5.0.0 -> v5.1.0 * github.com/go-git/go-git v5.2.0 -> v5.3.0 * github.com/go-ldap/ldap v3.2.4 -> v3.3.0 * github.com/go-redis/redis v8.6.0 -> v8.8.2 * github.com/go-sql-driver/mysql v1.5.0 -> v1.6.0 * github.com/go-swagger/go-swagger v0.26.1 -> v0.27.0 * github.com/lib/pq v1.9.0 -> v1.10.1 * github.com/mattn/go-sqlite3 v1.14.6 -> v1.14.7 * github.com/go-testfixtures/testfixtures v3.5.0 -> v3.6.0 * github.com/issue9/identicon v1.0.1 -> v1.2.0 * github.com/klauspost/compress v1.11.8 -> v1.12.1 * github.com/mgechev/revive v1.0.3 -> v1.0.6 * github.com/microcosm-cc/bluemonday v1.0.7 -> v1.0.8 * github.com/niklasfasching/go-org v1.4.0 -> v1.5.0 * github.com/olivere/elastic v7.0.22 -> v7.0.24 * github.com/pelletier/go-toml v1.8.1 -> v1.9.0 * github.com/prometheus/client_golang v1.9.0 -> v1.10.0 * github.com/xanzy/go-gitlab v0.44.0 -> v0.48.0 * github.com/yuin/goldmark v1.3.3 -> v1.3.5 * github.com/6543/go-version v1.2.4 -> v1.3.1 * do github.com/lib/pq v1.10.0 -> v1.10.1 again ...
177 lines
4.8 KiB
Go
Vendored
177 lines
4.8 KiB
Go
Vendored
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package impl
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
|
"google.golang.org/protobuf/internal/errors"
|
|
"google.golang.org/protobuf/proto"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
|
)
|
|
|
|
// Export is a zero-length named type that exists only to export a set of
|
|
// functions that we do not want to appear in godoc.
|
|
type Export struct{}
|
|
|
|
// NewError formats a string according to the format specifier and arguments and
|
|
// returns an error that has a "proto" prefix.
|
|
func (Export) NewError(f string, x ...interface{}) error {
|
|
return errors.New(f, x...)
|
|
}
|
|
|
|
// enum is any enum type generated by protoc-gen-go
|
|
// and must be a named int32 type.
|
|
type enum = interface{}
|
|
|
|
// EnumOf returns the protoreflect.Enum interface over e.
|
|
// It returns nil if e is nil.
|
|
func (Export) EnumOf(e enum) pref.Enum {
|
|
switch e := e.(type) {
|
|
case nil:
|
|
return nil
|
|
case pref.Enum:
|
|
return e
|
|
default:
|
|
return legacyWrapEnum(reflect.ValueOf(e))
|
|
}
|
|
}
|
|
|
|
// EnumDescriptorOf returns the protoreflect.EnumDescriptor for e.
|
|
// It returns nil if e is nil.
|
|
func (Export) EnumDescriptorOf(e enum) pref.EnumDescriptor {
|
|
switch e := e.(type) {
|
|
case nil:
|
|
return nil
|
|
case pref.Enum:
|
|
return e.Descriptor()
|
|
default:
|
|
return LegacyLoadEnumDesc(reflect.TypeOf(e))
|
|
}
|
|
}
|
|
|
|
// EnumTypeOf returns the protoreflect.EnumType for e.
|
|
// It returns nil if e is nil.
|
|
func (Export) EnumTypeOf(e enum) pref.EnumType {
|
|
switch e := e.(type) {
|
|
case nil:
|
|
return nil
|
|
case pref.Enum:
|
|
return e.Type()
|
|
default:
|
|
return legacyLoadEnumType(reflect.TypeOf(e))
|
|
}
|
|
}
|
|
|
|
// EnumStringOf returns the enum value as a string, either as the name if
|
|
// the number is resolvable, or the number formatted as a string.
|
|
func (Export) EnumStringOf(ed pref.EnumDescriptor, n pref.EnumNumber) string {
|
|
ev := ed.Values().ByNumber(n)
|
|
if ev != nil {
|
|
return string(ev.Name())
|
|
}
|
|
return strconv.Itoa(int(n))
|
|
}
|
|
|
|
// message is any message type generated by protoc-gen-go
|
|
// and must be a pointer to a named struct type.
|
|
type message = interface{}
|
|
|
|
// legacyMessageWrapper wraps a v2 message as a v1 message.
|
|
type legacyMessageWrapper struct{ m pref.ProtoMessage }
|
|
|
|
func (m legacyMessageWrapper) Reset() { proto.Reset(m.m) }
|
|
func (m legacyMessageWrapper) String() string { return Export{}.MessageStringOf(m.m) }
|
|
func (m legacyMessageWrapper) ProtoMessage() {}
|
|
|
|
// ProtoMessageV1Of converts either a v1 or v2 message to a v1 message.
|
|
// It returns nil if m is nil.
|
|
func (Export) ProtoMessageV1Of(m message) piface.MessageV1 {
|
|
switch mv := m.(type) {
|
|
case nil:
|
|
return nil
|
|
case piface.MessageV1:
|
|
return mv
|
|
case unwrapper:
|
|
return Export{}.ProtoMessageV1Of(mv.protoUnwrap())
|
|
case pref.ProtoMessage:
|
|
return legacyMessageWrapper{mv}
|
|
default:
|
|
panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
|
|
}
|
|
}
|
|
|
|
func (Export) protoMessageV2Of(m message) pref.ProtoMessage {
|
|
switch mv := m.(type) {
|
|
case nil:
|
|
return nil
|
|
case pref.ProtoMessage:
|
|
return mv
|
|
case legacyMessageWrapper:
|
|
return mv.m
|
|
case piface.MessageV1:
|
|
return nil
|
|
default:
|
|
panic(fmt.Sprintf("message %T is neither a v1 or v2 Message", m))
|
|
}
|
|
}
|
|
|
|
// ProtoMessageV2Of converts either a v1 or v2 message to a v2 message.
|
|
// It returns nil if m is nil.
|
|
func (Export) ProtoMessageV2Of(m message) pref.ProtoMessage {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
if mv := (Export{}).protoMessageV2Of(m); mv != nil {
|
|
return mv
|
|
}
|
|
return legacyWrapMessage(reflect.ValueOf(m)).Interface()
|
|
}
|
|
|
|
// MessageOf returns the protoreflect.Message interface over m.
|
|
// It returns nil if m is nil.
|
|
func (Export) MessageOf(m message) pref.Message {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
if mv := (Export{}).protoMessageV2Of(m); mv != nil {
|
|
return mv.ProtoReflect()
|
|
}
|
|
return legacyWrapMessage(reflect.ValueOf(m))
|
|
}
|
|
|
|
// MessageDescriptorOf returns the protoreflect.MessageDescriptor for m.
|
|
// It returns nil if m is nil.
|
|
func (Export) MessageDescriptorOf(m message) pref.MessageDescriptor {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
if mv := (Export{}).protoMessageV2Of(m); mv != nil {
|
|
return mv.ProtoReflect().Descriptor()
|
|
}
|
|
return LegacyLoadMessageDesc(reflect.TypeOf(m))
|
|
}
|
|
|
|
// MessageTypeOf returns the protoreflect.MessageType for m.
|
|
// It returns nil if m is nil.
|
|
func (Export) MessageTypeOf(m message) pref.MessageType {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
if mv := (Export{}).protoMessageV2Of(m); mv != nil {
|
|
return mv.ProtoReflect().Type()
|
|
}
|
|
return legacyLoadMessageType(reflect.TypeOf(m), "")
|
|
}
|
|
|
|
// MessageStringOf returns the message value as a string,
|
|
// which is the message serialized in the protobuf text format.
|
|
func (Export) MessageStringOf(m pref.ProtoMessage) string {
|
|
return prototext.MarshalOptions{Multiline: false}.Format(m)
|
|
}
|