提交 d7b8aa8b 编写于 作者: A astaxie

only add golang.org vendor

上级 6d69047f
This library was authored by George Lester, and contains contributions from:
vjeantet (regex support)
iasci (ternary operator)
oxtoacart (parameter structures, deferred parameter retrieval)
wmiller848 (bitwise operators)
prashantv (optimization of bools)
dpaolella (exposure of variables used in an expression)
benpaxton (fix for missing type checks during literal elide process)
abrander (panic-finding testing tool, float32 conversions)
xfennec (fix for dates being parsed in the current Location)
bgaifullin (lifting restriction on complex/struct types)
gautambt (hexadecimal literals)
felixonmars (fix multiple typos in test names)
sambonfire (automatic type conversion for accessor function calls)
\ No newline at end of file
package govaluate
import (
"errors"
"fmt"
)
const isoDateFormat string = "2006-01-02T15:04:05.999999999Z0700"
const shortCircuitHolder int = -1
var DUMMY_PARAMETERS = MapParameters(map[string]interface{}{})
/*
EvaluableExpression represents a set of ExpressionTokens which, taken together,
are an expression that can be evaluated down into a single value.
*/
type EvaluableExpression struct {
/*
Represents the query format used to output dates. Typically only used when creating SQL or Mongo queries from an expression.
Defaults to the complete ISO8601 format, including nanoseconds.
*/
QueryDateFormat string
/*
Whether or not to safely check types when evaluating.
If true, this library will return error messages when invalid types are used.
If false, the library will panic when operators encounter types they can't use.
This is exclusively for users who need to squeeze every ounce of speed out of the library as they can,
and you should only set this to false if you know exactly what you're doing.
*/
ChecksTypes bool
tokens []ExpressionToken
evaluationStages *evaluationStage
inputExpression string
}
/*
Parses a new EvaluableExpression from the given [expression] string.
Returns an error if the given expression has invalid syntax.
*/
func NewEvaluableExpression(expression string) (*EvaluableExpression, error) {
functions := make(map[string]ExpressionFunction)
return NewEvaluableExpressionWithFunctions(expression, functions)
}
/*
Similar to [NewEvaluableExpression], except that instead of a string, an already-tokenized expression is given.
This is useful in cases where you may be generating an expression automatically, or using some other parser (e.g., to parse from a query language)
*/
func NewEvaluableExpressionFromTokens(tokens []ExpressionToken) (*EvaluableExpression, error) {
var ret *EvaluableExpression
var err error
ret = new(EvaluableExpression)
ret.QueryDateFormat = isoDateFormat
err = checkBalance(tokens)
if err != nil {
return nil, err
}
err = checkExpressionSyntax(tokens)
if err != nil {
return nil, err
}
ret.tokens, err = optimizeTokens(tokens)
if err != nil {
return nil, err
}
ret.evaluationStages, err = planStages(ret.tokens)
if err != nil {
return nil, err
}
ret.ChecksTypes = true
return ret, nil
}
/*
Similar to [NewEvaluableExpression], except enables the use of user-defined functions.
Functions passed into this will be available to the expression.
*/
func NewEvaluableExpressionWithFunctions(expression string, functions map[string]ExpressionFunction) (*EvaluableExpression, error) {
var ret *EvaluableExpression
var err error
ret = new(EvaluableExpression)
ret.QueryDateFormat = isoDateFormat
ret.inputExpression = expression
ret.tokens, err = parseTokens(expression, functions)
if err != nil {
return nil, err
}
err = checkBalance(ret.tokens)
if err != nil {
return nil, err
}
err = checkExpressionSyntax(ret.tokens)
if err != nil {
return nil, err
}
ret.tokens, err = optimizeTokens(ret.tokens)
if err != nil {
return nil, err
}
ret.evaluationStages, err = planStages(ret.tokens)
if err != nil {
return nil, err
}
ret.ChecksTypes = true
return ret, nil
}
/*
Same as `Eval`, but automatically wraps a map of parameters into a `govalute.Parameters` structure.
*/
func (this EvaluableExpression) Evaluate(parameters map[string]interface{}) (interface{}, error) {
if parameters == nil {
return this.Eval(nil)
}
return this.Eval(MapParameters(parameters))
}
/*
Runs the entire expression using the given [parameters].
e.g., If the expression contains a reference to the variable "foo", it will be taken from `parameters.Get("foo")`.
This function returns errors if the combination of expression and parameters cannot be run,
such as if a variable in the expression is not present in [parameters].
In all non-error circumstances, this returns the single value result of the expression and parameters given.
e.g., if the expression is "1 + 1", this will return 2.0.
e.g., if the expression is "foo + 1" and parameters contains "foo" = 2, this will return 3.0
*/
func (this EvaluableExpression) Eval(parameters Parameters) (interface{}, error) {
if this.evaluationStages == nil {
return nil, nil
}
if parameters != nil {
parameters = &sanitizedParameters{parameters}
} else {
parameters = DUMMY_PARAMETERS
}
return this.evaluateStage(this.evaluationStages, parameters)
}
func (this EvaluableExpression) evaluateStage(stage *evaluationStage, parameters Parameters) (interface{}, error) {
var left, right interface{}
var err error
if stage.leftStage != nil {
left, err = this.evaluateStage(stage.leftStage, parameters)
if err != nil {
return nil, err
}
}
if stage.isShortCircuitable() {
switch stage.symbol {
case AND:
if left == false {
return false, nil
}
case OR:
if left == true {
return true, nil
}
case COALESCE:
if left != nil {
return left, nil
}
case TERNARY_TRUE:
if left == false {
right = shortCircuitHolder
}
case TERNARY_FALSE:
if left != nil {
right = shortCircuitHolder
}
}
}
if right != shortCircuitHolder && stage.rightStage != nil {
right, err = this.evaluateStage(stage.rightStage, parameters)
if err != nil {
return nil, err
}
}
if this.ChecksTypes {
if stage.typeCheck == nil {
err = typeCheck(stage.leftTypeCheck, left, stage.symbol, stage.typeErrorFormat)
if err != nil {
return nil, err
}
err = typeCheck(stage.rightTypeCheck, right, stage.symbol, stage.typeErrorFormat)
if err != nil {
return nil, err
}
} else {
// special case where the type check needs to know both sides to determine if the operator can handle it
if !stage.typeCheck(left, right) {
errorMsg := fmt.Sprintf(stage.typeErrorFormat, left, stage.symbol.String())
return nil, errors.New(errorMsg)
}
}
}
return stage.operator(left, right, parameters)
}
func typeCheck(check stageTypeCheck, value interface{}, symbol OperatorSymbol, format string) error {
if check == nil {
return nil
}
if check(value) {
return nil
}
errorMsg := fmt.Sprintf(format, value, symbol.String())
return errors.New(errorMsg)
}
/*
Returns an array representing the ExpressionTokens that make up this expression.
*/
func (this EvaluableExpression) Tokens() []ExpressionToken {
return this.tokens
}
/*
Returns the original expression used to create this EvaluableExpression.
*/
func (this EvaluableExpression) String() string {
return this.inputExpression
}
/*
Returns an array representing the variables contained in this EvaluableExpression.
*/
func (this EvaluableExpression) Vars() []string {
var varlist []string
for _, val := range this.Tokens() {
if val.Kind == VARIABLE {
varlist = append(varlist, val.Value.(string))
}
}
return varlist
}
package govaluate
import (
"errors"
"fmt"
"regexp"
"time"
)
/*
Returns a string representing this expression as if it were written in SQL.
This function assumes that all parameters exist within the same table, and that the table essentially represents
a serialized object of some sort (e.g., hibernate).
If your data model is more normalized, you may need to consider iterating through each actual token given by `Tokens()`
to create your query.
Boolean values are considered to be "1" for true, "0" for false.
Times are formatted according to this.QueryDateFormat.
*/
func (this EvaluableExpression) ToSQLQuery() (string, error) {
var stream *tokenStream
var transactions *expressionOutputStream
var transaction string
var err error
stream = newTokenStream(this.tokens)
transactions = new(expressionOutputStream)
for stream.hasNext() {
transaction, err = this.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
transactions.add(transaction)
}
return transactions.createString(" "), nil
}
func (this EvaluableExpression) findNextSQLString(stream *tokenStream, transactions *expressionOutputStream) (string, error) {
var token ExpressionToken
var ret string
token = stream.next()
switch token.Kind {
case STRING:
ret = fmt.Sprintf("'%v'", token.Value)
case PATTERN:
ret = fmt.Sprintf("'%s'", token.Value.(*regexp.Regexp).String())
case TIME:
ret = fmt.Sprintf("'%s'", token.Value.(time.Time).Format(this.QueryDateFormat))
case LOGICALOP:
switch logicalSymbols[token.Value.(string)] {
case AND:
ret = "AND"
case OR:
ret = "OR"
}
case BOOLEAN:
if token.Value.(bool) {
ret = "1"
} else {
ret = "0"
}
case VARIABLE:
ret = fmt.Sprintf("[%s]", token.Value.(string))
case NUMERIC:
ret = fmt.Sprintf("%g", token.Value.(float64))
case COMPARATOR:
switch comparatorSymbols[token.Value.(string)] {
case EQ:
ret = "="
case NEQ:
ret = "<>"
case REQ:
ret = "RLIKE"
case NREQ:
ret = "NOT RLIKE"
default:
ret = fmt.Sprintf("%s", token.Value.(string))
}
case TERNARY:
switch ternarySymbols[token.Value.(string)] {
case COALESCE:
left := transactions.rollback()
right, err := this.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
ret = fmt.Sprintf("COALESCE(%v, %v)", left, right)
case TERNARY_TRUE:
fallthrough
case TERNARY_FALSE:
return "", errors.New("Ternary operators are unsupported in SQL output")
}
case PREFIX:
switch prefixSymbols[token.Value.(string)] {
case INVERT:
ret = fmt.Sprintf("NOT")
default:
right, err := this.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
ret = fmt.Sprintf("%s%s", token.Value.(string), right)
}
case MODIFIER:
switch modifierSymbols[token.Value.(string)] {
case EXPONENT:
left := transactions.rollback()
right, err := this.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
ret = fmt.Sprintf("POW(%s, %s)", left, right)
case MODULUS:
left := transactions.rollback()
right, err := this.findNextSQLString(stream, transactions)
if err != nil {
return "", err
}
ret = fmt.Sprintf("MOD(%s, %s)", left, right)
default:
ret = fmt.Sprintf("%s", token.Value.(string))
}
case CLAUSE:
ret = "("
case CLAUSE_CLOSE:
ret = ")"
case SEPARATOR:
ret = ","
default:
errorMsg := fmt.Sprintf("Unrecognized query token '%s' of kind '%s'", token.Value, token.Kind)
return "", errors.New(errorMsg)
}
return ret, nil
}
package govaluate
/*
Represents a single parsed token.
*/
type ExpressionToken struct {
Kind TokenKind
Value interface{}
}
The MIT License (MIT)
Copyright (c) 2014-2016 George Lester
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
govaluate
====
This library contains quite a lot of functionality, this document is meant to be formal documentation on the operators and features of it.
Some of this documentation may duplicate what's in README.md, but should never conflict.
# Types
This library only officially deals with four types; `float64`, `bool`, `string`, and arrays.
All numeric literals, with or without a radix, will be converted to `float64` for evaluation. For instance; in practice, there is no difference between the literals "1.0" and "1", they both end up as `float64`. This matters to users because if you intend to return numeric values from your expressions, then the returned value will be `float64`, not any other numeric type.
Any string _literal_ (not parameter) which is interpretable as a date will be converted to a `float64` representation of that date's unix time. Any `time.Time` parameters will not be operable with these date literals; such parameters will need to use the `time.Time.Unix()` method to get a numeric representation.
Arrays are untyped, and can be mixed-type. Internally they're all just `interface{}`. Only two operators can interact with arrays, `IN` and `,`. All other operators will refuse to operate on arrays.
# Operators
## Modifiers
### Addition, concatenation `+`
If either left or right sides of the `+` operator are a `string`, then this operator will perform string concatenation and return that result. If neither are string, then both must be numeric, and this will return a numeric result.
Any other case is invalid.
### Arithmetic `-` `*` `/` `**` `%`
`**` refers to "take to the power of". For instance, `3 ** 4` == 81.
* _Left side_: numeric
* _Right side_: numeric
* _Returns_: numeric
### Bitwise shifts, masks `>>` `<<` `|` `&` `^`
All of these operators convert their `float64` left and right sides to `int64`, perform their operation, and then convert back.
Given how this library assumes numeric are represented (as `float64`), it is unlikely that this behavior will change, even though it may cause havoc with extremely large or small numbers.
* _Left side_: numeric
* _Right side_: numeric
* _Returns_: numeric
### Negation `-`
Prefix only. This can never have a left-hand value.
* _Right side_: numeric
* _Returns_: numeric
### Inversion `!`
Prefix only. This can never have a left-hand value.
* _Right side_: bool
* _Returns_: bool
### Bitwise NOT `~`
Prefix only. This can never have a left-hand value.
* _Right side_: numeric
* _Returns_: numeric
## Logical Operators
For all logical operators, this library will short-circuit the operation if the left-hand side is sufficient to determine what to do. For instance, `true || expensiveOperation()` will not actually call `expensiveOperation()`, since it knows the left-hand side is `true`.
### Logical AND/OR `&&` `||`
* _Left side_: bool
* _Right side_: bool
* _Returns_: bool
### Ternary true `?`
Checks if the left side is `true`. If so, returns the right side. If the left side is `false`, returns `nil`.
In practice, this is commonly used with the other ternary operator.
* _Left side_: bool
* _Right side_: Any type.
* _Returns_: Right side or `nil`
### Ternary false `:`
Checks if the left side is `nil`. If so, returns the right side. If the left side is non-nil, returns the left side.
In practice, this is commonly used with the other ternary operator.
* _Left side_: Any type.
* _Right side_: Any type.
* _Returns_: Right side or `nil`
### Null coalescence `??`
Similar to the C# operator. If the left value is non-nil, it returns that. If not, then the right-value is returned.
* _Left side_: Any type.
* _Right side_: Any type.
* _Returns_: No specific type - whichever is passed to it.
## Comparators
### Numeric/lexicographic comparators `>` `<` `>=` `<=`
If both sides are numeric, this returns the usual greater/lesser behavior that would be expected.
If both sides are string, this returns the lexicographic comparison of the strings. This uses Go's standard lexicographic compare.
* _Accepts_: Left and right side must either be both string, or both numeric.
* _Returns_: bool
### Regex comparators `=~` `!~`
These use go's standard `regexp` flavor of regex. The left side is expected to be the candidate string, the right side is the pattern. `=~` returns whether or not the candidate string matches the regex pattern given on the right. `!~` is the inverted version of the same logic.
* _Left side_: string
* _Right side_: string
* _Returns_: bool
## Arrays
### Separator `,`
The separator, always paired with parenthesis, creates arrays. It must always have both a left and right-hand value, so for instance `(, 0)` and `(0,)` are invalid uses of it.
Again, this should always be used with parenthesis; like `(1, 2, 3, 4)`.
### Membership `IN`
The only operator with a text name, this operator checks the right-hand side array to see if it contains a value that is equal to the left-side value.
Equality is determined by the use of the `==` operator, and this library doesn't check types between the values. Any two values, when cast to `interface{}`, and can still be checked for equality with `==` will act as expected.
Note that you can use a parameter for the array, but it must be an `[]interface{}`.
* _Left side_: Any type.
* _Right side_: array
* _Returns_: bool
# Parameters
Parameters must be passed in every time the expression is evaluated. Parameters can be of any type, but will not cause errors unless actually used in an erroneous way. There is no difference in behavior for any of the above operators for parameters - they are type checked when used.
All `int` and `float` values of any width will be converted to `float64` before use.
At no point is the parameter structure, or any value thereof, modified by this library.
## Alternates to maps
The default form of parameters as a map may not serve your use case. You may have parameters in some other structure, you may want to change the no-parameter-found behavior, or maybe even just have some debugging print statements invoked when a parameter is accessed.
To do this, define a type that implements the `govaluate.Parameters` interface. When you want to evaluate, instead call `EvaluableExpression.Eval` and pass your parameter structure.
# Functions
During expression parsing (_not_ evaluation), a map of functions can be given to `govaluate.NewEvaluableExpressionWithFunctions` (the lengthiest and finest of function names). The resultant expression will be able to invoke those functions during evaluation. Once parsed, an expression cannot have functions added or removed - a new expression will need to be created if you want to change the functions, or behavior of said functions.
Functions always take the form `<name>(<parameters>)`, including parens. Functions can have an empty list of parameters, like `<name>()`, but still must have parens.
If the expression contains something that looks like it ought to be a function (such as `foo()`), but no such function was given to it, it will error on parsing.
Functions must be of type `map[string]govaluate.ExpressionFunction`. `ExpressionFunction`, for brevity, has the following signature:
`func(args ...interface{}) (interface{}, error)`
Where `args` is whatever is passed to the function when called. If a non-nil error is returned from a function during evaluation, the evaluation stops and ultimately returns that error to the caller of `Evaluate()` or `Eval()`.
## Built-in functions
There aren't any builtin functions. The author is opposed to maintaining a standard library of functions to be used.
Every use case of this library is different, and even in simple use cases (such as parameters, see above) different users need different behavior, naming, or even functionality. The author prefers that users make their own decisions about what functions they need, and how they operate.
# Equality
The `==` and `!=` operators involve a moderately complex workflow. They use [`reflect.DeepEqual`](https://golang.org/pkg/reflect/#DeepEqual). This is for complicated reasons, but there are some types in Go that cannot be compared with the native `==` operator. Arrays, in particular, cannot be compared - Go will panic if you try. One might assume this could be handled with the type checking system in `govaluate`, but unfortunately without reflection there is no way to know if a variable is a slice/array. Worse, structs can be incomparable if they _contain incomparable types_.
It's all very complicated. Fortunately, Go includes the `reflect.DeepEqual` function to handle all the edge cases. Currently, `govaluate` uses that for all equality/inequality.
package govaluate
/*
Represents the valid symbols for operators.
*/
type OperatorSymbol int
const (
VALUE OperatorSymbol = iota
LITERAL
NOOP
EQ
NEQ
GT
LT
GTE
LTE
REQ
NREQ
IN
AND
OR
PLUS
MINUS
BITWISE_AND
BITWISE_OR
BITWISE_XOR
BITWISE_LSHIFT
BITWISE_RSHIFT
MULTIPLY
DIVIDE
MODULUS
EXPONENT
NEGATE
INVERT
BITWISE_NOT
TERNARY_TRUE
TERNARY_FALSE
COALESCE
FUNCTIONAL
ACCESS
SEPARATE
)
type operatorPrecedence int
const (
noopPrecedence operatorPrecedence = iota
valuePrecedence
functionalPrecedence
prefixPrecedence
exponentialPrecedence
additivePrecedence
bitwisePrecedence
bitwiseShiftPrecedence
multiplicativePrecedence
comparatorPrecedence
ternaryPrecedence
logicalAndPrecedence
logicalOrPrecedence
separatePrecedence
)
func findOperatorPrecedenceForSymbol(symbol OperatorSymbol) operatorPrecedence {
switch symbol {
case NOOP:
return noopPrecedence
case VALUE:
return valuePrecedence
case EQ:
fallthrough
case NEQ:
fallthrough
case GT:
fallthrough
case LT:
fallthrough
case GTE:
fallthrough
case LTE:
fallthrough
case REQ:
fallthrough
case NREQ:
fallthrough
case IN:
return comparatorPrecedence
case AND:
return logicalAndPrecedence
case OR:
return logicalOrPrecedence
case BITWISE_AND:
fallthrough
case BITWISE_OR:
fallthrough
case BITWISE_XOR:
return bitwisePrecedence
case BITWISE_LSHIFT:
fallthrough
case BITWISE_RSHIFT:
return bitwiseShiftPrecedence
case PLUS:
fallthrough
case MINUS:
return additivePrecedence
case MULTIPLY:
fallthrough
case DIVIDE:
fallthrough
case MODULUS:
return multiplicativePrecedence
case EXPONENT:
return exponentialPrecedence
case BITWISE_NOT:
fallthrough
case NEGATE:
fallthrough
case INVERT:
return prefixPrecedence
case COALESCE:
fallthrough
case TERNARY_TRUE:
fallthrough
case TERNARY_FALSE:
return ternaryPrecedence
case ACCESS:
fallthrough
case FUNCTIONAL:
return functionalPrecedence
case SEPARATE:
return separatePrecedence
}
return valuePrecedence
}
/*
Map of all valid comparators, and their string equivalents.
Used during parsing of expressions to determine if a symbol is, in fact, a comparator.
Also used during evaluation to determine exactly which comparator is being used.
*/
var comparatorSymbols = map[string]OperatorSymbol{
"==": EQ,
"!=": NEQ,
">": GT,
">=": GTE,
"<": LT,
"<=": LTE,
"=~": REQ,
"!~": NREQ,
"in": IN,
}
var logicalSymbols = map[string]OperatorSymbol{
"&&": AND,
"||": OR,
}
var bitwiseSymbols = map[string]OperatorSymbol{
"^": BITWISE_XOR,
"&": BITWISE_AND,
"|": BITWISE_OR,
}
var bitwiseShiftSymbols = map[string]OperatorSymbol{
">>": BITWISE_RSHIFT,
"<<": BITWISE_LSHIFT,
}
var additiveSymbols = map[string]OperatorSymbol{
"+": PLUS,
"-": MINUS,
}
var multiplicativeSymbols = map[string]OperatorSymbol{
"*": MULTIPLY,
"/": DIVIDE,
"%": MODULUS,
}
var exponentialSymbolsS = map[string]OperatorSymbol{
"**": EXPONENT,
}
var prefixSymbols = map[string]OperatorSymbol{
"-": NEGATE,
"!": INVERT,
"~": BITWISE_NOT,
}
var ternarySymbols = map[string]OperatorSymbol{
"?": TERNARY_TRUE,
":": TERNARY_FALSE,
"??": COALESCE,
}
// this is defined separately from additiveSymbols et al because it's needed for parsing, not stage planning.
var modifierSymbols = map[string]OperatorSymbol{
"+": PLUS,
"-": MINUS,
"*": MULTIPLY,
"/": DIVIDE,
"%": MODULUS,
"**": EXPONENT,
"&": BITWISE_AND,
"|": BITWISE_OR,
"^": BITWISE_XOR,
">>": BITWISE_RSHIFT,
"<<": BITWISE_LSHIFT,
}
var separatorSymbols = map[string]OperatorSymbol{
",": SEPARATE,
}
/*
Returns true if this operator is contained by the given array of candidate symbols.
False otherwise.
*/
func (this OperatorSymbol) IsModifierType(candidate []OperatorSymbol) bool {
for _, symbolType := range candidate {
if this == symbolType {
return true
}
}
return false
}
/*
Generally used when formatting type check errors.
We could store the stringified symbol somewhere else and not require a duplicated codeblock to translate
OperatorSymbol to string, but that would require more memory, and another field somewhere.
Adding operators is rare enough that we just stringify it here instead.
*/
func (this OperatorSymbol) String() string {
switch this {
case NOOP:
return "NOOP"
case VALUE:
return "VALUE"
case EQ:
return "="
case NEQ:
return "!="
case GT:
return ">"
case LT:
return "<"
case GTE:
return ">="
case LTE:
return "<="
case REQ:
return "=~"
case NREQ:
return "!~"
case AND:
return "&&"
case OR:
return "||"
case IN:
return "in"
case BITWISE_AND:
return "&"
case BITWISE_OR:
return "|"
case BITWISE_XOR:
return "^"
case BITWISE_LSHIFT:
return "<<"
case BITWISE_RSHIFT:
return ">>"
case PLUS:
return "+"
case MINUS:
return "-"
case MULTIPLY:
return "*"
case DIVIDE:
return "/"
case MODULUS:
return "%"
case EXPONENT:
return "**"
case NEGATE:
return "-"
case INVERT:
return "!"
case BITWISE_NOT:
return "~"
case TERNARY_TRUE:
return "?"
case TERNARY_FALSE:
return ":"
case COALESCE:
return "??"
}
return ""
}
govaluate
====
[![Build Status](https://travis-ci.org/Knetic/govaluate.svg?branch=master)](https://travis-ci.org/Knetic/govaluate)
[![Godoc](https://img.shields.io/badge/godoc-reference-5272B4.svg)](https://godoc.org/github.com/Knetic/govaluate)
[![Go Report Card](https://goreportcard.com/badge/github.com/Knetic/govaluate)](https://goreportcard.com/report/github.com/Knetic/govaluate)
[![Gocover](https://gocover.io/_badge/github.com/Knetic/govaluate)](https://gocover.io/github.com/Knetic/govaluate)
Provides support for evaluating arbitrary C-like artithmetic/string expressions.
Why can't you just write these expressions in code?
--
Sometimes, you can't know ahead-of-time what an expression will look like, or you want those expressions to be configurable.
Perhaps you've got a set of data running through your application, and you want to allow your users to specify some validations to run on it before committing it to a database. Or maybe you've written a monitoring framework which is capable of gathering a bunch of metrics, then evaluating a few expressions to see if any metrics should be alerted upon, but the conditions for alerting are different for each monitor.
A lot of people wind up writing their own half-baked style of evaluation language that fits their needs, but isn't complete. Or they wind up baking the expression into the actual executable, even if they know it's subject to change. These strategies may work, but they take time to implement, time for users to learn, and induce technical debt as requirements change. This library is meant to cover all the normal C-like expressions, so that you don't have to reinvent one of the oldest wheels on a computer.
How do I use it?
--
You create a new EvaluableExpression, then call "Evaluate" on it.
```go
expression, err := govaluate.NewEvaluableExpression("10 > 0");
result, err := expression.Evaluate(nil);
// result is now set to "true", the bool value.
```
Cool, but how about with parameters?
```go
expression, err := govaluate.NewEvaluableExpression("foo > 0");
parameters := make(map[string]interface{}, 8)
parameters["foo"] = -1;
result, err := expression.Evaluate(parameters);
// result is now set to "false", the bool value.
```
That's cool, but we can almost certainly have done all that in code. What about a complex use case that involves some math?
```go
expression, err := govaluate.NewEvaluableExpression("(requests_made * requests_succeeded / 100) >= 90");
parameters := make(map[string]interface{}, 8)
parameters["requests_made"] = 100;
parameters["requests_succeeded"] = 80;
result, err := expression.Evaluate(parameters);
// result is now set to "false", the bool value.
```
Or maybe you want to check the status of an alive check ("smoketest") page, which will be a string?
```go
expression, err := govaluate.NewEvaluableExpression("http_response_body == 'service is ok'");
parameters := make(map[string]interface{}, 8)
parameters["http_response_body"] = "service is ok";
result, err := expression.Evaluate(parameters);
// result is now set to "true", the bool value.
```
These examples have all returned boolean values, but it's equally possible to return numeric ones.
```go
expression, err := govaluate.NewEvaluableExpression("(mem_used / total_mem) * 100");
parameters := make(map[string]interface{}, 8)
parameters["total_mem"] = 1024;
parameters["mem_used"] = 512;
result, err := expression.Evaluate(parameters);
// result is now set to "50.0", the float64 value.
```
You can also do date parsing, though the formats are somewhat limited. Stick to RF3339, ISO8061, unix date, or ruby date formats. If you're having trouble getting a date string to parse, check the list of formats actually used: [parsing.go:248](https://github.com/Knetic/govaluate/blob/0580e9b47a69125afa0e4ebd1cf93c49eb5a43ec/parsing.go#L258).
```go
expression, err := govaluate.NewEvaluableExpression("'2014-01-02' > '2014-01-01 23:59:59'");
result, err := expression.Evaluate(nil);
// result is now set to true
```
Expressions are parsed once, and can be re-used multiple times. Parsing is the compute-intensive phase of the process, so if you intend to use the same expression with different parameters, just parse it once. Like so;
```go
expression, err := govaluate.NewEvaluableExpression("response_time <= 100");
parameters := make(map[string]interface{}, 8)
for {
parameters["response_time"] = pingSomething();
result, err := expression.Evaluate(parameters)
}
```
The normal C-standard order of operators is respected. When writing an expression, be sure that you either order the operators correctly, or use parenthesis to clarify which portions of an expression should be run first.
Escaping characters
--
Sometimes you'll have parameters that have spaces, slashes, pluses, ampersands or some other character
that this library interprets as something special. For example, the following expression will not
act as one might expect:
"response-time < 100"
As written, the library will parse it as "[response] minus [time] is less than 100". In reality,
"response-time" is meant to be one variable that just happens to have a dash in it.
There are two ways to work around this. First, you can escape the entire parameter name:
"[response-time] < 100"
Or you can use backslashes to escape only the minus sign.
"response\\-time < 100"
Backslashes can be used anywhere in an expression to escape the very next character. Square bracketed parameter names can be used instead of plain parameter names at any time.
Functions
--
You may have cases where you want to call a function on a parameter during execution of the expression. Perhaps you want to aggregate some set of data, but don't know the exact aggregation you want to use until you're writing the expression itself. Or maybe you have a mathematical operation you want to perform, for which there is no operator; like `log` or `tan` or `sqrt`. For cases like this, you can provide a map of functions to `NewEvaluableExpressionWithFunctions`, which will then be able to use them during execution. For instance;
```go
functions := map[string]govaluate.ExpressionFunction {
"strlen": func(args ...interface{}) (interface{}, error) {
length := len(args[0].(string))
return (float64)(length), nil
},
}
expString := "strlen('someReallyLongInputString') <= 16"
expression, _ := govaluate.NewEvaluableExpressionWithFunctions(expString, functions)
result, _ := expression.Evaluate(nil)
// result is now "false", the boolean value
```
Functions can accept any number of arguments, correctly handles nested functions, and arguments can be of any type (even if none of this library's operators support evaluation of that type). For instance, each of these usages of functions in an expression are valid (assuming that the appropriate functions and parameters are given):
```go
"sqrt(x1 ** y1, x2 ** y2)"
"max(someValue, abs(anotherValue), 10 * lastValue)"
```
Functions cannot be passed as parameters, they must be known at the time when the expression is parsed, and are unchangeable after parsing.
Accessors
--
If you have structs in your parameters, you can access their fields and methods in the usual way. For instance, given a struct that has a method "Echo", present in the parameters as `foo`, the following is valid:
"foo.Echo('hello world')"
Fields are accessed in a similar way. Assuming `foo` has a field called "Length":
"foo.Length > 9000"
Accessors can be nested to any depth, like the following
"foo.Bar.Baz.SomeFunction()"
However it is not _currently_ supported to access values in `map`s. So the following will not work
"foo.SomeMap['key']"
This may be convenient, but note that using accessors involves a _lot_ of reflection. This makes the expression about four times slower than just using a parameter (consult the benchmarks for more precise measurements on your system).
If at all reasonable, the author recommends extracting the values you care about into a parameter map beforehand, or defining a struct that implements the `Parameters` interface, and which grabs fields as required. If there are functions you want to use, it's better to pass them as expression functions (see the above section). These approaches use no reflection, and are designed to be fast and clean.
What operators and types does this support?
--
* Modifiers: `+` `-` `/` `*` `&` `|` `^` `**` `%` `>>` `<<`
* Comparators: `>` `>=` `<` `<=` `==` `!=` `=~` `!~`
* Logical ops: `||` `&&`
* Numeric constants, as 64-bit floating point (`12345.678`)
* String constants (single quotes: `'foobar'`)
* Date constants (single quotes, using any permutation of RFC3339, ISO8601, ruby date, or unix date; date parsing is automatically tried with any string constant)
* Boolean constants: `true` `false`
* Parenthesis to control order of evaluation `(` `)`
* Arrays (anything separated by `,` within parenthesis: `(1, 2, 'foo')`)
* Prefixes: `!` `-` `~`
* Ternary conditional: `?` `:`
* Null coalescence: `??`
See [MANUAL.md](https://github.com/Knetic/govaluate/blob/master/MANUAL.md) for exacting details on what types each operator supports.
Types
--
Some operators don't make sense when used with some types. For instance, what does it mean to get the modulo of a string? What happens if you check to see if two numbers are logically AND'ed together?
Everyone has a different intuition about the answers to these questions. To prevent confusion, this library will _refuse to operate_ upon types for which there is not an unambiguous meaning for the operation. See [MANUAL.md](https://github.com/Knetic/govaluate/blob/master/MANUAL.md) for details about what operators are valid for which types.
Benchmarks
--
If you're concerned about the overhead of this library, a good range of benchmarks are built into this repo. You can run them with `go test -bench=.`. The library is built with an eye towards being quick, but has not been aggressively profiled and optimized. For most applications, though, it is completely fine.
For a very rough idea of performance, here are the results output from a benchmark run on a 3rd-gen Macbook Pro (Linux Mint 17.1).
```
BenchmarkSingleParse-12 1000000 1382 ns/op
BenchmarkSimpleParse-12 200000 10771 ns/op
BenchmarkFullParse-12 30000 49383 ns/op
BenchmarkEvaluationSingle-12 50000000 30.1 ns/op
BenchmarkEvaluationNumericLiteral-12 10000000 119 ns/op
BenchmarkEvaluationLiteralModifiers-12 10000000 236 ns/op
BenchmarkEvaluationParameters-12 5000000 260 ns/op
BenchmarkEvaluationParametersModifiers-12 3000000 547 ns/op
BenchmarkComplexExpression-12 2000000 963 ns/op
BenchmarkRegexExpression-12 100000 20357 ns/op
BenchmarkConstantRegexExpression-12 1000000 1392 ns/op
ok
```
API Breaks
--
While this library has very few cases which will ever result in an API break, it can (and [has](https://github.com/Knetic/govaluate/releases/tag/v2.0.0)) happened. If you are using this in production, vendor the commit you've tested against, or use gopkg.in to redirect your import (e.g., `import "gopkg.in/Knetic/govaluate.v2"`). Master branch (while infrequent) _may_ at some point contain API breaking changes, and the author will have no way to communicate these to downstreams, other than creating a new major release.
Releases will explicitly state when an API break happens, and if they do not specify an API break it should be safe to upgrade.
License
--
This project is licensed under the MIT general use license. You're free to integrate, fork, and play with this code as you feel fit without consulting the author, as long as you provide proper credit to the author in your works.
package govaluate
/*
Represents all valid types of tokens that a token can be.
*/
type TokenKind int
const (
UNKNOWN TokenKind = iota
PREFIX
NUMERIC
BOOLEAN
STRING
PATTERN
TIME
VARIABLE
FUNCTION
SEPARATOR
ACCESSOR
COMPARATOR
LOGICALOP
MODIFIER
CLAUSE
CLAUSE_CLOSE
TERNARY
)
/*
GetTokenKindString returns a string that describes the given TokenKind.
e.g., when passed the NUMERIC TokenKind, this returns the string "NUMERIC".
*/
func (kind TokenKind) String() string {
switch kind {
case PREFIX:
return "PREFIX"
case NUMERIC:
return "NUMERIC"
case BOOLEAN:
return "BOOLEAN"
case STRING:
return "STRING"
case PATTERN:
return "PATTERN"
case TIME:
return "TIME"
case VARIABLE:
return "VARIABLE"
case FUNCTION:
return "FUNCTION"
case SEPARATOR:
return "SEPARATOR"
case COMPARATOR:
return "COMPARATOR"
case LOGICALOP:
return "LOGICALOP"
case MODIFIER:
return "MODIFIER"
case CLAUSE:
return "CLAUSE"
case CLAUSE_CLOSE:
return "CLAUSE_CLOSE"
case TERNARY:
return "TERNARY"
case ACCESSOR:
return "ACCESSOR"
}
return "UNKNOWN"
}
package govaluate
import (
"errors"
"fmt"
"math"
"reflect"
"regexp"
"strings"
)
const (
logicalErrorFormat string = "Value '%v' cannot be used with the logical operator '%v', it is not a bool"
modifierErrorFormat string = "Value '%v' cannot be used with the modifier '%v', it is not a number"
comparatorErrorFormat string = "Value '%v' cannot be used with the comparator '%v', it is not a number"
ternaryErrorFormat string = "Value '%v' cannot be used with the ternary operator '%v', it is not a bool"
prefixErrorFormat string = "Value '%v' cannot be used with the prefix '%v'"
)
type evaluationOperator func(left interface{}, right interface{}, parameters Parameters) (interface{}, error)
type stageTypeCheck func(value interface{}) bool
type stageCombinedTypeCheck func(left interface{}, right interface{}) bool
type evaluationStage struct {
symbol OperatorSymbol
leftStage, rightStage *evaluationStage
// the operation that will be used to evaluate this stage (such as adding [left] to [right] and return the result)
operator evaluationOperator
// ensures that both left and right values are appropriate for this stage. Returns an error if they aren't operable.
leftTypeCheck stageTypeCheck
rightTypeCheck stageTypeCheck
// if specified, will override whatever is used in "leftTypeCheck" and "rightTypeCheck".
// primarily used for specific operators that don't care which side a given type is on, but still requires one side to be of a given type
// (like string concat)
typeCheck stageCombinedTypeCheck
// regardless of which type check is used, this string format will be used as the error message for type errors
typeErrorFormat string
}
var (
_true = interface{}(true)
_false = interface{}(false)
)
func (this *evaluationStage) swapWith(other *evaluationStage) {
temp := *other
other.setToNonStage(*this)
this.setToNonStage(temp)
}
func (this *evaluationStage) setToNonStage(other evaluationStage) {
this.symbol = other.symbol
this.operator = other.operator
this.leftTypeCheck = other.leftTypeCheck
this.rightTypeCheck = other.rightTypeCheck
this.typeCheck = other.typeCheck
this.typeErrorFormat = other.typeErrorFormat
}
func (this *evaluationStage) isShortCircuitable() bool {
switch this.symbol {
case AND:
fallthrough
case OR:
fallthrough
case TERNARY_TRUE:
fallthrough
case TERNARY_FALSE:
fallthrough
case COALESCE:
return true
}
return false
}
func noopStageRight(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return right, nil
}
func addStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
// string concat if either are strings
if isString(left) || isString(right) {
return fmt.Sprintf("%v%v", left, right), nil
}
return left.(float64) + right.(float64), nil
}
func subtractStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return left.(float64) - right.(float64), nil
}
func multiplyStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return left.(float64) * right.(float64), nil
}
func divideStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return left.(float64) / right.(float64), nil
}
func exponentStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return math.Pow(left.(float64), right.(float64)), nil
}
func modulusStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return math.Mod(left.(float64), right.(float64)), nil
}
func gteStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) >= right.(string)), nil
}
return boolIface(left.(float64) >= right.(float64)), nil
}
func gtStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) > right.(string)), nil
}
return boolIface(left.(float64) > right.(float64)), nil
}
func lteStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) <= right.(string)), nil
}
return boolIface(left.(float64) <= right.(float64)), nil
}
func ltStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if isString(left) && isString(right) {
return boolIface(left.(string) < right.(string)), nil
}
return boolIface(left.(float64) < right.(float64)), nil
}
func equalStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(reflect.DeepEqual(left, right)), nil
}
func notEqualStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(!reflect.DeepEqual(left, right)), nil
}
func andStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(left.(bool) && right.(bool)), nil
}
func orStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(left.(bool) || right.(bool)), nil
}
func negateStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return -right.(float64), nil
}
func invertStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return boolIface(!right.(bool)), nil
}
func bitwiseNotStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return float64(^int64(right.(float64))), nil
}
func ternaryIfStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if left.(bool) {
return right, nil
}
return nil, nil
}
func ternaryElseStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if left != nil {
return left, nil
}
return right, nil
}
func regexStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
var pattern *regexp.Regexp
var err error
switch right.(type) {
case string:
pattern, err = regexp.Compile(right.(string))
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to compile regexp pattern '%v': %v", right, err))
}
case *regexp.Regexp:
pattern = right.(*regexp.Regexp)
}
return pattern.Match([]byte(left.(string))), nil
}
func notRegexStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
ret, err := regexStage(left, right, parameters)
if err != nil {
return nil, err
}
return !(ret.(bool)), nil
}
func bitwiseOrStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return float64(int64(left.(float64)) | int64(right.(float64))), nil
}
func bitwiseAndStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return float64(int64(left.(float64)) & int64(right.(float64))), nil
}
func bitwiseXORStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return float64(int64(left.(float64)) ^ int64(right.(float64))), nil
}
func leftShiftStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return float64(uint64(left.(float64)) << uint64(right.(float64))), nil
}
func rightShiftStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return float64(uint64(left.(float64)) >> uint64(right.(float64))), nil
}
func makeParameterStage(parameterName string) evaluationOperator {
return func(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
value, err := parameters.Get(parameterName)
if err != nil {
return nil, err
}
return value, nil
}
}
func makeLiteralStage(literal interface{}) evaluationOperator {
return func(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
return literal, nil
}
}
func makeFunctionStage(function ExpressionFunction) evaluationOperator {
return func(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
if right == nil {
return function()
}
switch right.(type) {
case []interface{}:
return function(right.([]interface{})...)
default:
return function(right)
}
}
}
func typeConvertParam(p reflect.Value, t reflect.Type) (ret reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
errorMsg := fmt.Sprintf("Argument type conversion failed: failed to convert '%s' to '%s'", p.Kind().String(), t.Kind().String())
err = errors.New(errorMsg)
ret = p
}
}()
return p.Convert(t), nil
}
func typeConvertParams(method reflect.Value, params []reflect.Value) ([]reflect.Value, error) {
methodType := method.Type()
numIn := methodType.NumIn()
numParams := len(params)
if numIn != numParams {
if numIn > numParams {
return nil, fmt.Errorf("Too few arguments to parameter call: got %d arguments, expected %d", len(params), numIn)
}
return nil, fmt.Errorf("Too many arguments to parameter call: got %d arguments, expected %d", len(params), numIn)
}
for i := 0; i < numIn; i++ {
t := methodType.In(i)
p := params[i]
pt := p.Type()
if t.Kind() != pt.Kind() {
np, err := typeConvertParam(p, t)
if err != nil {
return nil, err
}
params[i] = np
}
}
return params, nil
}
func makeAccessorStage(pair []string) evaluationOperator {
reconstructed := strings.Join(pair, ".")
return func(left interface{}, right interface{}, parameters Parameters) (ret interface{}, err error) {
var params []reflect.Value
value, err := parameters.Get(pair[0])
if err != nil {
return nil, err
}
// while this library generally tries to handle panic-inducing cases on its own,
// accessors are a sticky case which have a lot of possible ways to fail.
// therefore every call to an accessor sets up a defer that tries to recover from panics, converting them to errors.
defer func() {
if r := recover(); r != nil {
errorMsg := fmt.Sprintf("Failed to access '%s': %v", reconstructed, r.(string))
err = errors.New(errorMsg)
ret = nil
}
}()
for i := 1; i < len(pair); i++ {
coreValue := reflect.ValueOf(value)
var corePtrVal reflect.Value
// if this is a pointer, resolve it.
if coreValue.Kind() == reflect.Ptr {
corePtrVal = coreValue
coreValue = coreValue.Elem()
}
if coreValue.Kind() != reflect.Struct {
return nil, errors.New("Unable to access '" + pair[i] + "', '" + pair[i-1] + "' is not a struct")
}
field := coreValue.FieldByName(pair[i])
if field != (reflect.Value{}) {
value = field.Interface()
continue
}
method := coreValue.MethodByName(pair[i])
if method == (reflect.Value{}) {
if corePtrVal.IsValid() {
method = corePtrVal.MethodByName(pair[i])
}
if method == (reflect.Value{}) {
return nil, errors.New("No method or field '" + pair[i] + "' present on parameter '" + pair[i-1] + "'")
}
}
switch right.(type) {
case []interface{}:
givenParams := right.([]interface{})
params = make([]reflect.Value, len(givenParams))
for idx, _ := range givenParams {
params[idx] = reflect.ValueOf(givenParams[idx])
}
default:
if right == nil {
params = []reflect.Value{}
break
}
params = []reflect.Value{reflect.ValueOf(right.(interface{}))}
}
params, err = typeConvertParams(method, params)
if err != nil {
return nil, errors.New("Method call failed - '" + pair[0] + "." + pair[1] + "': " + err.Error())
}
returned := method.Call(params)
retLength := len(returned)
if retLength == 0 {
return nil, errors.New("Method call '" + pair[i-1] + "." + pair[i] + "' did not return any values.")
}
if retLength == 1 {
value = returned[0].Interface()
continue
}
if retLength == 2 {
errIface := returned[1].Interface()
err, validType := errIface.(error)
if validType && errIface != nil {
return returned[0].Interface(), err
}
value = returned[0].Interface()
continue
}
return nil, errors.New("Method call '" + pair[0] + "." + pair[1] + "' did not return either one value, or a value and an error. Cannot interpret meaning.")
}
value = castToFloat64(value)
return value, nil
}
}
func separatorStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
var ret []interface{}
switch left.(type) {
case []interface{}:
ret = append(left.([]interface{}), right)
default:
ret = []interface{}{left, right}
}
return ret, nil
}
func inStage(left interface{}, right interface{}, parameters Parameters) (interface{}, error) {
for _, value := range right.([]interface{}) {
if left == value {
return true, nil
}
}
return false, nil
}
//
func isString(value interface{}) bool {
switch value.(type) {
case string:
return true
}
return false
}
func isRegexOrString(value interface{}) bool {
switch value.(type) {
case string:
return true
case *regexp.Regexp:
return true
}
return false
}
func isBool(value interface{}) bool {
switch value.(type) {
case bool:
return true
}
return false
}
func isFloat64(value interface{}) bool {
switch value.(type) {
case float64:
return true
}
return false
}
/*
Addition usually means between numbers, but can also mean string concat.
String concat needs one (or both) of the sides to be a string.
*/
func additionTypeCheck(left interface{}, right interface{}) bool {
if isFloat64(left) && isFloat64(right) {
return true
}
if !isString(left) && !isString(right) {
return false
}
return true
}
/*
Comparison can either be between numbers, or lexicographic between two strings,
but never between the two.
*/
func comparatorTypeCheck(left interface{}, right interface{}) bool {
if isFloat64(left) && isFloat64(right) {
return true
}
if isString(left) && isString(right) {
return true
}
return false
}
func isArray(value interface{}) bool {
switch value.(type) {
case []interface{}:
return true
}
return false
}
/*
Converting a boolean to an interface{} requires an allocation.
We can use interned bools to avoid this cost.
*/
func boolIface(b bool) interface{} {
if b {
return _true
}
return _false
}
package govaluate
/*
Represents a function that can be called from within an expression.
This method must return an error if, for any reason, it is unable to produce exactly one unambiguous result.
An error returned will halt execution of the expression.
*/
type ExpressionFunction func(arguments ...interface{}) (interface{}, error)
package govaluate
import (
"bytes"
)
/*
Holds a series of "transactions" which represent each token as it is output by an outputter (such as ToSQLQuery()).
Some outputs (such as SQL) require a function call or non-c-like syntax to represent an expression.
To accomplish this, this struct keeps track of each translated token as it is output, and can return and rollback those transactions.
*/
type expressionOutputStream struct {
transactions []string
}
func (this *expressionOutputStream) add(transaction string) {
this.transactions = append(this.transactions, transaction)
}
func (this *expressionOutputStream) rollback() string {
index := len(this.transactions) - 1
ret := this.transactions[index]
this.transactions = this.transactions[:index]
return ret
}
func (this *expressionOutputStream) createString(delimiter string) string {
var retBuffer bytes.Buffer
var transaction string
penultimate := len(this.transactions) - 1
for i := 0; i < penultimate; i++ {
transaction = this.transactions[i]
retBuffer.WriteString(transaction)
retBuffer.WriteString(delimiter)
}
retBuffer.WriteString(this.transactions[penultimate])
return retBuffer.String()
}
package govaluate
import (
"errors"
"fmt"
)
type lexerState struct {
isEOF bool
isNullable bool
kind TokenKind
validNextKinds []TokenKind
}
// lexer states.
// Constant for all purposes except compiler.
var validLexerStates = []lexerState{
lexerState{
kind: UNKNOWN,
isEOF: false,
isNullable: true,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
BOOLEAN,
VARIABLE,
PATTERN,
FUNCTION,
ACCESSOR,
STRING,
TIME,
CLAUSE,
},
},
lexerState{
kind: CLAUSE,
isEOF: false,
isNullable: true,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
BOOLEAN,
VARIABLE,
PATTERN,
FUNCTION,
ACCESSOR,
STRING,
TIME,
CLAUSE,
CLAUSE_CLOSE,
},
},
lexerState{
kind: CLAUSE_CLOSE,
isEOF: true,
isNullable: true,
validNextKinds: []TokenKind{
COMPARATOR,
MODIFIER,
NUMERIC,
BOOLEAN,
VARIABLE,
STRING,
PATTERN,
TIME,
CLAUSE,
CLAUSE_CLOSE,
LOGICALOP,
TERNARY,
SEPARATOR,
},
},
lexerState{
kind: NUMERIC,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
TERNARY,
SEPARATOR,
},
},
lexerState{
kind: BOOLEAN,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
TERNARY,
SEPARATOR,
},
},
lexerState{
kind: STRING,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
TERNARY,
SEPARATOR,
},
},
lexerState{
kind: TIME,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
SEPARATOR,
},
},
lexerState{
kind: PATTERN,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
SEPARATOR,
},
},
lexerState{
kind: VARIABLE,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
TERNARY,
SEPARATOR,
},
},
lexerState{
kind: MODIFIER,
isEOF: false,
isNullable: false,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
VARIABLE,
FUNCTION,
ACCESSOR,
STRING,
BOOLEAN,
CLAUSE,
CLAUSE_CLOSE,
},
},
lexerState{
kind: COMPARATOR,
isEOF: false,
isNullable: false,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
BOOLEAN,
VARIABLE,
FUNCTION,
ACCESSOR,
STRING,
TIME,
CLAUSE,
CLAUSE_CLOSE,
PATTERN,
},
},
lexerState{
kind: LOGICALOP,
isEOF: false,
isNullable: false,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
BOOLEAN,
VARIABLE,
FUNCTION,
ACCESSOR,
STRING,
TIME,
CLAUSE,
CLAUSE_CLOSE,
},
},
lexerState{
kind: PREFIX,
isEOF: false,
isNullable: false,
validNextKinds: []TokenKind{
NUMERIC,
BOOLEAN,
VARIABLE,
FUNCTION,
ACCESSOR,
CLAUSE,
CLAUSE_CLOSE,
},
},
lexerState{
kind: TERNARY,
isEOF: false,
isNullable: false,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
BOOLEAN,
STRING,
TIME,
VARIABLE,
FUNCTION,
ACCESSOR,
CLAUSE,
SEPARATOR,
},
},
lexerState{
kind: FUNCTION,
isEOF: false,
isNullable: false,
validNextKinds: []TokenKind{
CLAUSE,
},
},
lexerState{
kind: ACCESSOR,
isEOF: true,
isNullable: false,
validNextKinds: []TokenKind{
CLAUSE,
MODIFIER,
COMPARATOR,
LOGICALOP,
CLAUSE_CLOSE,
TERNARY,
SEPARATOR,
},
},
lexerState{
kind: SEPARATOR,
isEOF: false,
isNullable: true,
validNextKinds: []TokenKind{
PREFIX,
NUMERIC,
BOOLEAN,
STRING,
TIME,
VARIABLE,
FUNCTION,
ACCESSOR,
CLAUSE,
},
},
}
func (this lexerState) canTransitionTo(kind TokenKind) bool {
for _, validKind := range this.validNextKinds {
if validKind == kind {
return true
}
}
return false
}
func checkExpressionSyntax(tokens []ExpressionToken) error {
var state lexerState
var lastToken ExpressionToken
var err error
state = validLexerStates[0]
for _, token := range tokens {
if !state.canTransitionTo(token.Kind) {
// call out a specific error for tokens looking like they want to be functions.
if lastToken.Kind == VARIABLE && token.Kind == CLAUSE {
return errors.New("Undefined function " + lastToken.Value.(string))
}
firstStateName := fmt.Sprintf("%s [%v]", state.kind.String(), lastToken.Value)
nextStateName := fmt.Sprintf("%s [%v]", token.Kind.String(), token.Value)
return errors.New("Cannot transition token types from " + firstStateName + " to " + nextStateName)
}
state, err = getLexerStateForToken(token.Kind)
if err != nil {
return err
}
if !state.isNullable && token.Value == nil {
errorMsg := fmt.Sprintf("Token kind '%v' cannot have a nil value", token.Kind.String())
return errors.New(errorMsg)
}
lastToken = token
}
if !state.isEOF {
return errors.New("Unexpected end of expression")
}
return nil
}
func getLexerStateForToken(kind TokenKind) (lexerState, error) {
for _, possibleState := range validLexerStates {
if possibleState.kind == kind {
return possibleState, nil
}
}
errorMsg := fmt.Sprintf("No lexer state found for token kind '%v'\n", kind.String())
return validLexerStates[0], errors.New(errorMsg)
}
package govaluate
type lexerStream struct {
source []rune
position int
length int
}
func newLexerStream(source string) *lexerStream {
var ret *lexerStream
var runes []rune
for _, character := range source {
runes = append(runes, character)
}
ret = new(lexerStream)
ret.source = runes
ret.length = len(runes)
return ret
}
func (this *lexerStream) readCharacter() rune {
var character rune
character = this.source[this.position]
this.position += 1
return character
}
func (this *lexerStream) rewind(amount int) {
this.position -= amount
}
func (this lexerStream) canRead() bool {
return this.position < this.length
}
package govaluate
import (
"errors"
)
/*
Parameters is a collection of named parameters that can be used by an EvaluableExpression to retrieve parameters
when an expression tries to use them.
*/
type Parameters interface {
/*
Get gets the parameter of the given name, or an error if the parameter is unavailable.
Failure to find the given parameter should be indicated by returning an error.
*/
Get(name string) (interface{}, error)
}
type MapParameters map[string]interface{}
func (p MapParameters) Get(name string) (interface{}, error) {
value, found := p[name]
if !found {
errorMessage := "No parameter '" + name + "' found."
return nil, errors.New(errorMessage)
}
return value, nil
}
package govaluate
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
func parseTokens(expression string, functions map[string]ExpressionFunction) ([]ExpressionToken, error) {
var ret []ExpressionToken
var token ExpressionToken
var stream *lexerStream
var state lexerState
var err error
var found bool
stream = newLexerStream(expression)
state = validLexerStates[0]
for stream.canRead() {
token, err, found = readToken(stream, state, functions)
if err != nil {
return ret, err
}
if !found {
break
}
state, err = getLexerStateForToken(token.Kind)
if err != nil {
return ret, err
}
// append this valid token
ret = append(ret, token)
}
err = checkBalance(ret)
if err != nil {
return nil, err
}
return ret, nil
}
func readToken(stream *lexerStream, state lexerState, functions map[string]ExpressionFunction) (ExpressionToken, error, bool) {
var function ExpressionFunction
var ret ExpressionToken
var tokenValue interface{}
var tokenTime time.Time
var tokenString string
var kind TokenKind
var character rune
var found bool
var completed bool
var err error
// numeric is 0-9, or . or 0x followed by digits
// string starts with '
// variable is alphanumeric, always starts with a letter
// bracket always means variable
// symbols are anything non-alphanumeric
// all others read into a buffer until they reach the end of the stream
for stream.canRead() {
character = stream.readCharacter()
if unicode.IsSpace(character) {
continue
}
kind = UNKNOWN
// numeric constant
if isNumeric(character) {
if stream.canRead() && character == '0' {
character = stream.readCharacter()
if stream.canRead() && character == 'x' {
tokenString, _ = readUntilFalse(stream, false, true, true, isHexDigit)
tokenValueInt, err := strconv.ParseUint(tokenString, 16, 64)
if err != nil {
errorMsg := fmt.Sprintf("Unable to parse hex value '%v' to uint64\n", tokenString)
return ExpressionToken{}, errors.New(errorMsg), false
}
kind = NUMERIC
tokenValue = float64(tokenValueInt)
break
} else {
stream.rewind(1)
}
}
tokenString = readTokenUntilFalse(stream, isNumeric)
tokenValue, err = strconv.ParseFloat(tokenString, 64)
if err != nil {
errorMsg := fmt.Sprintf("Unable to parse numeric value '%v' to float64\n", tokenString)
return ExpressionToken{}, errors.New(errorMsg), false
}
kind = NUMERIC
break
}
// comma, separator
if character == ',' {
tokenValue = ","
kind = SEPARATOR
break
}
// escaped variable
if character == '[' {
tokenValue, completed = readUntilFalse(stream, true, false, true, isNotClosingBracket)
kind = VARIABLE
if !completed {
return ExpressionToken{}, errors.New("Unclosed parameter bracket"), false
}
// above method normally rewinds us to the closing bracket, which we want to skip.
stream.rewind(-1)
break
}
// regular variable - or function?
if unicode.IsLetter(character) {
tokenString = readTokenUntilFalse(stream, isVariableName)
tokenValue = tokenString
kind = VARIABLE
// boolean?
if tokenValue == "true" {
kind = BOOLEAN
tokenValue = true
} else {
if tokenValue == "false" {
kind = BOOLEAN
tokenValue = false
}
}
// textual operator?
if tokenValue == "in" || tokenValue == "IN" {
// force lower case for consistency
tokenValue = "in"
kind = COMPARATOR
}
// function?
function, found = functions[tokenString]
if found {
kind = FUNCTION
tokenValue = function
}
// accessor?
accessorIndex := strings.Index(tokenString, ".")
if accessorIndex > 0 {
// check that it doesn't end with a hanging period
if tokenString[len(tokenString)-1] == '.' {
errorMsg := fmt.Sprintf("Hanging accessor on token '%s'", tokenString)
return ExpressionToken{}, errors.New(errorMsg), false
}
kind = ACCESSOR
splits := strings.Split(tokenString, ".")
tokenValue = splits
// check that none of them are unexported
for i := 1; i < len(splits); i++ {
firstCharacter := getFirstRune(splits[i])
if unicode.ToUpper(firstCharacter) != firstCharacter {
errorMsg := fmt.Sprintf("Unable to access unexported field '%s' in token '%s'", splits[i], tokenString)
return ExpressionToken{}, errors.New(errorMsg), false
}
}
}
break
}
if !isNotQuote(character) {
tokenValue, completed = readUntilFalse(stream, true, false, true, isNotQuote)
if !completed {
return ExpressionToken{}, errors.New("Unclosed string literal"), false
}
// advance the stream one position, since reading until false assumes the terminator is a real token
stream.rewind(-1)
// check to see if this can be parsed as a time.
tokenTime, found = tryParseTime(tokenValue.(string))
if found {
kind = TIME
tokenValue = tokenTime
} else {
kind = STRING
}
break
}
if character == '(' {
tokenValue = character
kind = CLAUSE
break
}
if character == ')' {
tokenValue = character
kind = CLAUSE_CLOSE
break
}
// must be a known symbol
tokenString = readTokenUntilFalse(stream, isNotAlphanumeric)
tokenValue = tokenString
// quick hack for the case where "-" can mean "prefixed negation" or "minus", which are used
// very differently.
if state.canTransitionTo(PREFIX) {
_, found = prefixSymbols[tokenString]
if found {
kind = PREFIX
break
}
}
_, found = modifierSymbols[tokenString]
if found {
kind = MODIFIER
break
}
_, found = logicalSymbols[tokenString]
if found {
kind = LOGICALOP
break
}
_, found = comparatorSymbols[tokenString]
if found {
kind = COMPARATOR
break
}
_, found = ternarySymbols[tokenString]
if found {
kind = TERNARY
break
}
errorMessage := fmt.Sprintf("Invalid token: '%s'", tokenString)
return ret, errors.New(errorMessage), false
}
ret.Kind = kind
ret.Value = tokenValue
return ret, nil, (kind != UNKNOWN)
}
func readTokenUntilFalse(stream *lexerStream, condition func(rune) bool) string {
var ret string
stream.rewind(1)
ret, _ = readUntilFalse(stream, false, true, true, condition)
return ret
}
/*
Returns the string that was read until the given [condition] was false, or whitespace was broken.
Returns false if the stream ended before whitespace was broken or condition was met.
*/
func readUntilFalse(stream *lexerStream, includeWhitespace bool, breakWhitespace bool, allowEscaping bool, condition func(rune) bool) (string, bool) {
var tokenBuffer bytes.Buffer
var character rune
var conditioned bool
conditioned = false
for stream.canRead() {
character = stream.readCharacter()
// Use backslashes to escape anything
if allowEscaping && character == '\\' {
character = stream.readCharacter()
tokenBuffer.WriteString(string(character))
continue
}
if unicode.IsSpace(character) {
if breakWhitespace && tokenBuffer.Len() > 0 {
conditioned = true
break
}
if !includeWhitespace {
continue
}
}
if condition(character) {
tokenBuffer.WriteString(string(character))
} else {
conditioned = true
stream.rewind(1)
break
}
}
return tokenBuffer.String(), conditioned
}
/*
Checks to see if any optimizations can be performed on the given [tokens], which form a complete, valid expression.
The returns slice will represent the optimized (or unmodified) list of tokens to use.
*/
func optimizeTokens(tokens []ExpressionToken) ([]ExpressionToken, error) {
var token ExpressionToken
var symbol OperatorSymbol
var err error
var index int
for index, token = range tokens {
// if we find a regex operator, and the right-hand value is a constant, precompile and replace with a pattern.
if token.Kind != COMPARATOR {
continue
}
symbol = comparatorSymbols[token.Value.(string)]
if symbol != REQ && symbol != NREQ {
continue
}
index++
token = tokens[index]
if token.Kind == STRING {
token.Kind = PATTERN
token.Value, err = regexp.Compile(token.Value.(string))
if err != nil {
return tokens, err
}
tokens[index] = token
}
}
return tokens, nil
}
/*
Checks the balance of tokens which have multiple parts, such as parenthesis.
*/
func checkBalance(tokens []ExpressionToken) error {
var stream *tokenStream
var token ExpressionToken
var parens int
stream = newTokenStream(tokens)
for stream.hasNext() {
token = stream.next()
if token.Kind == CLAUSE {
parens++
continue
}
if token.Kind == CLAUSE_CLOSE {
parens--
continue
}
}
if parens != 0 {
return errors.New("Unbalanced parenthesis")
}
return nil
}
func isDigit(character rune) bool {
return unicode.IsDigit(character)
}
func isHexDigit(character rune) bool {
character = unicode.ToLower(character)
return unicode.IsDigit(character) ||
character == 'a' ||
character == 'b' ||
character == 'c' ||
character == 'd' ||
character == 'e' ||
character == 'f'
}
func isNumeric(character rune) bool {
return unicode.IsDigit(character) || character == '.'
}
func isNotQuote(character rune) bool {
return character != '\'' && character != '"'
}
func isNotAlphanumeric(character rune) bool {
return !(unicode.IsDigit(character) ||
unicode.IsLetter(character) ||
character == '(' ||
character == ')' ||
character == '[' ||
character == ']' || // starting to feel like there needs to be an `isOperation` func (#59)
!isNotQuote(character))
}
func isVariableName(character rune) bool {
return unicode.IsLetter(character) ||
unicode.IsDigit(character) ||
character == '_' ||
character == '.'
}
func isNotClosingBracket(character rune) bool {
return character != ']'
}
/*
Attempts to parse the [candidate] as a Time.
Tries a series of standardized date formats, returns the Time if one applies,
otherwise returns false through the second return.
*/
func tryParseTime(candidate string) (time.Time, bool) {
var ret time.Time
var found bool
timeFormats := [...]string{
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.Kitchen,
time.RFC3339,
time.RFC3339Nano,
"2006-01-02", // RFC 3339
"2006-01-02 15:04", // RFC 3339 with minutes
"2006-01-02 15:04:05", // RFC 3339 with seconds
"2006-01-02 15:04:05-07:00", // RFC 3339 with seconds and timezone
"2006-01-02T15Z0700", // ISO8601 with hour
"2006-01-02T15:04Z0700", // ISO8601 with minutes
"2006-01-02T15:04:05Z0700", // ISO8601 with seconds
"2006-01-02T15:04:05.999999999Z0700", // ISO8601 with nanoseconds
}
for _, format := range timeFormats {
ret, found = tryParseExactTime(candidate, format)
if found {
return ret, true
}
}
return time.Now(), false
}
func tryParseExactTime(candidate string, format string) (time.Time, bool) {
var ret time.Time
var err error
ret, err = time.ParseInLocation(format, candidate, time.Local)
if err != nil {
return time.Now(), false
}
return ret, true
}
func getFirstRune(candidate string) rune {
for _, character := range candidate {
return character
}
return 0
}
package govaluate
// sanitizedParameters is a wrapper for Parameters that does sanitization as
// parameters are accessed.
type sanitizedParameters struct {
orig Parameters
}
func (p sanitizedParameters) Get(key string) (interface{}, error) {
value, err := p.orig.Get(key)
if err != nil {
return nil, err
}
return castToFloat64(value), nil
}
func castToFloat64(value interface{}) interface{} {
switch value.(type) {
case uint8:
return float64(value.(uint8))
case uint16:
return float64(value.(uint16))
case uint32:
return float64(value.(uint32))
case uint64:
return float64(value.(uint64))
case int8:
return float64(value.(int8))
case int16:
return float64(value.(int16))
case int32:
return float64(value.(int32))
case int64:
return float64(value.(int64))
case int:
return float64(value.(int))
case float32:
return float64(value.(float32))
}
return value
}
package govaluate
import (
"errors"
"fmt"
"time"
)
var stageSymbolMap = map[OperatorSymbol]evaluationOperator{
EQ: equalStage,
NEQ: notEqualStage,
GT: gtStage,
LT: ltStage,
GTE: gteStage,
LTE: lteStage,
REQ: regexStage,
NREQ: notRegexStage,
AND: andStage,
OR: orStage,
IN: inStage,
BITWISE_OR: bitwiseOrStage,
BITWISE_AND: bitwiseAndStage,
BITWISE_XOR: bitwiseXORStage,
BITWISE_LSHIFT: leftShiftStage,
BITWISE_RSHIFT: rightShiftStage,
PLUS: addStage,
MINUS: subtractStage,
MULTIPLY: multiplyStage,
DIVIDE: divideStage,
MODULUS: modulusStage,
EXPONENT: exponentStage,
NEGATE: negateStage,
INVERT: invertStage,
BITWISE_NOT: bitwiseNotStage,
TERNARY_TRUE: ternaryIfStage,
TERNARY_FALSE: ternaryElseStage,
COALESCE: ternaryElseStage,
SEPARATE: separatorStage,
}
/*
A "precedent" is a function which will recursively parse new evaluateionStages from a given stream of tokens.
It's called a `precedent` because it is expected to handle exactly what precedence of operator,
and defer to other `precedent`s for other operators.
*/
type precedent func(stream *tokenStream) (*evaluationStage, error)
/*
A convenience function for specifying the behavior of a `precedent`.
Most `precedent` functions can be described by the same function, just with different type checks, symbols, and error formats.
This struct is passed to `makePrecedentFromPlanner` to create a `precedent` function.
*/
type precedencePlanner struct {
validSymbols map[string]OperatorSymbol
validKinds []TokenKind
typeErrorFormat string
next precedent
nextRight precedent
}
var planPrefix precedent
var planExponential precedent
var planMultiplicative precedent
var planAdditive precedent
var planBitwise precedent
var planShift precedent
var planComparator precedent
var planLogicalAnd precedent
var planLogicalOr precedent
var planTernary precedent
var planSeparator precedent
func init() {
// all these stages can use the same code (in `planPrecedenceLevel`) to execute,
// they simply need different type checks, symbols, and recursive precedents.
// While not all precedent phases are listed here, most can be represented this way.
planPrefix = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: prefixSymbols,
validKinds: []TokenKind{PREFIX},
typeErrorFormat: prefixErrorFormat,
nextRight: planFunction,
})
planExponential = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: exponentialSymbolsS,
validKinds: []TokenKind{MODIFIER},
typeErrorFormat: modifierErrorFormat,
next: planFunction,
})
planMultiplicative = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: multiplicativeSymbols,
validKinds: []TokenKind{MODIFIER},
typeErrorFormat: modifierErrorFormat,
next: planExponential,
})
planAdditive = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: additiveSymbols,
validKinds: []TokenKind{MODIFIER},
typeErrorFormat: modifierErrorFormat,
next: planMultiplicative,
})
planShift = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: bitwiseShiftSymbols,
validKinds: []TokenKind{MODIFIER},
typeErrorFormat: modifierErrorFormat,
next: planAdditive,
})
planBitwise = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: bitwiseSymbols,
validKinds: []TokenKind{MODIFIER},
typeErrorFormat: modifierErrorFormat,
next: planShift,
})
planComparator = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: comparatorSymbols,
validKinds: []TokenKind{COMPARATOR},
typeErrorFormat: comparatorErrorFormat,
next: planBitwise,
})
planLogicalAnd = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: map[string]OperatorSymbol{"&&": AND},
validKinds: []TokenKind{LOGICALOP},
typeErrorFormat: logicalErrorFormat,
next: planComparator,
})
planLogicalOr = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: map[string]OperatorSymbol{"||": OR},
validKinds: []TokenKind{LOGICALOP},
typeErrorFormat: logicalErrorFormat,
next: planLogicalAnd,
})
planTernary = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: ternarySymbols,
validKinds: []TokenKind{TERNARY},
typeErrorFormat: ternaryErrorFormat,
next: planLogicalOr,
})
planSeparator = makePrecedentFromPlanner(&precedencePlanner{
validSymbols: separatorSymbols,
validKinds: []TokenKind{SEPARATOR},
next: planTernary,
})
}
/*
Given a planner, creates a function which will evaluate a specific precedence level of operators,
and link it to other `precedent`s which recurse to parse other precedence levels.
*/
func makePrecedentFromPlanner(planner *precedencePlanner) precedent {
var generated precedent
var nextRight precedent
generated = func(stream *tokenStream) (*evaluationStage, error) {
return planPrecedenceLevel(
stream,
planner.typeErrorFormat,
planner.validSymbols,
planner.validKinds,
nextRight,
planner.next,
)
}
if planner.nextRight != nil {
nextRight = planner.nextRight
} else {
nextRight = generated
}
return generated
}
/*
Creates a `evaluationStageList` object which represents an execution plan (or tree)
which is used to completely evaluate a set of tokens at evaluation-time.
The three stages of evaluation can be thought of as parsing strings to tokens, then tokens to a stage list, then evaluation with parameters.
*/
func planStages(tokens []ExpressionToken) (*evaluationStage, error) {
stream := newTokenStream(tokens)
stage, err := planTokens(stream)
if err != nil {
return nil, err
}
// while we're now fully-planned, we now need to re-order same-precedence operators.
// this could probably be avoided with a different planning method
reorderStages(stage)
stage = elideLiterals(stage)
return stage, nil
}
func planTokens(stream *tokenStream) (*evaluationStage, error) {
if !stream.hasNext() {
return nil, nil
}
return planSeparator(stream)
}
/*
The most usual method of parsing an evaluation stage for a given precedence.
Most stages use the same logic
*/
func planPrecedenceLevel(
stream *tokenStream,
typeErrorFormat string,
validSymbols map[string]OperatorSymbol,
validKinds []TokenKind,
rightPrecedent precedent,
leftPrecedent precedent) (*evaluationStage, error) {
var token ExpressionToken
var symbol OperatorSymbol
var leftStage, rightStage *evaluationStage
var checks typeChecks
var err error
var keyFound bool
if leftPrecedent != nil {
leftStage, err = leftPrecedent(stream)
if err != nil {
return nil, err
}
}
for stream.hasNext() {
token = stream.next()
if len(validKinds) > 0 {
keyFound = false
for _, kind := range validKinds {
if kind == token.Kind {
keyFound = true
break
}
}
if !keyFound {
break
}
}
if validSymbols != nil {
if !isString(token.Value) {
break
}
symbol, keyFound = validSymbols[token.Value.(string)]
if !keyFound {
break
}
}
if rightPrecedent != nil {
rightStage, err = rightPrecedent(stream)
if err != nil {
return nil, err
}
}
checks = findTypeChecks(symbol)
return &evaluationStage{
symbol: symbol,
leftStage: leftStage,
rightStage: rightStage,
operator: stageSymbolMap[symbol],
leftTypeCheck: checks.left,
rightTypeCheck: checks.right,
typeCheck: checks.combined,
typeErrorFormat: typeErrorFormat,
}, nil
}
stream.rewind()
return leftStage, nil
}
/*
A special case where functions need to be of higher precedence than values, and need a special wrapped execution stage operator.
*/
func planFunction(stream *tokenStream) (*evaluationStage, error) {
var token ExpressionToken
var rightStage *evaluationStage
var err error
token = stream.next()
if token.Kind != FUNCTION {
stream.rewind()
return planAccessor(stream)
}
rightStage, err = planAccessor(stream)
if err != nil {
return nil, err
}
return &evaluationStage{
symbol: FUNCTIONAL,
rightStage: rightStage,
operator: makeFunctionStage(token.Value.(ExpressionFunction)),
typeErrorFormat: "Unable to run function '%v': %v",
}, nil
}
func planAccessor(stream *tokenStream) (*evaluationStage, error) {
var token, otherToken ExpressionToken
var rightStage *evaluationStage
var err error
if !stream.hasNext() {
return nil, nil
}
token = stream.next()
if token.Kind != ACCESSOR {
stream.rewind()
return planValue(stream)
}
// check if this is meant to be a function or a field.
// fields have a clause next to them, functions do not.
// if it's a function, parse the arguments. Otherwise leave the right stage null.
if stream.hasNext() {
otherToken = stream.next()
if otherToken.Kind == CLAUSE {
stream.rewind()
rightStage, err = planTokens(stream)
if err != nil {
return nil, err
}
} else {
stream.rewind()
}
}
return &evaluationStage{
symbol: ACCESS,
rightStage: rightStage,
operator: makeAccessorStage(token.Value.([]string)),
typeErrorFormat: "Unable to access parameter field or method '%v': %v",
}, nil
}
/*
A truly special precedence function, this handles all the "lowest-case" errata of the process, including literals, parmeters,
clauses, and prefixes.
*/
func planValue(stream *tokenStream) (*evaluationStage, error) {
var token ExpressionToken
var symbol OperatorSymbol
var ret *evaluationStage
var operator evaluationOperator
var err error
if !stream.hasNext() {
return nil, nil
}
token = stream.next()
switch token.Kind {
case CLAUSE:
ret, err = planTokens(stream)
if err != nil {
return nil, err
}
// advance past the CLAUSE_CLOSE token. We know that it's a CLAUSE_CLOSE, because at parse-time we check for unbalanced parens.
stream.next()
// the stage we got represents all of the logic contained within the parens
// but for technical reasons, we need to wrap this stage in a "noop" stage which breaks long chains of precedence.
// see github #33.
ret = &evaluationStage{
rightStage: ret,
operator: noopStageRight,
symbol: NOOP,
}
return ret, nil
case CLAUSE_CLOSE:
// when functions have empty params, this will be hit. In this case, we don't have any evaluation stage to do,
// so we just return nil so that the stage planner continues on its way.
stream.rewind()
return nil, nil
case VARIABLE:
operator = makeParameterStage(token.Value.(string))
case NUMERIC:
fallthrough
case STRING:
fallthrough
case PATTERN:
fallthrough
case BOOLEAN:
symbol = LITERAL
operator = makeLiteralStage(token.Value)
case TIME:
symbol = LITERAL
operator = makeLiteralStage(float64(token.Value.(time.Time).Unix()))
case PREFIX:
stream.rewind()
return planPrefix(stream)
}
if operator == nil {
errorMsg := fmt.Sprintf("Unable to plan token kind: '%s', value: '%v'", token.Kind.String(), token.Value)
return nil, errors.New(errorMsg)
}
return &evaluationStage{
symbol: symbol,
operator: operator,
}, nil
}
/*
Convenience function to pass a triplet of typechecks between `findTypeChecks` and `planPrecedenceLevel`.
Each of these members may be nil, which indicates that type does not matter for that value.
*/
type typeChecks struct {
left stageTypeCheck
right stageTypeCheck
combined stageCombinedTypeCheck
}
/*
Maps a given [symbol] to a set of typechecks to be used during runtime.
*/
func findTypeChecks(symbol OperatorSymbol) typeChecks {
switch symbol {
case GT:
fallthrough
case LT:
fallthrough
case GTE:
fallthrough
case LTE:
return typeChecks{
combined: comparatorTypeCheck,
}
case REQ:
fallthrough
case NREQ:
return typeChecks{
left: isString,
right: isRegexOrString,
}
case AND:
fallthrough
case OR:
return typeChecks{
left: isBool,
right: isBool,
}
case IN:
return typeChecks{
right: isArray,
}
case BITWISE_LSHIFT:
fallthrough
case BITWISE_RSHIFT:
fallthrough
case BITWISE_OR:
fallthrough
case BITWISE_AND:
fallthrough
case BITWISE_XOR:
return typeChecks{
left: isFloat64,
right: isFloat64,
}
case PLUS:
return typeChecks{
combined: additionTypeCheck,
}
case MINUS:
fallthrough
case MULTIPLY:
fallthrough
case DIVIDE:
fallthrough
case MODULUS:
fallthrough
case EXPONENT:
return typeChecks{
left: isFloat64,
right: isFloat64,
}
case NEGATE:
return typeChecks{
right: isFloat64,
}
case INVERT:
return typeChecks{
right: isBool,
}
case BITWISE_NOT:
return typeChecks{
right: isFloat64,
}
case TERNARY_TRUE:
return typeChecks{
left: isBool,
}
// unchecked cases
case EQ:
fallthrough
case NEQ:
return typeChecks{}
case TERNARY_FALSE:
fallthrough
case COALESCE:
fallthrough
default:
return typeChecks{}
}
}
/*
During stage planning, stages of equal precedence are parsed such that they'll be evaluated in reverse order.
For commutative operators like "+" or "-", it's no big deal. But for order-specific operators, it ruins the expected result.
*/
func reorderStages(rootStage *evaluationStage) {
// traverse every rightStage until we find multiples in a row of the same precedence.
var identicalPrecedences []*evaluationStage
var currentStage, nextStage *evaluationStage
var precedence, currentPrecedence operatorPrecedence
nextStage = rootStage
precedence = findOperatorPrecedenceForSymbol(rootStage.symbol)
for nextStage != nil {
currentStage = nextStage
nextStage = currentStage.rightStage
// left depth first, since this entire method only looks for precedences down the right side of the tree
if currentStage.leftStage != nil {
reorderStages(currentStage.leftStage)
}
currentPrecedence = findOperatorPrecedenceForSymbol(currentStage.symbol)
if currentPrecedence == precedence {
identicalPrecedences = append(identicalPrecedences, currentStage)
continue
}
// precedence break.
// See how many in a row we had, and reorder if there's more than one.
if len(identicalPrecedences) > 1 {
mirrorStageSubtree(identicalPrecedences)
}
identicalPrecedences = []*evaluationStage{currentStage}
precedence = currentPrecedence
}
if len(identicalPrecedences) > 1 {
mirrorStageSubtree(identicalPrecedences)
}
}
/*
Performs a "mirror" on a subtree of stages.
This mirror functionally inverts the order of execution for all members of the [stages] list.
That list is assumed to be a root-to-leaf (ordered) list of evaluation stages, where each is a right-hand stage of the last.
*/
func mirrorStageSubtree(stages []*evaluationStage) {
var rootStage, inverseStage, carryStage, frontStage *evaluationStage
stagesLength := len(stages)
// reverse all right/left
for _, frontStage = range stages {
carryStage = frontStage.rightStage
frontStage.rightStage = frontStage.leftStage
frontStage.leftStage = carryStage
}
// end left swaps with root right
rootStage = stages[0]
frontStage = stages[stagesLength-1]
carryStage = frontStage.leftStage
frontStage.leftStage = rootStage.rightStage
rootStage.rightStage = carryStage
// for all non-root non-end stages, right is swapped with inverse stage right in list
for i := 0; i < (stagesLength-2)/2+1; i++ {
frontStage = stages[i+1]
inverseStage = stages[stagesLength-i-1]
carryStage = frontStage.rightStage
frontStage.rightStage = inverseStage.rightStage
inverseStage.rightStage = carryStage
}
// swap all other information with inverse stages
for i := 0; i < stagesLength/2; i++ {
frontStage = stages[i]
inverseStage = stages[stagesLength-i-1]
frontStage.swapWith(inverseStage)
}
}
/*
Recurses through all operators in the entire tree, eliding operators where both sides are literals.
*/
func elideLiterals(root *evaluationStage) *evaluationStage {
if root.leftStage != nil {
root.leftStage = elideLiterals(root.leftStage)
}
if root.rightStage != nil {
root.rightStage = elideLiterals(root.rightStage)
}
return elideStage(root)
}
/*
Elides a specific stage, if possible.
Returns the unmodified [root] stage if it cannot or should not be elided.
Otherwise, returns a new stage representing the condensed value from the elided stages.
*/
func elideStage(root *evaluationStage) *evaluationStage {
var leftValue, rightValue, result interface{}
var err error
// right side must be a non-nil value. Left side must be nil or a value.
if root.rightStage == nil ||
root.rightStage.symbol != LITERAL ||
root.leftStage == nil ||
root.leftStage.symbol != LITERAL {
return root
}
// don't elide some operators
switch root.symbol {
case SEPARATE:
fallthrough
case IN:
return root
}
// both sides are values, get their actual values.
// errors should be near-impossible here. If we encounter them, just abort this optimization.
leftValue, err = root.leftStage.operator(nil, nil, nil)
if err != nil {
return root
}
rightValue, err = root.rightStage.operator(nil, nil, nil)
if err != nil {
return root
}
// typcheck, since the grammar checker is a bit loose with which operator symbols go together.
err = typeCheck(root.leftTypeCheck, leftValue, root.symbol, root.typeErrorFormat)
if err != nil {
return root
}
err = typeCheck(root.rightTypeCheck, rightValue, root.symbol, root.typeErrorFormat)
if err != nil {
return root
}
if root.typeCheck != nil && !root.typeCheck(leftValue, rightValue) {
return root
}
// pre-calculate, and return a new stage representing the result.
result, err = root.operator(leftValue, rightValue, nil)
if err != nil {
return root
}
return &evaluationStage{
symbol: LITERAL,
operator: makeLiteralStage(result),
}
}
#!/bin/bash
# Script that runs tests, code coverage, and benchmarks all at once.
# Builds a symlink in /tmp, mostly to avoid messing with GOPATH at the user's shell level.
TEMPORARY_PATH="/tmp/govaluate_test"
SRC_PATH="${TEMPORARY_PATH}/src"
FULL_PATH="${TEMPORARY_PATH}/src/govaluate"
# set up temporary directory
rm -rf "${FULL_PATH}"
mkdir -p "${SRC_PATH}"
ln -s $(pwd) "${FULL_PATH}"
export GOPATH="${TEMPORARY_PATH}"
pushd "${TEMPORARY_PATH}/src/govaluate"
# run the actual tests.
export GOVALUATE_TORTURE_TEST="true"
go test -bench=. -benchmem #-coverprofile coverage.out
status=$?
if [ "${status}" != 0 ];
then
exit $status
fi
# coverage
# disabled because travis go1.4 seems not to support it suddenly?
#go tool cover -func=coverage.out
popd
package govaluate
type tokenStream struct {
tokens []ExpressionToken
index int
tokenLength int
}
func newTokenStream(tokens []ExpressionToken) *tokenStream {
var ret *tokenStream
ret = new(tokenStream)
ret.tokens = tokens
ret.tokenLength = len(tokens)
return ret
}
func (this *tokenStream) rewind() {
this.index -= 1
}
func (this *tokenStream) next() ExpressionToken {
var token ExpressionToken
token = this.tokens[this.index]
this.index += 1
return token
}
func (this tokenStream) hasNext() bool {
return this.index < this.tokenLength
}
goyaml2
=======
YAML for Golang
\ No newline at end of file
package goyaml2
import (
"regexp"
"strconv"
//"time"
)
var (
RE_INT, _ = regexp.Compile("^[0-9,]+$")
RE_FLOAT, _ = regexp.Compile("^[0-9]+[.][0-9]+$")
RE_DATE, _ = regexp.Compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")
RE_TIME, _ = regexp.Compile("^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$")
)
func string2Val(str string) interface{} {
tmp := []byte(str)
switch {
case str == "false":
return false
case str == "true":
return true
case RE_INT.Match(tmp):
// TODO check err
_int, _ := strconv.ParseInt(str, 10, 64)
return _int
case RE_FLOAT.Match(tmp):
_float, _ := strconv.ParseFloat(str, 64)
return _float
//TODO support time or Not?
/*
case RE_DATE.Match(tmp):
_date, _ := time.Parse("2006-01-02", str)
return _date
case RE_TIME.Match(tmp):
_time, _ := time.Parse("2006-01-02 03:04:05", str)
return _time
*/
}
return str
}
package goyaml2
import (
"bufio"
"fmt"
"github.com/wendal/errors"
"io"
"log"
"strings"
)
const (
DEBUG = true
MAP_KEY_ONLY = iota
)
func Read(r io.Reader) (interface{}, error) {
yr := &yamlReader{}
yr.br = bufio.NewReader(r)
obj, err := yr.ReadObject(0)
if err == io.EOF {
err = nil
}
if obj == nil {
log.Println("Obj == nil")
}
return obj, err
}
type yamlReader struct {
br *bufio.Reader
nodes []interface{}
lineNum int
lastLine string
}
func (y *yamlReader) ReadObject(minIndent int) (interface{}, error) {
line, err := y.NextLine()
if err != nil {
if err == io.EOF && line != "" {
//log.Println("Read EOF , but still some data here")
} else {
//log.Println("ReadERR", err)
return nil, err
}
}
y.lastLine = line
indent, str := getIndent(line)
if indent < minIndent {
//log.Println("Current Indent Unexpect : ", str, indent, minIndent)
return nil, y.Error("Unexpect Indent", nil)
}
if indent > minIndent {
//log.Println("Change minIndent from %d to %d", minIndent, indent)
minIndent = indent
}
switch str[0] {
case '-':
return y.ReadList(minIndent)
case '[':
fallthrough
case '{':
y.lastLine = ""
_, value, err := y.asMapKeyValue("tmp:" + str)
if err != nil {
return nil, y.Error("Err inline map/list", nil)
}
return value, nil
}
//log.Println("Read Objcet as Map", indent, str)
return y.ReadMap(minIndent)
}
func (y *yamlReader) ReadList(minIndent int) ([]interface{}, error) {
list := []interface{}{}
for {
line, err := y.NextLine()
if err != nil {
return list, err
}
indent, str := getIndent(line)
switch {
case indent < minIndent:
y.lastLine = line
if len(list) == 0 {
return nil, nil
}
return list, nil
case indent == minIndent:
if str[0] != '-' {
y.lastLine = line
return list, nil
}
if len(str) < 2 {
return nil, y.Error("ListItem is Emtry", nil)
}
key, value, err := y.asMapKeyValue(str[1:])
if err != nil {
return nil, err
}
switch value {
case nil:
list = append(list, key)
case MAP_KEY_ONLY:
return nil, y.Error("Not support List-Map yet", nil)
default:
_map := map[string]interface{}{key.(string): value}
list = append(list, _map)
_line, _err := y.NextLine()
if _err != nil && _err != io.EOF {
return nil, err
}
if _line == "" {
return list, nil
}
y.lastLine = _line
_indent, _str := getIndent(line)
if _indent >= minIndent+2 {
switch _str[0] {
case '-':
return nil, y.Error("Unexpect", nil)
case '[':
return nil, y.Error("Unexpect", nil)
case '{':
return nil, y.Error("Unexpect", nil)
}
// look like a map
_map2, _err := y.ReadMap(_indent)
if _map2 != nil {
_map2[key.(string)] = value
}
if err != nil {
return list, _err
}
}
}
continue
default:
return nil, y.Error("Bad Indent\n"+line, nil)
}
}
panic("ERROR")
return nil, errors.New("Impossible")
}
func (y *yamlReader) ReadMap(minIndent int) (map[string]interface{}, error) {
_map := map[string]interface{}{}
//log.Println("ReadMap", minIndent)
OUT:
for {
line, err := y.NextLine()
if err != nil {
return _map, err
}
indent, str := getIndent(line)
//log.Printf("Indent : %d, str = %s", indent, str)
switch {
case indent < minIndent:
y.lastLine = line
if len(_map) == 0 {
return nil, nil
}
return _map, nil
case indent == minIndent:
key, value, err := y.asMapKeyValue(str)
if err != nil {
return nil, err
}
//log.Println("Key=", key, "value=", value)
switch value {
case nil:
return nil, y.Error("Unexpect", nil)
case MAP_KEY_ONLY:
//log.Println("KeyOnly, read inner Map", key)
//--------------------------------------
_line, err := y.NextLine()
if err != nil {
if err == io.EOF {
if _line == "" {
// Emtry map item?
_map[key.(string)] = nil
return _map, err
}
} else {
return nil, y.Error("ERR?", err)
}
}
y.lastLine = _line
_indent, _str := getIndent(_line)
if _indent < minIndent {
return _map, nil
}
////log.Println("##>>", _indent, _str)
if _indent == minIndent {
if _str[0] == '-' {
//log.Println("Read Same-Indent ListItem for Map")
_list, err := y.ReadList(minIndent)
if _list != nil {
_map[key.(string)] = _list
}
if err != nil {
return _map, nil
}
continue OUT
} else {
// Emtry map item?
_map[key.(string)] = nil
continue OUT
}
}
//--------------------------------------
//log.Println("Read Map Item", _indent, _str)
obj, err := y.ReadObject(_indent)
if obj != nil {
_map[key.(string)] = obj
}
if err != nil {
return _map, err
}
default:
_map[key.(string)] = value
}
default:
//log.Println("Bad", indent, str)
return nil, y.Error("Bad Indent\n"+line, nil)
}
}
panic("ERROR")
return nil, errors.New("Impossible")
}
func (y *yamlReader) NextLine() (line string, err error) {
if y.lastLine != "" {
line = y.lastLine
y.lastLine = ""
//log.Println("Return lastLine", line)
return
}
for {
line, err = y.br.ReadString('\n')
y.lineNum++
if err != nil {
return
}
if strings.HasPrefix(line, "---") || strings.HasPrefix(line, "#") {
continue
}
line = strings.TrimRight(line, "\n\t\r ")
if line == "" {
continue
}
//log.Println("Return Line", line)
return
}
//log.Println("Impossbible : " + line)
return // impossbile!
}
func getIndent(str string) (int, string) {
indent := 0
for i, s := range str {
switch s {
case ' ':
indent++
case '\t':
indent += 4
default:
return indent, str[i:]
}
}
panic("Invalid indent : " + str)
return -1, ""
}
func (y *yamlReader) asMapKeyValue(str string) (key interface{}, val interface{}, err error) {
tokens := splitToken(str)
key = tokens[0]
if len(tokens) == 1 {
return key, nil, nil
}
if tokens[1] != ":" {
return "", nil, y.Error("Unexpect "+str, nil)
}
if len(tokens) == 2 {
return key, MAP_KEY_ONLY, nil
}
if len(tokens) == 3 {
return key, tokens[2], nil
}
switch tokens[2] {
case "[":
list := []interface{}{}
for i := 3; i < len(tokens)-1; i++ {
list = append(list, tokens[i])
}
return key, list, nil
case "{":
_map := map[string]interface{}{}
for i := 3; i < len(tokens)-1; i += 4 {
//log.Println(">>>", i, tokens[i])
if i > len(tokens)-2 {
return "", nil, y.Error("Unexpect "+str, nil)
}
if tokens[i+1] != ":" {
return "", nil, y.Error("Unexpect "+str, nil)
}
_map[tokens[i].(string)] = tokens[i+2]
if (i + 3) < (len(tokens) - 1) {
if tokens[i+3] != "," {
return "", "", y.Error("Unexpect "+str, nil)
}
} else {
break
}
}
return key, _map, nil
}
//log.Println(str, tokens)
return "", nil, y.Error("Unexpect "+str, nil)
}
func splitToken(str string) (tokens []interface{}) {
str = strings.Trim(str, "\r\t\n ")
if str == "" {
panic("Impossbile")
return
}
tokens = []interface{}{}
lastPos := 0
for i := 0; i < len(str); i++ {
switch str[i] {
case ':':
fallthrough
case '{':
fallthrough
case '[':
fallthrough
case '}':
fallthrough
case ']':
fallthrough
case ',':
if i > lastPos {
tokens = append(tokens, str[lastPos:i])
}
tokens = append(tokens, str[i:i+1])
lastPos = i + 1
case ' ':
if i > lastPos {
tokens = append(tokens, str[lastPos:i])
}
lastPos = i + 1
case '\'':
//log.Println("Scan End of String")
i++
start := i
for ; i < len(str); i++ {
if str[i] == '\'' {
//log.Println("Found End of String", start, i)
break
}
}
tokens = append(tokens, str[start:i])
lastPos = i + 1
case '"':
i++
start := i
for ; i < len(str); i++ {
if str[i] == '"' {
break
}
}
tokens = append(tokens, str[start:i])
lastPos = i + 1
}
}
////log.Println("last", lastPos)
if lastPos < len(str) {
tokens = append(tokens, str[lastPos:])
}
if len(tokens) == 1 {
tokens[0] = string2Val(tokens[0].(string))
return
}
if tokens[1] == ":" {
if len(tokens) == 2 {
return
}
if tokens[2] == "{" || tokens[2] == "[" {
return
}
str = strings.Trim(strings.SplitN(str, ":", 2)[1], "\t ")
if len(str) > 2 {
if str[0] == '\'' && str[len(str)-1] == '\'' {
str = str[1 : len(str)-1]
} else if str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}
}
val := string2Val(str)
tokens = []interface{}{tokens[0], tokens[1], val}
return
}
if len(str) > 2 {
if str[0] == '\'' && str[len(str)-1] == '\'' {
str = str[1 : len(str)-1]
} else if str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}
}
val := string2Val(str)
tokens = []interface{}{val}
return
}
func (y *yamlReader) Error(msg string, err error) error {
if err != nil {
return errors.New(fmt.Sprintf("line %d : %s : %v", y.lineNum, msg, err.Error()))
}
return errors.New(fmt.Sprintf("line %d >> %s", y.lineNum, msg))
}
package goyaml2
const (
N_Map = iota
N_List
N_String
)
type Node interface {
Type() int
}
type MapNode map[string]interface{}
type ListNode []interface{}
type StringNode string
func (m *MapNode) Type() int {
return N_Map
}
func (l *ListNode) Type() int {
return N_List
}
func (s *StringNode) Type() int {
return N_String
}
package goyaml2
import (
"io"
)
func Write(w io.Writer, v interface{}) error {
return nil
}
Copyright (c) 2012-2013 Charles Banning <clbanning@gmail.com>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
x2j.go - Unmarshal dynamic / arbitrary XML docs and extract values (using wildcards, if necessary).
ANNOUNCEMENTS
20 December 2013:
Non-UTF8 character sets supported via the X2jCharsetReader variable.
12 December 2013:
For symmetry, the package j2x has functions that marshal JSON strings and
map[string]interface{} values to XML encoded strings: http://godoc.org/github.com/clbanning/j2x.
Also, ToTree(), ToMap(), ToJson(), ToJsonIndent(), ReaderValuesFromTagPath() and ReaderValuesForTag() use io.Reader instead of string or []byte.
If you want to process a stream of XML messages check out XmlMsgsFromReader().
MOTIVATION
I make extensive use of JSON for messaging and typically unmarshal the messages into
map[string]interface{} variables. This is easily done using json.Unmarshal from the
standard Go libraries. Unfortunately, many legacy solutions use structured
XML messages; in those environments the applications would have to be refitted to
interoperate with my components.
The better solution is to just provide an alternative HTTP handler that receives
XML doc messages and parses it into a map[string]interface{} variable and then reuse
all the JSON-based code. The Go xml.Unmarshal() function does not provide the same
option of unmarshaling XML messages into map[string]interface{} variables. So I wrote
a couple of small functions to fill this gap.
Of course, once the XML doc was unmarshal'd into a map[string]interface{} variable it
was just a matter of calling json.Marshal() to provide it as a JSON string. Hence 'x2j'
rather than just 'x2m'.
USAGE
The package is fairly well self-documented. (http://godoc.org/github.com/clbanning/x2j)
The one really useful function is:
- Unmarshal(doc []byte, v interface{}) error
where v is a pointer to a variable of type 'map[string]interface{}', 'string', or
any other type supported by xml.Unmarshal().
To retrieve a value for specific tag use:
- DocValue(doc, path string, attrs ...string) (interface{},error)
- MapValue(m map[string]interface{}, path string, attr map[string]interface{}, recast ...bool) (interface{}, error)
The 'path' argument is a period-separated tag hierarchy - also known as dot-notation.
It is the program's responsibility to cast the returned value to the proper type; possible
types are the normal JSON unmarshaling types: string, float64, bool, []interface, map[string]interface{}.
To retrieve all values associated with a tag occurring anywhere in the XML document use:
- ValuesForTag(doc, tag string) ([]interface{}, error)
- ValuesForKey(m map[string]interface{}, key string) []interface{}
Demos: http://play.golang.org/p/m8zP-cpk0O
http://play.golang.org/p/cIteTS1iSg
http://play.golang.org/p/vd8pMiI21b
Returned values should be one of map[string]interface, []interface{}, or string.
All the values assocated with a tag-path that may include one or more wildcard characters -
'*' - can also be retrieved using:
- ValuesFromTagPath(doc, path string, getAttrs ...bool) ([]interface{}, error)
- ValuesFromKeyPath(map[string]interface{}, path string, getAttrs ...bool) []interface{}
Demos: http://play.golang.org/p/kUQnZ8VuhS
http://play.golang.org/p/l1aMHYtz7G
NOTE: care should be taken when using "*" at the end of a path - i.e., "books.book.*". See
the x2jpath_test.go case on how the wildcard returns all key values and collapses list values;
the same message structure can load a []interface{} or a map[string]interface{} (or an interface{})
value for a tag.
See the test cases in "x2jpath_test.go" and programs in "example" subdirectory for more.
XML PARSING CONVENTIONS
- Attributes are parsed to map[string]interface{} values by prefixing a hyphen, '-',
to the attribute label.
- If the element is a simple element and has attributes, the element value
is given the key '#text' for its map[string]interface{} representation. (See
the 'atomFeedString.xml' test data, below.)
BULK PROCESSING OF MESSAGE FILES
Sometime messages may be logged into files for transmission via FTP (e.g.) and subsequent
processing. You can use the bulk XML message processor to convert files of XML messages into
map[string]interface{} values with custom processing and error handler functions. See
the notes and test code for:
- XmlMsgsFromFile(fname string, phandler func(map[string]interface{}) bool, ehandler func(error) bool,recast ...bool) error
IMPLEMENTATION NOTES
Nothing fancy here, just brute force.
- Use xml.Decoder to parse the XML doc and build a tree.
- Walk the tree and load values into a map[string]interface{} variable, 'm', as
appropriate.
- Use json.Marshaler to convert 'm' to JSON.
As for testing:
- Copy an XML doc into 'x2j_test.xml'.
- Run "go test" and you'll get a full dump.
("pathTestString.xml" and "atomFeedString.xml" are test data from "read_test.go"
in the encoding/xml directory of the standard package library.)
USES
- putting a XML API on our message hub middleware (http://jsonhub.net)
- loading XML data into NoSQL database, such as, mongoDB
PERFORMANCE IMPROVEMENTS WITH GO 1.1 and 1.2
Upgrading to Go 1.1 environment results in performance improvements for XML and JSON
unmarshalling, in general. The x2j package gets an average performance boost of 40%.
----- Go 1.0.2 ----- ----------- Go 1.1 -----------
iterations ns/op iterations ns/op % improved
Benchmark_UseXml-4 100000 18776 200000 10377 45%
Benchmark_UseX2j-4 50000 55323 50000 33958 39%
Benchmark_UseJson-4 1000000 2257 1000000 1484 34%
Benchmark_UseJsonToMap-4 1000000 2531 1000000 1566 38%
BenchmarkBig_UseXml-4 100000 28918 100000 15876 45%
BenchmarkBig_UseX2j-4 20000 86338 50000 52661 39%
BenchmarkBig_UseJson-4 500000 4448 1000000 2664 40%
BenchmarkBig_UseJsonToMap-4 200000 9076 500000 5753 37%
BenchmarkBig3_UseXml-4 50000 42224 100000 24686 42%
BenchmarkBig3_UseX2j-4 10000 147407 20000 84332 43%
BenchmarkBig3_UseJson-4 500000 5921 500000 3930 34%
BenchmarkBig3_UseJsonToMap-4 200000 13037 200000 8670 33%
The x2j package gets an additional 15-20% performance boost going to Go 1.2.
------ Go 1.1 ------ ----------- Go 1.2 -----------
iterations ns/op iterations ns/op % improved
Benchmark_UseXml-4 200000 10377 200000 11031 -6%
Benchmark_UseX2j-4 50000 33958 100000 29188 14%
Benchmark_UseJson-4 1000000 1484 1000000 1347 9%
Benchmark_UseJsonToMap-4 1000000 1566 1000000 1434 8%
BenchmarkBig_UseXml-4 100000 15876 100000 16585 -4%
BenchmarkBig_UseX2j-4 50000 52661 50000 43452 17%
BenchmarkBig_UseJson-4 1000000 2664 1000000 2523 5%
BenchmarkBig_UseJsonToMap-4 500000 5753 500000 4992 13%
BenchmarkBig3_UseXml-4 100000 24686 100000 24348 1%
BenchmarkBig3_UseX2j-4 20000 84332 50000 66736 21%
BenchmarkBig3_UseJson-4 500000 3930 500000 3733 5%
BenchmarkBig3_UseJsonToMap-4 200000 8670 200000 7810 10%
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us" updated="2009-10-04T01:35:58+00:00"><title>Code Review - My issues</title><link href="http://codereview.appspot.com/" rel="alternate"></link><link href="http://codereview.appspot.com/rss/mine/rsc" rel="self"></link><id>http://codereview.appspot.com/</id><author><name>rietveld&lt;&gt;</name></author><entry><title>rietveld: an attempt at pubsubhubbub
</title><link href="http://codereview.appspot.com/126085" rel="alternate"></link><updated>2009-10-04T01:35:58+00:00</updated><author><name>email-address-removed</name></author><id>urn:md5:134d9179c41f806be79b3a5f7877d19a</id><summary type="html">
An attempt at adding pubsubhubbub support to Rietveld.
http://code.google.com/p/pubsubhubbub
http://code.google.com/p/rietveld/issues/detail?id=155
The server side of the protocol is trivial:
1. add a &amp;lt;link rel=&amp;quot;hub&amp;quot; href=&amp;quot;hub-server&amp;quot;&amp;gt; tag to all
feeds that will be pubsubhubbubbed.
2. every time one of those feeds changes, tell the hub
with a simple POST request.
I have tested this by adding debug prints to a local hub
server and checking that the server got the right publish
requests.
I can&amp;#39;t quite get the server to work, but I think the bug
is not in my code. I think that the server expects to be
able to grab the feed and see the feed&amp;#39;s actual URL in
the link rel=&amp;quot;self&amp;quot;, but the default value for that drops
the :port from the URL, and I cannot for the life of me
figure out how to get the Atom generator deep inside
django not to do that, or even where it is doing that,
or even what code is running to generate the Atom feed.
(I thought I knew but I added some assert False statements
and it kept running!)
Ignoring that particular problem, I would appreciate
feedback on the right way to get the two values at
the top of feeds.py marked NOTE(rsc).
</summary></entry><entry><title>rietveld: correct tab handling
</title><link href="http://codereview.appspot.com/124106" rel="alternate"></link><updated>2009-10-03T23:02:17+00:00</updated><author><name>email-address-removed</name></author><id>urn:md5:0a2a4f19bb815101f0ba2904aed7c35a</id><summary type="html">
This fixes the buggy tab rendering that can be seen at
http://codereview.appspot.com/116075/diff/1/2
The fundamental problem was that the tab code was
not being told what column the text began in, so it
didn&amp;#39;t know where to put the tab stops. Another problem
was that some of the code assumed that string byte
offsets were the same as column offsets, which is only
true if there are no tabs.
In the process of fixing this, I cleaned up the arguments
to Fold and ExpandTabs and renamed them Break and
_ExpandTabs so that I could be sure that I found all the
call sites. I also wanted to verify that ExpandTabs was
not being used from outside intra_region_diff.py.
</summary></entry></feed> `
<Result>
<Before>1</Before>
<Items>
<Item1>
<Value>A</Value>
</Item1>
<Item2>
<Value>B</Value>
</Item2>
<Item1>
<Value>C</Value>
<Value>D</Value>
</Item1>
<_>
<Value>E</Value>
</_>
</Items>
<After>2</After>
</Result>
// io.Reader --> map[string]interface{} or JSON string
// nothing magic - just implements generic Go case
package x2j
import (
"encoding/json"
"encoding/xml"
"io"
)
// ToTree() - parse a XML io.Reader to a tree of Nodes
func ToTree(rdr io.Reader) (*Node, error) {
p := xml.NewDecoder(rdr)
p.CharsetReader = X2jCharsetReader
n, perr := xmlToTree("", nil, p)
if perr != nil {
return nil, perr
}
return n, nil
}
// ToMap() - parse a XML io.Reader to a map[string]interface{}
func ToMap(rdr io.Reader, recast ...bool) (map[string]interface{}, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
n, err := ToTree(rdr)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
m[n.key] = n.treeToMap(r)
return m, nil
}
// ToJson() - parse a XML io.Reader to a JSON string
func ToJson(rdr io.Reader, recast ...bool) (string, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
m, merr := ToMap(rdr, r)
if m == nil || merr != nil {
return "", merr
}
b, berr := json.Marshal(m)
if berr != nil {
return "", berr
}
return string(b), nil
}
// ToJsonIndent - the pretty form of ReaderToJson
func ToJsonIndent(rdr io.Reader, recast ...bool) (string, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
m, merr := ToMap(rdr, r)
if m == nil || merr != nil {
return "", merr
}
b, berr := json.MarshalIndent(m, "", " ")
if berr != nil {
return "", berr
}
// NOTE: don't have to worry about safe JSON marshaling with json.Marshal, since '<' and '>" are reservedin XML.
return string(b), nil
}
// ReaderValuesFromTagPath - io.Reader version of ValuesFromTagPath()
func ReaderValuesFromTagPath(rdr io.Reader, path string, getAttrs ...bool) ([]interface{}, error) {
var a bool
if len(getAttrs) == 1 {
a = getAttrs[0]
}
m, err := ToMap(rdr)
if err != nil {
return nil, err
}
return ValuesFromKeyPath(m, path, a), nil
}
// ReaderValuesForTag - io.Reader version of ValuesForTag()
func ReaderValuesForTag(rdr io.Reader, tag string) ([]interface{}, error) {
m, err := ToMap(rdr)
if err != nil {
return nil, err
}
return ValuesForKey(m, tag), nil
}
<msg mtype="alert" mpriority="1">
<text>help me!</text>
<song title="A Long Time" author="Mayer Hawthorne">
<verses>
<verse name="verse 1" no="1">
<line no="1">Henry was a renegade</line>
<line no="2">Didn't like to play it safe</line>
<line no="3">One component at a time</line>
<line no="4">There's got to be a better way</line>
<line no="5">Oh, people came from miles around</line>
<line no="6">Searching for a steady job</line>
<line no="7">Welcome to the Motor Town</line>
<line no="8">Booming like an atom bomb</line>
</verse>
<verse name="verse 2" no="2">
<line no="1">Oh, Henry was the end of the story</line>
<line no="2">Then everything went wrong</line>
<line no="3">And we'll return it to its former glory</line>
<line no="4">But it just takes so long</line>
</verse>
</verses>
<chorus>
<line no="1">It's going to take a long time</line>
<line no="2">It's going to take it, but we'll make it one day</line>
<line no="3">It's going to take a long time</line>
<line no="4">It's going to take it, but we'll make it one day</line>
</chorus>
</song>
</msg>
此差异已折叠。
// Copyright 2012-2013 Charles Banning. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
// x2j_bulk.go: Process files with multiple XML messages.
// Extends x2m_bulk.go to work with JSON strings rather than map[string]interface{}.
package x2j
import (
"bytes"
"encoding/json"
"io"
"os"
"regexp"
)
// XmlMsgsFromFileAsJson()
// 'fname' is name of file
// 'phandler' is the JSON string processing handler. Return of 'false' stops further processing.
// 'ehandler' is the parsing error handler. Return of 'false' stops further processing and returns error.
// Note: phandler() and ehandler() calls are blocking, so reading and processing of messages is serialized.
// This means that you can stop reading the file on error or after processing a particular message.
// To have reading and handling run concurrently, pass arguments to a go routine in handler and return true.
func XmlMsgsFromFileAsJson(fname string, phandler func(string)(bool), ehandler func(error)(bool), recast ...bool) error {
var r bool
if len(recast) == 1 {
r = recast[0]
}
fi, fierr := os.Stat(fname)
if fierr != nil {
return fierr
}
fh, fherr := os.Open(fname)
if fherr != nil {
return fherr
}
defer fh.Close()
buf := make([]byte,fi.Size())
_, rerr := fh.Read(buf)
if rerr != nil {
return rerr
}
doc := string(buf)
// xml.Decoder doesn't properly handle whitespace in some doc
// see songTextString.xml test case ...
reg,_ := regexp.Compile("[ \t\n\r]*<")
doc = reg.ReplaceAllString(doc,"<")
b := bytes.NewBufferString(doc)
for {
s, serr := XmlBufferToJson(b,r)
if serr != nil && serr != io.EOF {
if ok := ehandler(serr); !ok {
// caused reader termination
return serr
}
}
if s != "" {
if ok := phandler(s); !ok {
break
}
}
if serr == io.EOF {
break
}
}
return nil
}
// XmlBufferToJson - process XML message from a bytes.Buffer
// 'b' is the buffer
// Optional argument 'recast' coerces values to float64 or bool where possible.
func XmlBufferToJson(b *bytes.Buffer,recast ...bool) (string,error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}
n,err := XmlBufferToTree(b)
if err != nil {
return "", err
}
m := make(map[string]interface{})
m[n.key] = n.treeToMap(r)
j, jerr := json.Marshal(m)
return string(j), jerr
}
// ============================= io.Reader version for stream processing ======================
// XmlMsgsFromReaderAsJson() - io.Reader version of XmlMsgsFromFileAsJson
// 'rdr' is an io.Reader for an XML message (stream)
// 'phandler' is the JSON string processing handler. Return of 'false' stops further processing.
// 'ehandler' is the parsing error handler. Return of 'false' stops further processing and returns error.
// Note: phandler() and ehandler() calls are blocking, so reading and processing of messages is serialized.
// This means that you can stop reading the file on error or after processing a particular message.
// To have reading and handling run concurrently, pass arguments to a go routine in handler and return true.
func XmlMsgsFromReaderAsJson(rdr io.Reader, phandler func(string)(bool), ehandler func(error)(bool), recast ...bool) error {
var r bool
if len(recast) == 1 {
r = recast[0]
}
for {
s, serr := ToJson(rdr,r)
if serr != nil && serr != io.EOF {
if ok := ehandler(serr); !ok {
// caused reader termination
return serr
}
}
if s != "" {
if ok := phandler(s); !ok {
break
}
}
if serr == io.EOF {
break
}
}
return nil
}
<msg mtype="alert" mpriority="1">
<text>help me!</text>
<song title="A Long Time" author="Mayer Hawthorne">
<verses>
<verse name="verse 1" no="1">
<line no="1">Henry was a renegade</line>
<line no="2">Didn't like to play it safe</line>
<line no="3">One component at a time</line>
<line no="4">There's got to be a better way</line>
<line no="5">Oh, people came from miles around</line>
<line no="6">Searching for a steady job</line>
<line no="7">Welcome to the Motor Town</line>
<line no="8">Booming like an atom bomb</line>
</verse>
<verse name="verse 2" no="2">
<line no="1">Oh, Henry was the end of the story</line>
<line no="2">Then everything went wrong</line>
<line no="3">And we'll return it to its former glory</line>
<line no="4">But it just takes so long</line>
</verse>
</verses>
<chorus>
<line no="1">It's going to take a long time</line>
<line no="2">It's going to take it, but we'll make it one day</line>
<line no="3">It's going to take a long time</line>
<line no="4">It's going to take it, but we'll make it one day</line>
</chorus>
</song>
</msg>
// Copyright 2012-2013 Charles Banning. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
// x2j_valuesFrom.go: Extract values from an arbitrary XML doc. Tag path can include wildcard characters.
package x2j
import (
"strings"
)
// ------------------- sweep up everything for some point in the node tree ---------------------
// ValuesFromTagPath - deliver all values for a path node from a XML doc
// If there are no values for the path 'nil' is returned.
// A return value of (nil, nil) means that there were no values and no errors parsing the doc.
// 'doc' is the XML document
// 'path' is a dot-separated path of tag nodes
// 'getAttrs' can be set 'true' to return attribute values for "*"-terminated path
// If a node is '*', then everything beyond is scanned for values.
// E.g., "doc.books' might return a single value 'book' of type []interface{}, but
// "doc.books.*" could return all the 'book' entries as []map[string]interface{}.
// "doc.books.*.author" might return all the 'author' tag values as []string - or
// "doc.books.*.author.lastname" might be required, depending on he schema.
func ValuesFromTagPath(doc, path string, getAttrs ...bool) ([]interface{}, error) {
var a bool
if len(getAttrs) == 1 {
a = getAttrs[0]
}
m, err := DocToMap(doc)
if err != nil {
return nil, err
}
v := ValuesFromKeyPath(m, path, a)
return v, nil
}
// ValuesFromKeyPath - deliver all values for a path node from a map[string]interface{}
// If there are no values for the path 'nil' is returned.
// 'm' is the map to be walked
// 'path' is a dot-separated path of key values
// 'getAttrs' can be set 'true' to return attribute values for "*"-terminated path
// If a node is '*', then everything beyond is walked.
// E.g., see ValuesFromTagPath documentation.
func ValuesFromKeyPath(m map[string]interface{}, path string, getAttrs ...bool) []interface{} {
var a bool
if len(getAttrs) == 1 {
a = getAttrs[0]
}
keys := strings.Split(path, ".")
ret := make([]interface{}, 0)
valuesFromKeyPath(&ret, m, keys, a)
if len(ret) == 0 {
return nil
}
return ret
}
func valuesFromKeyPath(ret *[]interface{}, m interface{}, keys []string, getAttrs bool) {
lenKeys := len(keys)
// load 'm' values into 'ret'
// expand any lists
if lenKeys == 0 {
switch m.(type) {
case map[string]interface{}:
*ret = append(*ret, m)
case []interface{}:
for _, v := range m.([]interface{}) {
*ret = append(*ret, v)
}
default:
*ret = append(*ret, m)
}
return
}
// key of interest
key := keys[0]
switch key {
case "*": // wildcard - scan all values
switch m.(type) {
case map[string]interface{}:
for k, v := range m.(map[string]interface{}) {
if string(k[:1]) == "-" && !getAttrs { // skip attributes?
continue
}
valuesFromKeyPath(ret, v, keys[1:], getAttrs)
}
case []interface{}:
for _, v := range m.([]interface{}) {
switch v.(type) {
// flatten out a list of maps - keys are processed
case map[string]interface{}:
for kk, vv := range v.(map[string]interface{}) {
if string(kk[:1]) == "-" && !getAttrs { // skip attributes?
continue
}
valuesFromKeyPath(ret, vv, keys[1:], getAttrs)
}
default:
valuesFromKeyPath(ret, v, keys[1:], getAttrs)
}
}
}
default: // key - must be map[string]interface{}
switch m.(type) {
case map[string]interface{}:
if v, ok := m.(map[string]interface{})[key]; ok {
valuesFromKeyPath(ret, v, keys[1:], getAttrs)
}
case []interface{}: // may be buried in list
for _, v := range m.([]interface{}) {
switch v.(type) {
case map[string]interface{}:
if vv, ok := v.(map[string]interface{})[key]; ok {
valuesFromKeyPath(ret, vv, keys[1:], getAttrs)
}
}
}
}
}
}
此差异已折叠。
<msg mtype="alert" mpriority="1">
<text>help me!</text>
<song title="A Long Time" author="Mayer Hawthorne">
<verses>
<verse name="verse 1" no="1">
<line no="1">Henry was a renegade</line>
<line no="2">Didn't like to play it safe</line>
<line no="3">One component at a time</line>
<line no="4">There's got to be a better way</line>
<line no="5">Oh, people came from miles around</line>
<line no="6">Searching for a steady job</line>
<line no="7">Welcome to the Motor Town</line>
<line no="8">Booming like an atom bomb</line>
</verse>
<verse name="verse 2" no="2">
<line no="1">Oh, Henry was the end of the story</line>
<line no="2">Then everything went wrong</line>
<line no="3">And we'll return it to its former glory</line>
<line no="4">But it just takes so long</line>
</verse>
</verses>
<chorus>
<line no="1">It's going to take a long time</line>
<line no="2">It's going to take it, but we'll make it one day</line>
<line no="3">It's going to take a long time</line>
<line no="4">It's going to take it, but we'll make it one day</line>
</chorus>
</song>
</msg>
<msg mtype="alert" mpriority="1">
<text>help me!</text>
<song title="A Long Time" author="Mayer Hawthorne">
<verses>
<verse name="verse 1" no="1">
<line no="1">Henry was a renegade</line>
<line no="2">Didn't like to play it safe</line>
<line no="3">One component at a time</line>
<line no="4">There's got to be a better way</line>
<line no="5">Oh, people came from miles around</line>
<line no="6">Searching for a steady job</line>
<line no="7">Welcome to the Motor Town</line>
<line no="8">Booming like an atom bomb</line>
</verse>
</verses>
</song>
</msg>
<msg mtype="alert" mpriority="1">
<text>help me!</text>
<song title="A Long Time" author="Mayer Hawthorne">
<chorus>
<line no="1">It's going to take a long time</line>
<line no="2">It's going to take it, but we'll make it one day</line>
<line no="3">It's going to take a long time</line>
<line no="4">It's going to take it, but we'll make it one day</line>
</chorus>
</song>
</msg>
<msg mtype="alert" mpriority="1">
<text>help me!</text>
<song title="A Long Time" author="Mayer Hawthorne">
<chorus>
<line no="1">It's going to take a long time</line>
<line no="2">It's going to take it, but we'll make it one day</line>
<line no="3">It's going to take a long time</line>
<line no="4">It's going to take it, but we'll make it one day</line>
</song>
</msg>
Copyright (c) 2013 Belogik. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Belogik nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
help:
@echo "Available targets:"
@echo "- test: run tests"
@echo "- installdependencies: installs dependencies declared in dependencies.txt"
installdependencies:
cat dependencies.txt | xargs go get
test: installdependencies
go test -i && go test
There is a new maintener for this library.
Please go here : https://github.com/OwnLocal/goes
!!!!! By using this repo you are running on thin ice !!!!!!
https://github.com/belogik/goes/issues/40 might be of interest for you.
- Add Gzip support to bulk data to save bandwith
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
// Copyright 2018 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package effect
// Effect is the result for a policy rule.
type Effect int
// Values for policy effect.
const (
Allow Effect = iota
Indeterminate
Deny
)
// Effector is the interface for Casbin effectors.
type Effector interface {
// MergeEffects merges all matching results collected by the enforcer into a single decision.
MergeEffects(expr string, effects []Effect, results []float64) (bool, error)
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
golz4
=====
Golang interface to LZ4 compression
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册