3 // license that can be found in the LICENSE file. |
3 // license that can be found in the LICENSE file. |
4 |
4 |
5 package order |
5 package order |
6 |
6 |
7 import ( |
7 import ( |
8 pref "google.golang.org/protobuf/reflect/protoreflect" |
8 "google.golang.org/protobuf/reflect/protoreflect" |
9 ) |
9 ) |
10 |
10 |
11 // FieldOrder specifies the ordering to visit message fields. |
11 // FieldOrder specifies the ordering to visit message fields. |
12 // It is a function that reports whether x is ordered before y. |
12 // It is a function that reports whether x is ordered before y. |
13 type FieldOrder func(x, y pref.FieldDescriptor) bool |
13 type FieldOrder func(x, y protoreflect.FieldDescriptor) bool |
14 |
14 |
15 var ( |
15 var ( |
16 // AnyFieldOrder specifies no specific field ordering. |
16 // AnyFieldOrder specifies no specific field ordering. |
17 AnyFieldOrder FieldOrder = nil |
17 AnyFieldOrder FieldOrder = nil |
18 |
18 |
19 // LegacyFieldOrder sorts fields in the same ordering as emitted by |
19 // LegacyFieldOrder sorts fields in the same ordering as emitted by |
20 // wire serialization in the github.com/golang/protobuf implementation. |
20 // wire serialization in the github.com/golang/protobuf implementation. |
21 LegacyFieldOrder FieldOrder = func(x, y pref.FieldDescriptor) bool { |
21 LegacyFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool { |
22 ox, oy := x.ContainingOneof(), y.ContainingOneof() |
22 ox, oy := x.ContainingOneof(), y.ContainingOneof() |
23 inOneof := func(od pref.OneofDescriptor) bool { |
23 inOneof := func(od protoreflect.OneofDescriptor) bool { |
24 return od != nil && !od.IsSynthetic() |
24 return od != nil && !od.IsSynthetic() |
25 } |
25 } |
26 |
26 |
27 // Extension fields sort before non-extension fields. |
27 // Extension fields sort before non-extension fields. |
28 if x.IsExtension() != y.IsExtension() { |
28 if x.IsExtension() != y.IsExtension() { |
39 // Fields sorted by field number. |
39 // Fields sorted by field number. |
40 return x.Number() < y.Number() |
40 return x.Number() < y.Number() |
41 } |
41 } |
42 |
42 |
43 // NumberFieldOrder sorts fields by their field number. |
43 // NumberFieldOrder sorts fields by their field number. |
44 NumberFieldOrder FieldOrder = func(x, y pref.FieldDescriptor) bool { |
44 NumberFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool { |
45 return x.Number() < y.Number() |
45 return x.Number() < y.Number() |
46 } |
46 } |
47 |
47 |
48 // IndexNameFieldOrder sorts non-extension fields before extension fields. |
48 // IndexNameFieldOrder sorts non-extension fields before extension fields. |
49 // Non-extensions are sorted according to their declaration index. |
49 // Non-extensions are sorted according to their declaration index. |
50 // Extensions are sorted according to their full name. |
50 // Extensions are sorted according to their full name. |
51 IndexNameFieldOrder FieldOrder = func(x, y pref.FieldDescriptor) bool { |
51 IndexNameFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool { |
52 // Non-extension fields sort before extension fields. |
52 // Non-extension fields sort before extension fields. |
53 if x.IsExtension() != y.IsExtension() { |
53 if x.IsExtension() != y.IsExtension() { |
54 return !x.IsExtension() && y.IsExtension() |
54 return !x.IsExtension() && y.IsExtension() |
55 } |
55 } |
56 // Extensions sorted by fullname. |
56 // Extensions sorted by fullname. |
62 } |
62 } |
63 ) |
63 ) |
64 |
64 |
65 // KeyOrder specifies the ordering to visit map entries. |
65 // KeyOrder specifies the ordering to visit map entries. |
66 // It is a function that reports whether x is ordered before y. |
66 // It is a function that reports whether x is ordered before y. |
67 type KeyOrder func(x, y pref.MapKey) bool |
67 type KeyOrder func(x, y protoreflect.MapKey) bool |
68 |
68 |
69 var ( |
69 var ( |
70 // AnyKeyOrder specifies no specific key ordering. |
70 // AnyKeyOrder specifies no specific key ordering. |
71 AnyKeyOrder KeyOrder = nil |
71 AnyKeyOrder KeyOrder = nil |
72 |
72 |
73 // GenericKeyOrder sorts false before true, numeric keys in ascending order, |
73 // GenericKeyOrder sorts false before true, numeric keys in ascending order, |
74 // and strings in lexicographical ordering according to UTF-8 codepoints. |
74 // and strings in lexicographical ordering according to UTF-8 codepoints. |
75 GenericKeyOrder KeyOrder = func(x, y pref.MapKey) bool { |
75 GenericKeyOrder KeyOrder = func(x, y protoreflect.MapKey) bool { |
76 switch x.Interface().(type) { |
76 switch x.Interface().(type) { |
77 case bool: |
77 case bool: |
78 return !x.Bool() && y.Bool() |
78 return !x.Bool() && y.Bool() |
79 case int32, int64: |
79 case int32, int64: |
80 return x.Int() < y.Int() |
80 return x.Int() < y.Int() |