github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/transport/http/encoding/jsonpb/json.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package jsonpb 16 17 import ( 18 "github.com/golang/protobuf/proto" 19 "google.golang.org/protobuf/reflect/protoreflect" 20 "google.golang.org/protobuf/reflect/protoregistry" 21 "google.golang.org/protobuf/runtime/protoimpl" 22 ) 23 24 // AnyResolver takes a type URL, present in an Any message, 25 // and resolves it into an instance of the associated message. 26 type AnyResolver interface { 27 Resolve(typeURL string) (proto.Message, error) 28 } 29 30 type anyResolver struct{ AnyResolver } 31 32 func (r anyResolver) FindMessageByName(message protoreflect.FullName) (protoreflect.MessageType, error) { 33 return r.FindMessageByURL(string(message)) 34 } 35 36 func (r anyResolver) FindMessageByURL(url string) (protoreflect.MessageType, error) { 37 m, err := r.Resolve(url) 38 if err != nil { 39 return nil, err 40 } 41 return protoimpl.X.MessageTypeOf(m), nil 42 } 43 44 func (r anyResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) { 45 return protoregistry.GlobalTypes.FindExtensionByName(field) 46 } 47 48 func (r anyResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) { 49 return protoregistry.GlobalTypes.FindExtensionByNumber(message, field) 50 } 51 52 func wellKnownType(s protoreflect.FullName) string { 53 if s.Parent() == "google.protobuf" { 54 switch s.Name() { 55 case "Empty", "Any", 56 "BoolValue", "BytesValue", "StringValue", 57 "Int32Value", "UInt32Value", "FloatValue", 58 "Int64Value", "UInt64Value", "DoubleValue", 59 "Duration", "Timestamp", 60 "NullValue", "Struct", "Value", "ListValue": 61 return string(s.Name()) 62 } 63 } 64 return "" 65 } 66 67 func isMessageSet(md protoreflect.MessageDescriptor) bool { 68 ms, ok := md.(interface{ IsMessageSet() bool }) 69 return ok && ms.IsMessageSet() 70 }