| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package proto |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"compress/gzip" |
| 10 |
"fmt" |
| 11 |
"io/ioutil" |
| 12 |
"reflect" |
| 13 |
"strings" |
| 14 |
"sync" |
| 15 |
|
| 16 |
"google.golang.org/protobuf/reflect/protodesc" |
| 17 |
"google.golang.org/protobuf/reflect/protoreflect" |
| 18 |
"google.golang.org/protobuf/reflect/protoregistry" |
| 19 |
"google.golang.org/protobuf/runtime/protoimpl" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
type filePath = string |
| 24 |
|
| 25 |
|
| 26 |
type fileDescGZIP = []byte |
| 27 |
|
| 28 |
var fileCache sync.Map |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
func RegisterFile(s filePath, d fileDescGZIP) { |
| 35 |
|
| 36 |
zr, err := gzip.NewReader(bytes.NewReader(d)) |
| 37 |
if err != nil { |
| 38 |
panic(fmt.Sprintf("proto: invalid compressed file descriptor: %v", err)) |
| 39 |
} |
| 40 |
b, err := ioutil.ReadAll(zr) |
| 41 |
if err != nil { |
| 42 |
panic(fmt.Sprintf("proto: invalid compressed file descriptor: %v", err)) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protoimpl.DescBuilder{RawDescriptor: b}.Build() |
| 49 |
|
| 50 |
|
| 51 |
fileCache.Store(s, d) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func FileDescriptor(s filePath) fileDescGZIP { |
| 59 |
if v, ok := fileCache.Load(s); ok { |
| 60 |
return v.(fileDescGZIP) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
var b []byte |
| 65 |
if fd, _ := protoregistry.GlobalFiles.FindFileByPath(s); fd != nil { |
| 66 |
b, _ = Marshal(protodesc.ToFileDescriptorProto(fd)) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
if len(b) > 0 { |
| 71 |
v, _ := fileCache.LoadOrStore(s, protoimpl.X.CompressGZIP(b)) |
| 72 |
return v.(fileDescGZIP) |
| 73 |
} |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
type enumName = string |
| 82 |
|
| 83 |
|
| 84 |
type enumsByName = map[string]int32 |
| 85 |
|
| 86 |
|
| 87 |
type enumsByNumber = map[int32]string |
| 88 |
|
| 89 |
var enumCache sync.Map |
| 90 |
var numFilesCache sync.Map |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
func RegisterEnum(s enumName, _ enumsByNumber, m enumsByName) { |
| 97 |
if _, ok := enumCache.Load(s); ok { |
| 98 |
panic("proto: duplicate enum registered: " + s) |
| 99 |
} |
| 100 |
enumCache.Store(s, m) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
func EnumValueMap(s enumName) enumsByName { |
| 111 |
if v, ok := enumCache.Load(s); ok { |
| 112 |
return v.(enumsByName) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
var protoPkg protoreflect.FullName |
| 119 |
if i := strings.LastIndexByte(s, '.'); i >= 0 { |
| 120 |
protoPkg = protoreflect.FullName(s[:i]) |
| 121 |
} |
| 122 |
v, _ := numFilesCache.Load(protoPkg) |
| 123 |
numFiles, _ := v.(int) |
| 124 |
if protoregistry.GlobalFiles.NumFilesByPackage(protoPkg) == numFiles { |
| 125 |
return nil |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
numFiles = 0 |
| 130 |
protoregistry.GlobalFiles.RangeFilesByPackage(protoPkg, func(fd protoreflect.FileDescriptor) bool { |
| 131 |
walkEnums(fd, func(ed protoreflect.EnumDescriptor) { |
| 132 |
name := protoimpl.X.LegacyEnumName(ed) |
| 133 |
if _, ok := enumCache.Load(name); !ok { |
| 134 |
m := make(enumsByName) |
| 135 |
evs := ed.Values() |
| 136 |
for i := evs.Len() - 1; i >= 0; i-- { |
| 137 |
ev := evs.Get(i) |
| 138 |
m[string(ev.Name())] = int32(ev.Number()) |
| 139 |
} |
| 140 |
enumCache.LoadOrStore(name, m) |
| 141 |
} |
| 142 |
}) |
| 143 |
numFiles++ |
| 144 |
return true |
| 145 |
}) |
| 146 |
numFilesCache.Store(protoPkg, numFiles) |
| 147 |
|
| 148 |
|
| 149 |
if v, ok := enumCache.Load(s); ok { |
| 150 |
return v.(enumsByName) |
| 151 |
} |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
func walkEnums(d interface { |
| 157 |
Enums() protoreflect.EnumDescriptors |
| 158 |
Messages() protoreflect.MessageDescriptors |
| 159 |
}, f func(protoreflect.EnumDescriptor)) { |
| 160 |
eds := d.Enums() |
| 161 |
for i := eds.Len() - 1; i >= 0; i-- { |
| 162 |
f(eds.Get(i)) |
| 163 |
} |
| 164 |
mds := d.Messages() |
| 165 |
for i := mds.Len() - 1; i >= 0; i-- { |
| 166 |
walkEnums(mds.Get(i), f) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
type messageName = string |
| 172 |
|
| 173 |
var messageTypeCache sync.Map |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
func RegisterType(m Message, s messageName) { |
| 180 |
mt := protoimpl.X.LegacyMessageTypeOf(m, protoreflect.FullName(s)) |
| 181 |
if err := protoregistry.GlobalTypes.RegisterMessage(mt); err != nil { |
| 182 |
panic(err) |
| 183 |
} |
| 184 |
messageTypeCache.Store(s, reflect.TypeOf(m)) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func RegisterMapType(m interface{}, s messageName) { |
| 192 |
t := reflect.TypeOf(m) |
| 193 |
if t.Kind() != reflect.Map { |
| 194 |
panic(fmt.Sprintf("invalid map kind: %v", t)) |
| 195 |
} |
| 196 |
if _, ok := messageTypeCache.Load(s); ok { |
| 197 |
panic(fmt.Errorf("proto: duplicate proto message registered: %s", s)) |
| 198 |
} |
| 199 |
messageTypeCache.Store(s, t) |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
func MessageType(s messageName) reflect.Type { |
| 207 |
if v, ok := messageTypeCache.Load(s); ok { |
| 208 |
return v.(reflect.Type) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
var t reflect.Type |
| 213 |
if mt, _ := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(s)); mt != nil { |
| 214 |
t = messageGoType(mt) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if t == nil { |
| 220 |
d, _ := protoregistry.GlobalFiles.FindDescriptorByName(protoreflect.FullName(s)) |
| 221 |
if md, _ := d.(protoreflect.MessageDescriptor); md != nil && md.IsMapEntry() { |
| 222 |
kt := goTypeForField(md.Fields().ByNumber(1)) |
| 223 |
vt := goTypeForField(md.Fields().ByNumber(2)) |
| 224 |
t = reflect.MapOf(kt, vt) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
if t != nil { |
| 230 |
v, _ := messageTypeCache.LoadOrStore(s, t) |
| 231 |
return v.(reflect.Type) |
| 232 |
} |
| 233 |
return nil |
| 234 |
} |
| 235 |
|
| 236 |
func goTypeForField(fd protoreflect.FieldDescriptor) reflect.Type { |
| 237 |
switch k := fd.Kind(); k { |
| 238 |
case protoreflect.EnumKind: |
| 239 |
if et, _ := protoregistry.GlobalTypes.FindEnumByName(fd.Enum().FullName()); et != nil { |
| 240 |
return enumGoType(et) |
| 241 |
} |
| 242 |
return reflect.TypeOf(protoreflect.EnumNumber(0)) |
| 243 |
case protoreflect.MessageKind, protoreflect.GroupKind: |
| 244 |
if mt, _ := protoregistry.GlobalTypes.FindMessageByName(fd.Message().FullName()); mt != nil { |
| 245 |
return messageGoType(mt) |
| 246 |
} |
| 247 |
return reflect.TypeOf((*protoreflect.Message)(nil)).Elem() |
| 248 |
default: |
| 249 |
return reflect.TypeOf(fd.Default().Interface()) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func enumGoType(et protoreflect.EnumType) reflect.Type { |
| 254 |
return reflect.TypeOf(et.New(0)) |
| 255 |
} |
| 256 |
|
| 257 |
func messageGoType(mt protoreflect.MessageType) reflect.Type { |
| 258 |
return reflect.TypeOf(MessageV1(mt.Zero().Interface())) |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
func MessageName(m Message) messageName { |
| 265 |
if m == nil { |
| 266 |
return "" |
| 267 |
} |
| 268 |
if m, ok := m.(interface{ XXX_MessageName() messageName }); ok { |
| 269 |
return m.XXX_MessageName() |
| 270 |
} |
| 271 |
return messageName(protoimpl.X.MessageDescriptorOf(m).FullName()) |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
func RegisterExtension(d *ExtensionDesc) { |
| 279 |
if err := protoregistry.GlobalTypes.RegisterExtension(d); err != nil { |
| 280 |
panic(err) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
type extensionsByNumber = map[int32]*ExtensionDesc |
| 285 |
|
| 286 |
var extensionCache sync.Map |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
func RegisteredExtensions(m Message) extensionsByNumber { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
s := MessageName(m) |
| 297 |
v, _ := extensionCache.Load(s) |
| 298 |
xs, _ := v.(extensionsByNumber) |
| 299 |
if protoregistry.GlobalTypes.NumExtensionsByMessage(protoreflect.FullName(s)) == len(xs) { |
| 300 |
return xs |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
xs = make(extensionsByNumber) |
| 305 |
protoregistry.GlobalTypes.RangeExtensionsByMessage(protoreflect.FullName(s), func(xt protoreflect.ExtensionType) bool { |
| 306 |
if xd, ok := xt.(*ExtensionDesc); ok { |
| 307 |
xs[int32(xt.TypeDescriptor().Number())] = xd |
| 308 |
} else { |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
} |
| 313 |
return true |
| 314 |
}) |
| 315 |
extensionCache.Store(s, xs) |
| 316 |
return xs |
| 317 |
} |