directive

package
v1.39.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(errs *perr.List, d *Directive, spec ValidateSpec) (ok bool)

Validate checks that the directive is valid according to spec.

Types

type Directive

type Directive struct {
	AST *ast.CommentGroup // the comment group containing the directive

	Name    string  // "foo" in "encore:foo"
	Options []Field // options that are enabled ("public" and "raw" in "encore:api public raw path=/foo")
	Fields  []Field // key-value pairs ({"path": "/foo"} in "encore:api public raw path=/foo")
	Tags    []Field // tag names ("tag:foo" in "encore:api public tag:foo")
	// contains filtered or unexported fields
}

Directive represents a parsed "encore:" directive.

func Parse

func Parse(errs *perr.List, cg *ast.CommentGroup) (dir *Directive, doc string, ok bool)

Parse parses the encore:foo directives in cg. It returns the parsed directives, if any, and the remaining doc text after stripping the directive lines.

func (Directive) End

func (d Directive) End() token.Pos

func (Directive) Get

func (d Directive) Get(name string) string

Get returns the value of the given field, if any. If the field doesn't exist it reports "".

func (Directive) GetList

func (d Directive) GetList(name string) []string

GetList returns the value of the given field, split by commas. If the field doesn't exist it reports nil.

func (Directive) HasOption

func (d Directive) HasOption(name string) bool

HasOption reports whether the directive contains the given option.

func (Directive) Pos

func (d Directive) Pos() token.Pos

func (Directive) String

func (d Directive) String() string

String returns the string representation of d.

type Field

type Field struct {
	Key   string
	Value string
	// contains filtered or unexported fields
}

func (Field) End

func (f Field) End() token.Pos

func (Field) Equal

func (f Field) Equal(other Field) bool

Equal reports whether two fields are equal. It's implemented for testing purposes.

func (Field) List

func (f Field) List() []string

List returns the field value as a list, split by commas.

func (Field) Pos

func (f Field) Pos() token.Pos

type ValidateSpec

type ValidateSpec struct {
	// AllowedFields and AllowedOptions are the allowed fields and options.
	// Expected values must be found in the corresponding list.
	AllowedOptions []string
	AllowedFields  []string

	// ValidateOption, if non-nil, is called for each option in the directive.
	ValidateOption func(*perr.List, Field) (ok bool)

	// ValidateField, if non-nil, is called for each field in the directive.
	ValidateField func(*perr.List, Field) (ok bool)

	// ValidateTag, if non-nil, is called for each tag in the directive.
	// It is called with the whole tag, including the "tag:" prefix.
	ValidateTag func(*perr.List, Field) (ok bool)
}

Jump to

Keyboard shortcuts

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