config

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeFile

func DecodeFile(file string, c *Config) error

Types

type Bool

type Bool struct {
	Value bool
	IsSet bool
}

Bool implements both the flag.Value and the yaml.Unmarshaler interfaces.

func (Bool) Get

func (b Bool) Get() interface{}

Get implements the flag.Getter interface.

func (Bool) IsBoolFlag

func (b Bool) IsBoolFlag() bool

IsBoolFlag indicates that the Bool type can be used as a boolean flag.

func (*Bool) Set

func (b *Bool) Set(value string) error

Set implements the flag.Value interface.

func (Bool) String

func (b Bool) String() string

String implements the flag.Value interface.

func (*Bool) UnmarshalYAML

func (b *Bool) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type Config

type Config struct {
	// The file from which the config was decoded. Used for error reporting.
	File String `yaml:"-"`
	// The directory in which the tool will search for files to process.
	// If not provided, the current working directory will be used by default.
	WorkDir String `yaml:"working_directory"`
	// If set to true, the tool will search the hierarchy of the working
	// directory for files to process.
	Recursive Bool `yaml:"recursive"`
	// List of files to be used as input for the tool.
	// The files must be located in the working directory.
	FileList StringSlice `yaml:"file_list"`
	// List of regular expressions to match input files that the tool
	// should process. The regular expressions must match files that
	// are located in the working directory.
	FilePatternList StringSlice `yaml:"file_pattern_list"`
	// A string containing the format to be used for generating
	// the name of the output files. The format string MUST end
	// with the ".go" file extension.
	//
	// Inside the format string the percent sign ("%") can be used as
	// the placeholder to be replaced by the tool with the base name of
	// the source file. If no placeholder is present in the format string
	// then the base name of the source file will simply be prefixed
	// to the format string.
	//
	// If not provided, the format will default to "%_valid.go".
	OutNameFormat String `yaml:"out_name_format"`
	// A string containing a regular expression that will be used by the tool
	// to identify the struct types for which to generate the validation code.
	//
	// If not provided, the pattern will default to "^(?i:\w*Validator)$".
	ValidatorNamePattern String `yaml:"validator_name_pattern"`
	// Configures the code generation of the handling of validation errors.
	ErrorHandling ErrorHandlingConfig `yaml:"error_handling"`
	// List of custom rules to be made available to the tool.
	Rules []RuleConfig `yaml:"rules"`
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig() Config

func (*Config) FileFilterFunc

func (c *Config) FileFilterFunc() (filter func(filePath string) bool)

func (*Config) MergeAndCheck

func (c *Config) MergeAndCheck() error

MergeAndCheck merges the receiver with a config file, if available.

func (*Config) ValidatorRegexp

func (c *Config) ValidatorRegexp() *regexp.Regexp

type Error

type Error struct {
	C ErrorCode
	// contains filtered or unexported fields
}

func (*Error) ErrType

func (e *Error) ErrType() string

func (*Error) Error

func (e *Error) Error() string

func (*Error) File

func (e *Error) File() string

func (*Error) FilePos

func (e *Error) FilePos() string

func (*Error) Key

func (e *Error) Key() string

func (*Error) OriginalError

func (e *Error) OriginalError() string

func (*Error) TargetType

func (e *Error) TargetType() string

func (*Error) Value

func (e *Error) Value() string

func (*Error) WDir

func (e *Error) WDir() string

func (*Error) YAMLNode

func (e *Error) YAMLNode() string

func (*Error) YAMLNodeLine

func (e *Error) YAMLNodeLine() string

type ErrorCode

type ErrorCode uint
const (
	ERR_FLAG_DECODE    ErrorCode // flag decoding failed
	ERR_YAML_ERROR               // error from gopkg.in/yaml.v3
	ERR_YAML_TYPE                // unsupported yaml type
	ERR_YAML_FILE                // yaml file is invalid
	ERR_OBJECT_IDENT             // invalid format for object identifier
	ERR_JOIN_OP                  // invalid join op value
	ERR_CONFIG_FILE              // config file unusable
	ERR_WORK_DIR                 // working_directory unusable
	ERR_FILE_ITEM                // file_list item unusable
	ERR_OUTNAME_FORMAT           // invalid output name format
	ERR_PATTERN                  // invalid regular expression
	ERR_FKEY_TAG                 // invalid field key tag
	ERR_FKEY_SEP                 // invalid field key separator
	ERR_RULE_NONAME              // rule with no name
	ERR_RULE_NOFUNC              // missing rule func
	ERR_RULE_DUPNAME             // duplicate rule name
)

func (ErrorCode) Id

func (e ErrorCode) Id() string

type ErrorHandlingConfig

type ErrorHandlingConfig struct {
	// Configures how field keys should be constructed.
	//
	// Field keys are used for error reporting by the generated code.
	// When a field fails validation the field's key, along with other
	// details, will be passed as an argument to the client's
	// implementation of the error handling code.
	FieldKey FieldKeyConfig `yaml:"field_key"`
	// The identifier of a function that the generated code should
	// use for constructing custom, application-specific errors.
	// The function's signature MUST be the following:
	//
	//     func(key string, val any, rule string, args ...any) error
	//
	Constructor ObjectIdent `yaml:"constructor"`
	// The identifier of a type that the generated code should use
	// for constructing & aggregation of custom, application-specific
	// errors. The type MUST implement the following interface:
	//
	//     interface {
	//         Error(key string, val any, rule string, args ...any)
	//         Out() error
	//     }
	//
	Aggregator ObjectIdent `yaml:"aggregator"`
}

type FieldKeyConfig

type FieldKeyConfig struct {
	// If non-empty string, specifies the struct tag whose value will be
	// used for constructing the field keys. If explicitly set to an empty
	// string, the generator will default to use the fields' names for
	// constructing the field keys.
	//
	// A valid tag must begin with a letter (A-z) or an underscore (_),
	// subsequent characters in the tag can be letters, underscores,
	// and digits (0-9).
	//
	// If not provided, the tag "json" will be used by default.
	Tag String `yaml:"tag"`
	// When set to true, a nested struct field's key will be constructed by
	// joining it together with all of its parent fields. When false, a nested
	// struct field's key will be constructed only from that field's tag or name.
	//
	// If not provided, `true` will be used by default.
	Join Bool `yaml:"join"`
	// The separator to be used for joining fields' tags or names
	// when constructing field keys.
	//
	// The separator MUST be a single, one byte long, character.
	//
	// If not provided, the separator "." will be used by default.
	Separator String `yaml:"separator"`
}

type JoinOp

type JoinOp uint

JoinOp represents a logical join operator.

const (
	JOIN_NOT JoinOp
	JOIN_AND
	JOIN_OR
)

func (JoinOp) String

func (op JoinOp) String() string

func (*JoinOp) UnmarshalYAML

func (op *JoinOp) UnmarshalYAML(n *yaml.Node) error

type ObjectIdent

type ObjectIdent struct {
	Pkg   string // the package path of the Go object
	Name  string // the name of the Go object
	IsSet bool
}

ObjectIdent represents a Go type or function identifier. It implements both the flag.Value and the yaml.Unmarshaler interfaces.

func (ObjectIdent) Get

func (id ObjectIdent) Get() interface{}

Get implements the flag.Getter interface.

func (*ObjectIdent) Set

func (id *ObjectIdent) Set(value string) error

Set implements the flag.Value interface.

func (ObjectIdent) String

func (id ObjectIdent) String() string

String implements the flag.Value interface.

func (*ObjectIdent) UnmarshalYAML

func (id *ObjectIdent) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type RuleArgConfig

type RuleArgConfig struct {
	// The rule argument's default value. If nil, then the
	// rule argument's value MUST be provided in the struct tag.
	//
	// If not nil, the value must be a scalar value.
	Default *Scalar `yaml:"default"`
	// If options is empty, then ANY value can be provided for
	// the argument in the rule's struct tag.
	//
	// If options is NOT empty, then it is considered to represent, together
	// with the default value, the *complete* set of valid values that can be
	// provided as the argument in the rule's struct tag.
	Options []RuleArgOption `yaml:"options"`
}

type RuleArgOption

type RuleArgOption struct {
	// Value specifies the value that the generator should supply
	// as the rule's argument in the generated code.
	Value Scalar `yaml:"value"`
	// Alias is an alternative identifier of the argument's value that
	// can be used within the rule's struct tag. This field is optional.
	Alias string `yaml:"alias"`
}

type RuleConfig

type RuleConfig struct {
	// The function's name qualified with
	// the path of the function's package.
	Func ObjectIdent `yaml:"func"`
	// The spec for the function's rule. Optional if
	// the func's doc already has a valid config.
	Rule *RuleSpec `yaml:"rule"`
}

type RuleErrorConfig

type RuleErrorConfig struct {
	// The text of the error message.
	Text string `yaml:"text,omitempty"`
	// If true the generated error message
	// will include the rule's arguments.
	WithArgs bool `yaml:"with_args,omitempty"`
	// The separator used to join the rule's
	// arguments for the error message.
	ArgSep string `yaml:"arg_sep,omitempty"`
	// The text to be appended after the list of arguments.
	ArgSuffix string `yaml:"arg_suffix,omitempty"`
}

type RuleSpec

type RuleSpec struct {
	// The name of the rule.
	Name string `yaml:"name"`
	// The configuration for the rule's arguments.
	//
	// NOTE: If args is NOT empty, then it MUST contain a number
	// of elements that is compatible with the parameters of the
	// rule's function.
	Args []RuleArgConfig `yaml:"args"`
	// ArgMin can be used to enforce the number of arguments
	// that the rule must accept.
	//
	// - ArgMin is optional, if omitted its value will
	//   be inferred from the function's signature.
	// - When ArgMin is provided it will be used to enforce
	//   the valid number of arguments for variadic functions.
	// - When ArgMin is provided it MUST be compatible
	//   with the function's signature, if it isn't then
	//   the tool will exit with an error.
	ArgMin *uint `yaml:"arg_min"`
	// ArgMax can be used to override the upper limit of the number
	// of arguments that the rule should be allowed to accept.
	// A negative ArgMax can be used to indicate that there's no
	// upper limit to the number of arguments.
	//
	// - ArgMax is optional, if omitted its value will
	//   be inferred from the function's signature.
	// - When ArgMax is provided it will be used to enforce
	//   the valid number of arguments for variadic functions.
	// - When ArgMax is provided it MUST be compatible
	//   with the function's signature, if it isn't then
	//   the tool will exit with an error.
	ArgMax *int `yaml:"arg_max"`
	// The configuration for the error that should be generated for the rule.
	Error RuleErrorConfig `yaml:"error"`
	// The join operator that should be used to join
	// multiple instances of the rule into a single one.
	//
	// The value MUST be one of: "AND", "OR", or "NOT" (case insensitive).
	JoinOp JoinOp `yaml:"join_op"`
}

type Scalar

type Scalar struct {
	// The type of the scalar value.
	Type ScalarType `yaml:"-"`
	// The literal string representation of the value.
	Value string `yaml:"-"`
}

Scalar represents a scalar-only value.

func (Scalar) MarshalYAML

func (s Scalar) MarshalYAML() (interface{}, error)

func (*Scalar) UnmarshalYAML

func (s *Scalar) UnmarshalYAML(n *yaml.Node) error

type ScalarType

type ScalarType uint
const (
	NIL ScalarType
	BOOL
	STRING
	FLOAT
	INT
)

type String

type String struct {
	Value string
	IsSet bool
}

String implements both the flag.Value and the yaml.Unmarshaler interfaces.

func (String) Get

func (s String) Get() interface{}

Get implements the flag.Getter interface.

func (*String) Set

func (s *String) Set(value string) error

Set implements the flag.Value interface.

func (String) String

func (s String) String() string

String implements the flag.Value interface.

func (*String) UnmarshalYAML

func (s *String) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type StringSlice

type StringSlice struct {
	Value []string
	IsSet bool
}

StringSlice implements both the flag.Value and the yaml.Unmarshaler interfaces.

func (StringSlice) Get

func (ss StringSlice) Get() interface{}

Get implements the flag.Getter interface.

func (*StringSlice) Set

func (ss *StringSlice) Set(value string) error

Set implements the flag.Value interface.

func (StringSlice) String

func (ss StringSlice) String() string

String implements the flag.Value interface.

func (*StringSlice) UnmarshalYAML

func (ss *StringSlice) UnmarshalYAML(n *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

Jump to

Keyboard shortcuts

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