blob: 517c4e2a041a8ea72b21ac653e969ffa0314f82c [file] [log] [blame]
Andrea Campanella3614a922021-02-25 12:40:42 +01001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package fieldsort defines an ordering of fields.
6//
7// The ordering defined by this package matches the historic behavior of the proto
8// package, placing extensions first and oneofs last.
9//
10// There is no guarantee about stability of the wire encoding, and users should not
11// depend on the order defined in this package as it is subject to change without
12// notice.
13package fieldsort
14
15import (
16 "google.golang.org/protobuf/reflect/protoreflect"
17)
18
19// Less returns true if field a comes before field j in ordered wire marshal output.
20func Less(a, b protoreflect.FieldDescriptor) bool {
21 ea := a.IsExtension()
22 eb := b.IsExtension()
23 oa := a.ContainingOneof()
24 ob := b.ContainingOneof()
25 switch {
26 case ea != eb:
27 return ea
28 case oa != nil && ob != nil:
29 if oa == ob {
30 return a.Number() < b.Number()
31 }
32 return oa.Index() < ob.Index()
33 case oa != nil && !oa.IsSynthetic():
34 return false
35 case ob != nil && !ob.IsSynthetic():
36 return true
37 default:
38 return a.Number() < b.Number()
39 }
40}