structmask

package
v0.0.0-...-678c4f2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Overview

Package structmask implements a functionality similar to google.protobuf.FieldMask, but which applies only to google.protobuf.Struct.

A google.protobuf.FieldMask can refer only to valid protobuf fields and "google.golang.org/protobuf" asserts that when serializing the field mask. It makes this mechanism unusable for targeting "unusual" struct fields (for example ones containing '.'). Additionally, google.protobuf.FieldMask doesn't support wildcard matches (with '*', since it is not a valid proto field name).

Index

Constants

This section is empty.

Variables

View Source
var File_go_chromium_org_luci_common_proto_structmask_structmask_proto protoreflect.FileDescriptor

Functions

This section is empty.

Types

type Filter

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

Filter knows how to use StructMask to filter google.protobuf.Struct.

Construct it using NewFilter.

func NewFilter

func NewFilter(mask []*StructMask) (*Filter, error)

NewFilter returns a filter that filters structs according to the struct mask.

Returns an error if the struct mask is malformed. If `mask` is empty, returns a filter that doesn't actually filter anything.

func (*Filter) Apply

func (f *Filter) Apply(s *structpb.Struct) *structpb.Struct

Apply returns a shallow copy of the struct, selecting only elements matching the mask.

The result may reuse fields of the original struct (i.e. it copies pointers, whenever possible, not actual objects). In extreme case of mask `*` it will return `s` as is.

If you need to modify the result, consider explicitly making a deep copy with proto.Clone first.

If given `nil`, returns `nil` as well.

type StructMask

type StructMask struct {

	// A field path inside the struct to select.
	//
	// Each item can be:
	//   - `some_value` - a concrete dict key to follow (unless it is a number or
	//     includes `*`, use quotes in this case).
	//   - `"some_value"` - same, but quoted. Useful for selecting `*` or numbers
	//     literally. See https://pkg.go.dev/strconv#Unquote for syntax.
	//   - `<number>` (e.g. `0`) - a zero-based list index to follow.
	//     **Not implemented**.
	//   - `*` - follow all dict keys and all list elements. Applies **only** to
	//     dicts and lists. Trying to recurse into a number or a string results
	//     in an empty match.
	//
	// When examining a value the following exceptional conditions result in
	// an empty match, which is represented by `null` for list elements or
	// omissions of the field for dicts:
	//   - Trying to follow a dict key while examining a list.
	//   - Trying to follow a key which is not present in the dict.
	//   - Trying to use `*` mask with values that aren't dicts or lists.
	//
	// When using `*`, the result is always a subset of the input. In particular
	// this is important when filtering lists: if a list of size N is selected by
	// the mask, then the filtered result will also always be a list of size N,
	// with elements filtered further according to the rest of the mask (perhaps
	// resulting in `null` elements on type mismatches, as explained above).
	Path []string `protobuf:"bytes,1,rep,name=path,proto3" json:"path,omitempty"`
	// contains filtered or unexported fields
}

StructMask selects a subset of a google.protobuf.Struct.

Usually used as a repeated field, to allow specifying a union of different subsets.

func (*StructMask) Descriptor deprecated

func (*StructMask) Descriptor() ([]byte, []int)

Deprecated: Use StructMask.ProtoReflect.Descriptor instead.

func (*StructMask) GetPath

func (x *StructMask) GetPath() []string

func (*StructMask) ProtoMessage

func (*StructMask) ProtoMessage()

func (*StructMask) ProtoReflect

func (x *StructMask) ProtoReflect() protoreflect.Message

func (*StructMask) Reset

func (x *StructMask) Reset()

func (*StructMask) String

func (x *StructMask) String() string

Jump to

Keyboard shortcuts

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