eval

package
v0.0.0-...-c3423aa Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package eval is the package to eval the expression and extract the value from the document

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Query

func Query[T XPathNode](document, xpath string,
	parser func(string) (*T, error),
	query func(*T, string) (*T, error),
	inner func(*T) string) (string, error)

Query query the string from the document by xpath expression

Types

type BaseExtractor

type BaseExtractor struct {
	Name         string  `yaml:"name"` // variable name
	VarType      VarType `yaml:"type"` // variable type
	Document     string  `yaml:"-"`
	DocType      DocType `yaml:"-"`
	XPath        string  `yaml:"-"`
	ExtractStrFn func() (string, error)
}

BaseExtractor is the base extractor

func (*BaseExtractor) Extract

func (x *BaseExtractor) Extract() (interface{}, error)

Extract extracts the value from the document by xpath expression

func (*BaseExtractor) ExtractBool

func (x *BaseExtractor) ExtractBool() (bool, error)

ExtractBool extracts the value from the document by xpath expression

func (*BaseExtractor) ExtractDuration

func (x *BaseExtractor) ExtractDuration() (time.Duration, error)

ExtractDuration extracts the value from the document by xpath expression

func (*BaseExtractor) ExtractFloat

func (x *BaseExtractor) ExtractFloat() (float64, error)

ExtractFloat extracts the value from the document by xpath expression

func (*BaseExtractor) ExtractInt

func (x *BaseExtractor) ExtractInt() (int, error)

ExtractInt extracts the value from the document by xpath expression

func (*BaseExtractor) ExtractLengthVariable

func (x *BaseExtractor) ExtractLengthVariable() (any, error)

func (*BaseExtractor) ExtractTime

func (x *BaseExtractor) ExtractTime() (time.Time, error)

ExtractTime extracts the value from the document by xpath expression

func (*BaseExtractor) SetDocument

func (x *BaseExtractor) SetDocument(doc string)

SetDocument sets the document

func (*BaseExtractor) SetVarType

func (x *BaseExtractor) SetVarType(t VarType)

SetVarType sets the variable type

type DocType

type DocType int

DocType is the different type of document

const (
	Unsupported DocType = iota
	HTML
	XML
	JSON
	TEXT
)

The Document Type

func (DocType) MarshalYAML

func (t DocType) MarshalYAML() (interface{}, error)

MarshalYAML is marshal the type

func (DocType) String

func (t DocType) String() string

String covert the DocType to string

func (*DocType) Type

func (t *DocType) Type(s string)

Type covert the string to Type

func (*DocType) UnmarshalYAML

func (t *DocType) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is unmarshal the type

type Evaluator

type Evaluator struct {
	Variables  []Variable                              `` /* 149-byte string literal not displayed */
	DocType    DocType                                 `` /* 140-byte string literal not displayed */
	Expression string                                  `yaml:"expression" json:"expression" jsonschema:"required,title=Expression,description=Expression need to be evaluated"`
	Document   string                                  `yaml:"-" json:"-"`
	Extractor  Extractor                               `yaml:"-" json:"-"`
	EvalFuncs  map[string]govaluate.ExpressionFunction `yaml:"-" json:"-"`

	ExtractedValues map[string]interface{} `yaml:"-" json:"-"`
}

Evaluator is the structure of evaluator

func NewEvaluator

func NewEvaluator(doc string, t DocType, exp string) *Evaluator

NewEvaluator is the function to create a evaluator

func (*Evaluator) AddVariable

func (e *Evaluator) AddVariable(v *Variable)

AddVariable is the function to add a variable

func (*Evaluator) CleanVariable

func (e *Evaluator) CleanVariable()

CleanVariable is the function to clean the variable

func (*Evaluator) Config

func (e *Evaluator) Config() error

Config is the function to config the evaluator

func (*Evaluator) Evaluate

func (e *Evaluator) Evaluate() (bool, error)

Evaluate is the function to evaluate the expression

func (*Evaluator) Extract

func (e *Evaluator) Extract() error

Extract is the function to extract the value from the document

func (*Evaluator) ExtractValue

func (e *Evaluator) ExtractValue(v *Variable) error

ExtractValue is the function to extract the value from the document

func (*Evaluator) SetDocument

func (e *Evaluator) SetDocument(t DocType, doc string)

SetDocument is the function to set the document

type Extractor

type Extractor interface {
	SetQuery(string)
	SetVarType(VarType)
	SetDocument(string)
	Extract() (interface{}, error)
}

Extractor is the interface for all extractors

type RegexExtractor

type RegexExtractor struct {
	BaseExtractor
	Regex string `yaml:"regex"` // regex expression
}

RegexExtractor is a struct for extracting values from a plain string

func NewRegexExtractor

func NewRegexExtractor(document string, docType DocType) *RegexExtractor

NewRegexExtractor creates a new RegexExtractor

func (*RegexExtractor) MatchStr

func (r *RegexExtractor) MatchStr() (string, error)

MatchStr matches the string with the regex expression

func (*RegexExtractor) SetQuery

func (r *RegexExtractor) SetQuery(q string)

SetQuery sets the regex expression

type VarType

type VarType int

VarType is an enum for the different types of values

const (
	Unknown VarType = iota
	Int
	Float
	String
	Bool
	Time
	Duration
	LengthVariable
)

The value types

func (VarType) MarshalYAML

func (t VarType) MarshalYAML() (interface{}, error)

MarshalYAML is marshal the type

func (VarType) String

func (t VarType) String() string

String covert the Type to string

func (*VarType) Type

func (t *VarType) Type(s string)

Type covert the string to Type

func (*VarType) UnmarshalYAML

func (t *VarType) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML is unmarshal the type

type Variable

type Variable struct {
	Name  string      `yaml:"name" json:"name" jsonschema:"required,title=Variable Name,description=Variable Name"`
	Type  VarType     `` /* 179-byte string literal not displayed */
	Query string      `yaml:"query" json:"query" jsonschema:"required,title=Query,description=XPath/Regex Expression to extract the value"`
	Value interface{} `yaml:"-" json:"-"`
}

Variable is the variable type

func NewVariable

func NewVariable(name string, t VarType, query string) *Variable

NewVariable is the function to create a variable

type XPathExtractor

type XPathExtractor[T XPathNode] struct {
	BaseExtractor
	XPath  string `yaml:"xpath"` // xpath expression
	Parser func(string) (*T, error)
	Query  func(*T, string) (*T, error)
	Inner  func(*T) string
}

XPathExtractor is a struct for extracting values from a html/xml/json string

func NewHTMLExtractor

func NewHTMLExtractor(document string, docType DocType) *XPathExtractor[html.Node]

NewHTMLExtractor creates a new HTMLExtractor

func NewJSONExtractor

func NewJSONExtractor(document string, docType DocType) *XPathExtractor[jq.Node]

NewJSONExtractor creates a new JSONExtractor

func NewXMLExtractor

func NewXMLExtractor(document string, docType DocType) *XPathExtractor[xq.Node]

NewXMLExtractor creates a new XMLExtractor

func (*XPathExtractor[T]) ExtractStr

func (x *XPathExtractor[T]) ExtractStr() (string, error)

ExtractStr extracts the value from the document by xpath expression

func (*XPathExtractor[T]) SetQuery

func (x *XPathExtractor[T]) SetQuery(q string)

SetQuery sets the xpath expression

type XPathNode

type XPathNode interface {
	jq.Node | xq.Node | html.Node
}

XPathNode is the generic type for xpath node

Jump to

Keyboard shortcuts

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