### Evaluate Basic Expression in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates evaluating a simple boolean expression using Gval. This is a fundamental example of how to use the library to check conditions. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { result, err := gval.Evaluate("10 > 0") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Evaluate String Length Function in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates using a custom function 'strlen' to get the length of a string and use it in a comparison within an expression. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" "strings" ) func main() { customFuncs := map[string]gval.Func{ "strlen": func(args ...interface{}) (interface{}, error) { return strings.Count(args[0].(string), ""), nil }, } result, err := gval.Evaluate("strlen(\"someReallyLongInputString\") <= 16", nil, gval.WithFunctions(customFuncs)) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### gval Benchmark Results Source: https://github.com/paesslerag/gval/blob/master/README.md A detailed breakdown of performance metrics for different gval operations, including evaluation and parsing times for constants, parameters, complex expressions, literals, modifiers, regex, and accessors. Results are presented in nanoseconds per operation (ns/op). ```text BenchmarkGval/const_evaluation-4 500000000 3.57 ns/op BenchmarkGval/const_parsing-4 1000000 1144 ns/op BenchmarkGval/single_parameter_evaluation-4 10000000 165 ns/op BenchmarkGval/single_parameter_parsing-4 1000000 1648 ns/op BenchmarkGval/parameter_evaluation-4 5000000 352 ns/op BenchmarkGval/parameter_parsing-4 500000 2773 ns/op BenchmarkGval/common_evaluation-4 3000000 434 ns/op BenchmarkGval/common_parsing-4 300000 4419 ns/op BenchmarkGval/complex_evaluation-4 100000000 11.6 ns/op BenchmarkGval/complex_parsing-4 100000 17936 ns/op BenchmarkGval/literal_evaluation-4 300000000 3.84 ns/op BenchmarkGval/literal_parsing-4 500000 2559 ns/op BenchmarkGval/modifier_evaluation-4 500000000 3.54 ns/op BenchmarkGval/modifier_parsing-4 500000 3755 ns/op BenchmarkGval/regex_evaluation-4 50000 21347 ns/op BenchmarkGval/regex_parsing-4 200000 6480 ns/op BenchmarkGval/constant_regex_evaluation-4 1000000 1000 ns/op BenchmarkGval/constant_regex_parsing-4 200000 9417 ns/op BenchmarkGval/accessors_evaluation-4 3000000 417 ns/op BenchmarkGval/accessors_parsing-4 1000000 1778 ns/op BenchmarkGval/accessors_method_evaluation-4 1000000 1931 ns/op BenchmarkGval/accessors_method_parsing-4 1000000 1729 ns/op BenchmarkGval/accessors_method_parameter_evaluation-4 1000000 2162 ns/op BenchmarkGval/accessors_method_parameter_parsing-4 500000 2618 ns/op BenchmarkGval/nested_accessors_evaluation-4 2000000 681 ns/op BenchmarkGval/nested_accessors_parsing-4 1000000 2115 ns/op BenchmarkRandom-4 500000 3631 ns/op ok ``` -------------------------------- ### Go Benchmarks for gval Source: https://github.com/paesslerag/gval/blob/master/README.md This snippet displays the results of benchmarks run on the gval library using Go's testing framework. It measures the performance of constant evaluation, parsing, and various complex expression types. ```go go test -bench=. ``` -------------------------------- ### Implement Custom Selector in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates how to implement a custom selector using the `SelectGVal` method to provide custom logic for selectors within gval expressions. This allows for dynamic evaluation of paths based on the provided context and key. ```Go type CustomStruct struct {} func (c *CustomStruct) SelectGVal(ctx context.Context, k string) (interface{}, error) { // Implement custom logic here based on the key 'k' return nil, nil } ``` -------------------------------- ### Evaluate Parameterized Expression in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Shows how to evaluate an expression with a parameter. This allows dynamic values to be used in expressions, making them more flexible. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "foo": 10, } result, err := gval.Evaluate("foo > 0", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Evaluate Float64 Expression in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates evaluating an expression involving floating-point numbers for calculations like percentages. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "mem_used": 75.5, "total_mem": 100.0, } result, err := gval.Evaluate("(mem_used / total_mem) * 100", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Parse and Evaluate Expression Once in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Shows the efficient way to parse an expression once and reuse it multiple times with different parameters, optimizing performance. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { exp, err := gval.Parse("foo + bar") if err != nil { fmt.Printf("Error parsing: %v\n", err) return } params1 := map[string]interface{}{"foo": 10, "bar": 5} result1, err := exp.Evaluate(params1) if err != nil { fmt.Printf("Error evaluating 1: %v\n", err) return } fmt.Printf("Result 1: %v\n", result1) params2 := map[string]interface{}{"foo": 20, "bar": 7} result2, err := exp.Evaluate(params2) if err != nil { fmt.Printf("Error evaluating 2: %v\n", err) return } fmt.Printf("Result 2: %v\n", result2) } ``` -------------------------------- ### Evaluate String Expression in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Shows how to evaluate an expression that compares a string parameter with a literal string. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "http_response_body": "service is ok", } result, err := gval.Evaluate("http_response_body == \"service is ok\"", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Evaluate Arithmetic Expression in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates evaluating a complex arithmetic expression involving multiplication, division, and comparison. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "requests_made": 100, "requests_succeeded": 95, } result, err := gval.Evaluate("(requests_made * requests_succeeded / 100) >= 90", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Evaluate Custom Date Comparator in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Shows how to extend Gval with custom functions, specifically a date comparator for comparing dates within expressions. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" "time" ) func main() { customFuncs := map[string]gval.Func{ "date": func(args ...interface{}) (interface{}, error) { return time.Parse("2006-01-02", args[0].(string)) }, } result, err := gval.Evaluate("date(\"2014-01-02\") > date(\"2014-01-01 23:59:59\")", nil, gval.WithFunctions(customFuncs)) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Evaluate Expression with Encoding in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates evaluating a complex expression involving string concatenation, unicode characters, and conditional operators. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { result, err := gval.Evaluate("(7 < \"47\" == true ? \"hello world!\\n\u263a\") + \` more text\`") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Evaluate Nested Parameterized Expression in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Illustrates evaluating an expression with nested parameters, accessing values within a nested structure. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "foo": map[string]interface{}{ "bar": 5, }, } result, err := gval.Evaluate("foo.bar > 0", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Access Array Element via Bracket Selector in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Shows how to access elements of an array parameter using bracket notation in Gval expressions. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "foo": []interface{}{10, 20, 30}, } result, err := gval.Evaluate("foo[0]", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Access Map Element with String Concatenation in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates accessing a map element where the key is constructed by concatenating strings within the Gval expression. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) func main() { parameters := map[string]interface{}{ "foo": map[string]interface{}{ "bar": 10, }, } result, err := gval.Evaluate("foo[\"b\" + \"a\" + \"r\"]", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Access Struct Field and Method in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Shows how to access fields and call methods of struct parameters directly within Gval expressions. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) type MyStruct struct { Hello string } func (m MyStruct) World() string { return "World" } func main() { parameters := MyStruct{ Hello: "Hello", } result, err := gval.Evaluate("Hello + World()", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Access Nested Struct Field and Method in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Demonstrates accessing fields and methods of nested structs within Gval expressions for more complex data structures. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" ) type Inner struct { Hello string } func (i Inner) World() string { return "World" } type Outer struct { Inner Inner } func main() { parameters := Outer{ Inner: Inner{ Hello: "Hello", }, } result, err := gval.Evaluate("Inner.Hello + Inner.World()", parameters) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` -------------------------------- ### Access JSON Path Field in Go Source: https://github.com/paesslerag/gval/blob/master/README.md Illustrates using a custom selector, like JSON Path, to access parameters with keys that contain special characters. ```Go package main import ( "fmt" "github.com/PaesslerAG/gval" "github.com/PaesslerAG/jsonpath" ) func main() { parameters := map[string]interface{}{ "response-time": 500, } result, err := gval.Evaluate("$[\"response-time\"]", parameters, gval.WithSelectors(jsonpath.Selectors{})) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Result: %v\n", result) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.