rivertags

package
v0.35.3 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package rivertags decodes a struct type into river object and structural tags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name  []string // Name of tagged field.
	Index []int    // Index into field. Use [reflectutil.GetOrAlloc] to retrieve a Value.
	Flags Flags    // Flags assigned to field.
}

Field is a tagged field within a struct.

func Get

func Get(ty reflect.Type) []Field

Get returns the list of tagged fields for some struct type ty. Get panics if ty is not a struct type.

Get examines each tagged field in ty for a river key. The river key is then parsed as containing a name for the field, followed by a required comma-separated list of options. The name may be empty for fields which do not require a name. Get will ignore any field that is not tagged with a river key.

Get will treat anonymous struct fields as if the inner fields were fields in the outer struct.

Examples of struct field tags and their meanings:

// Field is used as a required block named "my_block".
Field struct{} `river:"my_block,block"`

// Field is used as an optional block named "my_block".
Field struct{} `river:"my_block,block,optional"`

// Field is used as a required attribute named "my_attr".
Field string `river:"my_attr,attr"`

// Field is used as an optional attribute named "my_attr".
Field string `river:"my_attr,attr,optional"`

// Field is used for storing the label of the block which the struct
// represents.
Field string `river:",label"`

// Attributes and blocks inside of Field are exposed as top-level fields.
Field struct{} `river:",squash"`

Blocks []struct{} `river:"my_block_prefix,enum"`

With the exception of the `river:",label"` and `river:",squash" tags, all tagged fields must have a unique name.

The type of tagged fields may be any Go type, with the exception of `river:",label"` tags, which must be strings.

func (Field) Equals added in v0.32.0

func (f Field) Equals(other Field) bool

Equals returns true if two fields are equal.

func (Field) IsAttr

func (f Field) IsAttr() bool

IsAttr returns whether f is for an attribute.

func (Field) IsBlock

func (f Field) IsBlock() bool

IsBlock returns whether f is for a block.

func (Field) IsEnum added in v0.32.0

func (f Field) IsEnum() bool

IsEnum returns whether f represents an enum of blocks, where only one block is set at a time.

func (Field) IsLabel added in v0.33.0

func (f Field) IsLabel() bool

IsLabel returns whether f is label.

func (Field) IsOptional

func (f Field) IsOptional() bool

IsOptional returns whether f is optional.

type Flags

type Flags uint

Flags is a bitmap of flags associated with a field on a struct.

const (
	FlagAttr  Flags = 1 << iota // FlagAttr treats a field as attribute
	FlagBlock                   // FlagBlock treats a field as a block
	FlagEnum                    // FlagEnum treats a field as an enum of blocks

	FlagOptional // FlagOptional marks a field optional for decoding/encoding
	FlagLabel    // FlagLabel will store block labels in the field
	FlagSquash   // FlagSquash will expose inner fields from a struct as outer fields.
)

Valid flags.

func (Flags) GoString

func (f Flags) GoString() string

GoString returns the %#v format of Flags.

func (Flags) String

func (f Flags) String() string

String returns the flags as a string.

Jump to

Keyboard shortcuts

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