...
1
2
3
4
5 package order
6
7 import (
8 "google.golang.org/protobuf/reflect/protoreflect"
9 )
10
11
12
13 type FieldOrder func(x, y protoreflect.FieldDescriptor) bool
14
15 var (
16
17 AnyFieldOrder FieldOrder = nil
18
19
20
21 LegacyFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool {
22 ox, oy := x.ContainingOneof(), y.ContainingOneof()
23 inOneof := func(od protoreflect.OneofDescriptor) bool {
24 return od != nil && !od.IsSynthetic()
25 }
26
27
28 if x.IsExtension() != y.IsExtension() {
29 return x.IsExtension() && !y.IsExtension()
30 }
31
32 if inOneof(ox) != inOneof(oy) {
33 return !inOneof(ox) && inOneof(oy)
34 }
35
36 if inOneof(ox) && inOneof(oy) && ox != oy {
37 return ox.Index() < oy.Index()
38 }
39
40 return x.Number() < y.Number()
41 }
42
43
44 NumberFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool {
45 return x.Number() < y.Number()
46 }
47
48
49
50
51 IndexNameFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool {
52
53 if x.IsExtension() != y.IsExtension() {
54 return !x.IsExtension() && y.IsExtension()
55 }
56
57 if x.IsExtension() && y.IsExtension() {
58 return x.FullName() < y.FullName()
59 }
60
61 return x.Index() < y.Index()
62 }
63 )
64
65
66
67 type KeyOrder func(x, y protoreflect.MapKey) bool
68
69 var (
70
71 AnyKeyOrder KeyOrder = nil
72
73
74
75 GenericKeyOrder KeyOrder = func(x, y protoreflect.MapKey) bool {
76 switch x.Interface().(type) {
77 case bool:
78 return !x.Bool() && y.Bool()
79 case int32, int64:
80 return x.Int() < y.Int()
81 case uint32, uint64:
82 return x.Uint() < y.Uint()
83 case string:
84 return x.String() < y.String()
85 default:
86 panic("invalid map key type")
87 }
88 }
89 )
90
View as plain text