{"id":13412734,"url":"https://github.com/PaesslerAG/gval","last_synced_at":"2025-03-14T18:32:03.294Z","repository":{"id":41390314,"uuid":"104993127","full_name":"PaesslerAG/gval","owner":"PaesslerAG","description":"Expression evaluation in golang","archived":false,"fork":false,"pushed_at":"2024-05-30T04:33:19.000Z","size":816,"stargazers_count":731,"open_issues_count":25,"forks_count":82,"subscribers_count":25,"default_branch":"master","last_synced_at":"2024-10-01T08:41:21.383Z","etag":null,"topics":["evaluate-expressions","evaluation","expression-evaluator","expression-language","go","godoc","golang","gval","parser","parsing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PaesslerAG.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-27T08:32:49.000Z","updated_at":"2024-09-25T14:11:25.000Z","dependencies_parsed_at":"2024-10-26T08:36:29.780Z","dependency_job_id":null,"html_url":"https://github.com/PaesslerAG/gval","commit_stats":{"total_commits":109,"total_committers":22,"mean_commits":4.954545454545454,"dds":"0.39449541284403666","last_synced_commit":"cd026a3dee26df39acddc2292229512c4fa68756"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaesslerAG%2Fgval","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaesslerAG%2Fgval/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaesslerAG%2Fgval/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaesslerAG%2Fgval/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PaesslerAG","download_url":"https://codeload.github.com/PaesslerAG/gval/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243625161,"owners_count":20321248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["evaluate-expressions","evaluation","expression-evaluator","expression-language","go","godoc","golang","gval","parser","parsing"],"created_at":"2024-07-30T20:01:28.492Z","updated_at":"2025-03-14T18:32:02.997Z","avatar_url":"https://github.com/PaesslerAG.png","language":"Go","funding_links":[],"categories":["开源类库","Embeddable Scripting Languages","Open source library","Go","可嵌入的脚本语言","脚本语言与嵌入式编程`在你的go代码中嵌入其他脚本语言`","脚本语言与嵌入式编程","Relational Databases"],"sub_categories":["解释器","Search and Analytic Databases","Advanced Console UIs","Interpreter","检索及分析资料库","SQL 查询语句构建库"],"readme":"# Gval\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/PaesslerAG/gval.svg)](https://pkg.go.dev/github.com/PaesslerAG/gval)\n[![Build Status](https://api.travis-ci.org/PaesslerAG/gval.svg?branch=master)](https://travis-ci.org/PaesslerAG/gval)\n[![Coverage Status](https://coveralls.io/repos/github/PaesslerAG/gval/badge.svg?branch=master)](https://coveralls.io/github/PaesslerAG/gval?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/PaesslerAG/gval)](https://goreportcard.com/report/github.com/PaesslerAG/gval)\n\nGval (Go eVALuate) provides support for evaluating arbitrary expressions, in particular Go-like expressions.\n\n![gopher](./prtg-batmin-gopher.png)\n\n## Evaluate\n\nGval can evaluate expressions with parameters, arimethetic, logical, and string operations:\n\n- basic expression: [10 \u003e 0](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Basic)\n- parameterized expression: [foo \u003e 0](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Parameter)\n- nested parameterized expression: [foo.bar \u003e 0](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-NestedParameter)\n- arithmetic expression: [(requests_made * requests_succeeded / 100) \u003e= 90](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Arithmetic)\n- string expression: [http_response_body == \"service is ok\"](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-String)\n- float64 expression: [(mem_used / total_mem) * 100](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Float64)\n\nIt can easily be extended with custom functions or operators:\n\n- custom date comparator: [date(\\`2014-01-02\\`) \u003e date(\\`2014-01-01 23:59:59\\`)](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-DateComparison)\n- string length: [strlen(\"someReallyLongInputString\") \u003c= 16](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Strlen)\n\nYou can parse gval.Expressions once and re-use them 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:\n\n- [Parsing and Evaluation](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluable)\n\nThe normal Go-standard order of operators is respected. When writing an expression, be sure that you either order the operators correctly, or use parentheses to clarify which portions of an expression should be run first.\n\nStrings, numbers, and booleans can be used like in Go:\n\n- [(7 \u003c \"47\" == true ? \"hello world!\\n\\u263a\") + \\` more text\\`](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Encoding)\n\n## Parameter\n\nVariables can be accessed via string literals. They can be used for values with string keys if the parameter is a `map[string]interface{}` or `map[interface{}]interface{}` and for fields or methods if the parameter is a struct.\n\n- [foo \u003e 0](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Parameter)\n\n### Bracket Selector\n\nMap and array elements and Struct Field can be accessed via `[]`.\n\n- [foo[0]](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Array)\n- [foo[\"b\" + \"a\" + \"r\"]](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-ExampleEvaluate_ComplexAccessor)\n\n### Dot Selector\n\nA nested variable with a name containing only letters and underscores can be accessed via a dot selector.\n\n- [foo.bar \u003e 0](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-NestedParameter)\n\n### Custom Selector\n\nParameter names like `response-time` will be interpreted as `response` minus `time`. While gval doesn't support these parameter names directly, you can easily access them via a custom extension like [JSON Path](https://github.com/PaesslerAG/jsonpath):\n\n- [$[\"response-time\"]](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Jsonpath)\n\nJsonpath is also suitable for accessing array elements.\n\n### Fields and Methods\n\nIf you have structs in your parameters, you can access their fields and methods in the usual way:\n\n- [foo.Hello + foo.World()](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-FlatAccessor)\n\nIt also works if the parameter is a struct directly\n[Hello + World()](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-Accessor)\nor if the fields are nested\n[foo.Hello + foo.World()](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Evaluate-NestedAccessor)\n\nThis may be convenient but note that using accessors on strucs makes the expression about four times slower than just using a parameter (consult the benchmarks for more precise measurements on your system). If there are functions you want to use, it's faster (and probably cleaner) to define them as functions (see the Evaluate section). These approaches use no reflection, and are designed to be fast and clean.\n\n## Default Language\n\nThe default language is in serveral sub languages like text, arithmetic or propositional logic defined. See [Godoc](https://pkg.go.dev/github.com/PaesslerAG/gval/#Gval) for details. All sub languages are merged into gval.Full which contains the following elements:\n\n- Modifiers: `+` `-` `/` `*` `\u0026` `|` `^` `**` `%` `\u003e\u003e` `\u003c\u003c`\n- Comparators: `\u003e` `\u003e=` `\u003c` `\u003c=` `==` `!=` `=~` `!~`\n- Logical ops: `||` `\u0026\u0026`\n- Numeric constants, as 64-bit floating point (`12345.678`)\n- String constants (double quotes: `\"foobar\"`)\n- Date function 'Date(x)', using any permutation of RFC3339, ISO8601, ruby date, or unix date\n- Boolean constants: `true` `false`\n- Parentheses to control order of evaluation `(` `)`\n- Json Arrays : `[1, 2, \"foo\"]`\n- Json Objects : `{\"a\":1, \"b\":2, \"c\":\"foo\"}`\n- Prefixes: `!` `-` `~`\n- Ternary conditional: `?` `:`\n- Null coalescence: `??`\n\n## Customize\n\nGval is completly customizable. Every constant, function or operator can be defined separately and existing expression languages can be reused:\n\n- [foo.Hello + foo.World()](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-Language)\n\nFor details see [Godoc](https://pkg.go.dev/github.com/PaesslerAG/gval).\n\n### Implementing custom selector\n\nIn a case you want to provide custom logic for selectors you can implement `SelectGVal(ctx context.Context, k string) (interface{}, error)` on your struct.\nFunction receives next part of the path and can return any type of var that is again evaluated through standard gval procedures.\n\n[Example Custom Selector](https://pkg.go.dev/github.com/PaesslerAG/gval/#example-custom-selector)\n\n### External gval Languages\n\nA list of external libraries for gval. Feel free to add your own library.\n\n- [gvalstrings](https://github.com/generikvault/gvalstrings) parse single quoted strings in gval.\n- [jsonpath](https://github.com/PaesslerAG/jsonpath) full support for jsonpath in gval.\n\n## Performance\n\nThe library is built with the intention of being quick but has not been aggressively profiled and optimized. For most applications, though, it is completely fine.\nIf performance is an issue, make sure to create your expression language with all functions, constants and operators only once. Evaluating an expression like gval.Evaluate(\"expression, const1, func1, func2, ...) creates a new gval.Language everytime it is called and slows execution.\n\nThe library comes with a bunch of benchmarks to measure the performance of parsing and evaluating expressions. You can run them with `go test -bench=.`.\n\nFor a very rough idea of performance, here are the results from a benchmark run on a Dell Latitude E7470 Win 10 i5-6300U.\n\n``` text\nBenchmarkGval/const_evaluation-4                               500000000                 3.57 ns/op\nBenchmarkGval/const_parsing-4                                    1000000              1144 ns/op\nBenchmarkGval/single_parameter_evaluation-4                     10000000               165 ns/op\nBenchmarkGval/single_parameter_parsing-4                         1000000              1648 ns/op\nBenchmarkGval/parameter_evaluation-4                             5000000               352 ns/op\nBenchmarkGval/parameter_parsing-4                                 500000              2773 ns/op\nBenchmarkGval/common_evaluation-4                                3000000               434 ns/op\nBenchmarkGval/common_parsing-4                                    300000              4419 ns/op\nBenchmarkGval/complex_evaluation-4                             100000000                11.6 ns/op\nBenchmarkGval/complex_parsing-4                                   100000             17936 ns/op\nBenchmarkGval/literal_evaluation-4                             300000000                 3.84 ns/op\nBenchmarkGval/literal_parsing-4                                   500000              2559 ns/op\nBenchmarkGval/modifier_evaluation-4                            500000000                 3.54 ns/op\nBenchmarkGval/modifier_parsing-4                                  500000              3755 ns/op\nBenchmarkGval/regex_evaluation-4                                   50000             21347 ns/op\nBenchmarkGval/regex_parsing-4                                     200000              6480 ns/op\nBenchmarkGval/constant_regex_evaluation-4                        1000000              1000 ns/op\nBenchmarkGval/constant_regex_parsing-4                            200000              9417 ns/op\nBenchmarkGval/accessors_evaluation-4                             3000000               417 ns/op\nBenchmarkGval/accessors_parsing-4                                1000000              1778 ns/op\nBenchmarkGval/accessors_method_evaluation-4                      1000000              1931 ns/op\nBenchmarkGval/accessors_method_parsing-4                         1000000              1729 ns/op\nBenchmarkGval/accessors_method_parameter_evaluation-4            1000000              2162 ns/op\nBenchmarkGval/accessors_method_parameter_parsing-4                500000              2618 ns/op\nBenchmarkGval/nested_accessors_evaluation-4                      2000000               681 ns/op\nBenchmarkGval/nested_accessors_parsing-4                         1000000              2115 ns/op\nBenchmarkRandom-4                                                 500000              3631 ns/op\nok\n```\n\n## API Breaks\n\nGval is designed with easy expandability in mind and API breaks will be avoided if possible. If API breaks are unavoidable they wil be explicitly stated via an increased major version number.\n\n-------------------------------------\nCredits to Reene French for the gophers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPaesslerAG%2Fgval","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPaesslerAG%2Fgval","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPaesslerAG%2Fgval/lists"}