Documentation
¶
Index ¶
- Constants
- Variables
- func GetConditionByType(conditionFull string, conditionType ConditionType) (string, error)
- func LookupConditionType(conType ConditionType) error
- func LookupOperator(operator Operator) error
- type AstValue
- type AstValueType
- type ConditionGroup
- type ConditionType
- type Group
- type Operator
- type RootNode
- type Token
- type TokenType
- type Validation
- type ValidatorMap
- type ValidatorType
Constants ¶
const ( // Group states GrpStart state = iota GrpOpen GrpEnd // Condition states ConType ConValue ConEnd )
Available states for each type used in parsing
const VLD string = "vld"
Default tag type.
Variables ¶
var ValidConditionTypes = map[ConditionType]int{ NONE: 0, EQUAL: 1, NOT_EQUAL: 2, MIN_VALUE: 3, MAX_VALUE: 4, CONTAINS: 5, NOT_CONTAINS: 6, FROM: 7, NOT_FROM: 8, REGX: 9, }
Functions ¶
func GetConditionByType ¶
func GetConditionByType(conditionFull string, conditionType ConditionType) (string, error)
GetConditionByType extracts the condition value from a string based on the condition type. It trims the prefix of the condition type from the string and returns the remaining part. If the condition type is not valid or the value is empty, an error is returned.
func LookupConditionType ¶ added in v0.2.0
func LookupConditionType(conType ConditionType) error
LookupConditionType checks our validConditionType map for the scanned condition type. If not found, an error is returned.
func LookupOperator ¶ added in v0.2.0
LookupOperator checks our validOperator map for the scanned operator. If not found, an error is returned.
Types ¶
type AstValue ¶ added in v0.2.0
type AstValue struct {
Type AstValueType
ConditionType ConditionType
ConditionValue string
ConditionGroup ConditionGroup
Operator Operator
Start int
End int
}
AstValue (=abstract syntax tree value) holds a Type ("Condition" or "Group") as well as a `ConditionType` and `ConditionValue`. The ConditionType is a model.ConditionType and the ConditionValue is any string (numbers are also represented as string).
func (AstValue) AstConditionToString ¶ added in v0.2.0
AstConditionToString converts the AstValue's ConditionType and ConditionValue to a string representation. If the AstValue has an Operator, it includes that in the string. The resulting string is formatted as "<ConditionType>'<ConditionValue>' <Operator>" if the Operator is present, or as "<ConditionType>'<ConditionValue>'" if the Operator is not present.
func (AstValue) AstGroupToString ¶ added in v0.2.0
AstGroupToString converts the AstValue's ConditionGroup to a string representation. It iterates over each AstValue in the group and formats it based on its type. If the AstValue is a group, it recursively calls itself to get the string representation of the group. If the AstValue is a condition, it formats it as a string with its ConditionType and ConditionValue. The resulting string is a concatenation of all conditions and groups, separated by spaces.
type AstValueType ¶ added in v0.2.0
type AstValueType string
AstValueType is the type for all available AST value types.
const ( EMPTY AstValueType = "Empty" GROUP AstValueType = "Group" CONDITION AstValueType = "Condition" )
type ConditionGroup ¶ added in v0.2.0
type ConditionGroup []*AstValue
ConditionGroup is a slice of AstValue pointers.
type ConditionType ¶ added in v0.2.0
type ConditionType string
ConditionType is the type for all available condition types.
const ( NONE ConditionType = "-" EQUAL ConditionType = "equ" NOT_EQUAL ConditionType = "neq" MIN_VALUE ConditionType = "min" MAX_VALUE ConditionType = "max" CONTAINS ConditionType = "con" NOT_CONTAINS ConditionType = "nco" FROM ConditionType = "frm" NOT_FROM ConditionType = "nfr" REGX ConditionType = "rex" FUNC ConditionType = "fun" )
Available condition types.
func GetConditionType ¶
func GetConditionType(s string) (ConditionType, error)
GetConditionType returns the condition type from a string. It checks if the string starts with a valid condition type prefix. If the string is not valid, an error is returned.
type Group ¶ added in v0.2.1
type Group struct {
Name string
ConditionType ConditionType
ConditionValue string
}
Group represents a validation group with a name, condition type, and condition value. The name is expected to start with "gr" followed by a condition type and value. The condition type is validated against known condition types, allowed are min and max. The condition value is extracted based on the condition type.
func GetGroups ¶ added in v0.2.1
GetGroups parses a string to extract validation groups. It splits the string by spaces and creates a Group for each part. Each group must start with "gr" and can have a condition type and value. If the group name is invalid or the condition type is not recognized, an error is returned.
type Operator ¶ added in v0.2.0
type Operator string
Operator is the type for all available operators.
type RootNode ¶ added in v0.2.0
type RootNode struct {
RootValue *AstValue
}
RootNode is what starts every parsed AST (=abstract syntax tree).
type Token ¶ added in v0.2.0
Token represents a token in the parser. It contains the type of the token, its literal value, and its position in the requirement. The position is defined by the line number, start index, and end index. This structure is used to tokenize the input for parsing and validation.
type TokenType ¶ added in v0.2.0
type TokenType string
TokenType represents the type of a token in the parser.
const ( // Token/character we don't know about LexerIllegal TokenType = "ILLEGAL" // Empty condition LexerEmptyRequirement TokenType = "EMPTY" // End of file LexerEOF TokenType = "EOF" // Group LexerLeftBrace TokenType = "GROUP_OPEN" LexerRightBrace TokenType = "GROUP_CLOSE" // Literals LexerConditionType TokenType = "CONDITION_TYPE" LexerConditionValue TokenType = "CONDITION_VALUE" LexerConditionValueString TokenType = "CONDITION_VALUE_STRING" // Operators LexerOperator TokenType = "OPERATOR" )
type Validation ¶
type Validation struct {
Key string
Type ValidatorType
Requirement string
Groups []*Group
Default string
// Inner Struct validation
InnerValidation []Validation
}
Validation represents a validation rule for a struct field.
func GetValidationFromStructField ¶ added in v0.4.0
func GetValidationFromStructField(tagType string, fieldValue reflect.Value, fieldType reflect.StructField) (Validation, error)
GetValidationFromStructField extracts validation rules from a struct field based on the provided tag type. It checks the field's tag for the specified tag type and constructs a Validation object. If no json tag is found, it uses the field name as the key.
func GetValidationsFromStruct ¶ added in v0.4.0
func GetValidationsFromStruct(in any, tagType string) ([]Validation, error)
GetValidationsFromStruct extracts validation rules from a struct based on the provided tag type. It iterates over the struct fields, checks for the specified tag type, and constructs Validation.
type ValidatorMap ¶
type ValidatorMap map[string]Validation
ValidatorMap is a map of validation keys to Validation objects. It is used to store and manage multiple validation rules for different struct fields.
type ValidatorType ¶
type ValidatorType string
ValidatorType is the type for all available validation types.
const ( String ValidatorType = "string" Int ValidatorType = "int" Float ValidatorType = "float" Bool ValidatorType = "bool" Array ValidatorType = "array" Map ValidatorType = "map" Struct ValidatorType = "struct" Time ValidatorType = "time" TimeISO8601 ValidatorType = "timeIso8601" TimeUnix ValidatorType = "timeUnix" )
func TypeFromInterface ¶ added in v0.1.9
func TypeFromInterface(in any) ValidatorType
TypeFromInterface determines the ValidatorType based on the type of the input interface. It checks the type of the input and returns the corresponding ValidatorType. If the type is not recognized, it defaults to Struct. It handles basic types like string, int, float, bool, and complex types like JsonMap and arrays. It also checks for time.Time type and returns the appropriate ValidatorType.