protopath

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package protopath defines methods for getting and setting nested proto fields using selectors.

A path is defined as a period-delimited list of Go proto field names, i.e. "Msg.Field.SubField". Repeated fields can be indexed using a numeric field name, i.e. "Msg.RepeatedField.0.SubField". To refer to the last repeat element use the special field name "-1". Setting index "-1" will extend an array, not replace the last element.

Index

Constants

View Source
const Zero zeroType = 0

Zero represents the zero value of any type, it can be used in Set as the value argument to set the value at the path to its corresponding zero value, or Get as the default value, to return the zero value of the field being retrieved if the field is valid but not populated.

Variables

This section is empty.

Functions

func Get

func Get(m proto.Message, path Path, defVal interface{}) (interface{}, error)

Get retrieves a value from a V2 proto at `path`, or returns a default value. An error will occur if the path is invalid, or the default value's type is incompatible with the type of the field at `path`.

If the last value of `path` is a oneof then the populated field of the oneof will be returned. `defVal` is ignored in this case, an error will be returned if the oneof is not populated.

If the input message is nil, the default value will be returned.

func Set

func Set(m proto.Message, path Path, value interface{}) error

Set sets the value of a proto at `path`. An error will occur if the path is invalid, or the specified value's type is incompatible with the type of the field at `path`.

If the last value of `path` is a oneof then the field of the oneof that is assignable to value will be set. If multiple fields have the same type an error is returned.

If the input message is nil, an error will be returned.

Types

type Path

type Path struct {
	// contains filtered or unexported fields
}

Path is a selector for a proto field.

func NewPath

func NewPath(p string) Path

NewPath creates a Path from a string definition using proto field names.

func (Path) String

func (p Path) String() string

String implements the Stringer interface, returns a string representation of the path.

Jump to

Keyboard shortcuts

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