order

package
v1.32.1-cherry-pick-fo... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2024 License: MIT, BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package order provides ordered access to messages and maps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RangeEntries

func RangeEntries(es EntryRanger, less KeyOrder, fn VisitEntry)

RangeEntries iterates over the entries of es according to the specified order.

func RangeFields

func RangeFields(fs FieldRanger, less FieldOrder, fn VisitField)

RangeFields iterates over the fields of fs according to the specified order.

Types

type EntryRanger

type EntryRanger interface{ Range(VisitEntry) }

EntryRanger is an interface for visiting all fields in a message. The protoreflect.Map type implements this interface.

type FieldOrder

type FieldOrder func(x, y protoreflect.FieldDescriptor) bool

FieldOrder specifies the ordering to visit message fields. It is a function that reports whether x is ordered before y.

var (
	// AnyFieldOrder specifies no specific field ordering.
	AnyFieldOrder FieldOrder = nil

	// LegacyFieldOrder sorts fields in the same ordering as emitted by
	// wire serialization in the github.com/golang/protobuf implementation.
	LegacyFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool {
		ox, oy := x.ContainingOneof(), y.ContainingOneof()
		inOneof := func(od protoreflect.OneofDescriptor) bool {
			return od != nil && !od.IsSynthetic()
		}

		if x.IsExtension() != y.IsExtension() {
			return x.IsExtension() && !y.IsExtension()
		}

		if inOneof(ox) != inOneof(oy) {
			return !inOneof(ox) && inOneof(oy)
		}

		if inOneof(ox) && inOneof(oy) && ox != oy {
			return ox.Index() < oy.Index()
		}

		return x.Number() < y.Number()
	}

	// NumberFieldOrder sorts fields by their field number.
	NumberFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool {
		return x.Number() < y.Number()
	}

	// IndexNameFieldOrder sorts non-extension fields before extension fields.
	// Non-extensions are sorted according to their declaration index.
	// Extensions are sorted according to their full name.
	IndexNameFieldOrder FieldOrder = func(x, y protoreflect.FieldDescriptor) bool {

		if x.IsExtension() != y.IsExtension() {
			return !x.IsExtension() && y.IsExtension()
		}

		if x.IsExtension() && y.IsExtension() {
			return x.FullName() < y.FullName()
		}

		return x.Index() < y.Index()
	}
)

type FieldRanger

type FieldRanger interface{ Range(VisitField) }

FieldRnger is an interface for visiting all fields in a message. The protoreflect.Message type implements this interface.

type KeyOrder

type KeyOrder func(x, y protoreflect.MapKey) bool

KeyOrder specifies the ordering to visit map entries. It is a function that reports whether x is ordered before y.

var (
	// AnyKeyOrder specifies no specific key ordering.
	AnyKeyOrder KeyOrder = nil

	// GenericKeyOrder sorts false before true, numeric keys in ascending order,
	// and strings in lexicographical ordering according to UTF-8 codepoints.
	GenericKeyOrder KeyOrder = func(x, y protoreflect.MapKey) bool {
		switch x.Interface().(type) {
		case bool:
			return !x.Bool() && y.Bool()
		case int32, int64:
			return x.Int() < y.Int()
		case uint32, uint64:
			return x.Uint() < y.Uint()
		case string:
			return x.String() < y.String()
		default:
			panic("invalid map key type")
		}
	}
)

type VisitEntry

type VisitEntry = func(protoreflect.MapKey, protoreflect.Value) bool

VisitEntry is called every time a map entry is visited.

type VisitField

VisitField is called every time a message field is visited.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL