|
1 | | -# ksql |
2 | | -a JSON data expression lexer, parser, cli and library |
| 1 | +ksql |
| 2 | +===== |
| 3 | + |
| 4 | +[](https://pkg.go.dev/github.com/go-playground/ksql) |
| 5 | + |
| 6 | + |
| 7 | +**Is a JSON data expression lexer, parser, cli and library.** |
| 8 | + |
| 9 | +#### Requirements |
| 10 | +- Go 1.18+ |
| 11 | + |
| 12 | +#### How to install CLI |
| 13 | +```shell |
| 14 | +~ go install github.com/go-playground/ksql |
| 15 | +``` |
| 16 | + |
| 17 | +#### Usage |
| 18 | +```go |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/go-playground/ksql" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + expression := []byte(`.properties.employees > 20`) |
| 29 | + input := []byte(`{"name":"MyCompany", "properties":{"employees": 50}`) |
| 30 | + ex, err := ksql.Parse(expression) |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | + |
| 35 | + result, err := ex.Calculate(input) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + fmt.Printf("%v\n", result) |
| 40 | +} |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +#### Expressions |
| 45 | +Expressions support most mathematical and string expressions see below for details: |
| 46 | + |
| 47 | +#### Syntax & Rules |
| 48 | + |
| 49 | +| Token | Example | Syntax Rules | |
| 50 | +|----------------|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 51 | +| `Equals` | `==` | supports both `==` and `=`. | |
| 52 | +| `Add` | `+` | N/A | |
| 53 | +| `Subtract` | `-` | N/A | |
| 54 | +| `Multiply` | `*` | N/A | |
| 55 | +| `Divide` | `/` | N/A | |
| 56 | +| `Gt` | `>` | N/A | |
| 57 | +| `Gte` | `>=` | N/A | |
| 58 | +| `Lt` | `<` | N/A | |
| 59 | +| `Lte` | `<=` | N/A | |
| 60 | +| `OpenParen` | `(` | N/A | |
| 61 | +| `CloseParen` | `)` | N/A | |
| 62 | +| `OpenBracket` | `[` | N/A | |
| 63 | +| `CloseBracket` | `]` | N/A | |
| 64 | +| `Comma` | `,` | N/A | |
| 65 | +| `QuotedString` | `"sample text"` | Must start and end with an unescaped `"` character | |
| 66 | +| `Number` | `123.45` | Must start and end with a valid `0-9` digit. | |
| 67 | +| `Boolen` | `true` | Accepts `true` or `false` as a boolean only. | |
| 68 | +| `Identifier` | `.identifier` | Starts with a `.` and ends with whitespace blank space. This crate currently uses [gjson](https://github.com/tidwall/gjson.rs) and so the full gjson syntax for identifiers is supported. | |
| 69 | +| `And` | `&&` | N/A | |
| 70 | +| `Not` | `!` | Must be before Boolean identifier or expression or be followed by an operation | |
| 71 | +| `Or` | <code>||<code> | N/A | |
| 72 | +| `Contains` | `CONTAINS ` | Ends with whitespace blank space. | |
| 73 | +| `In` | `IN ` | Ends with whitespace blank space. | |
| 74 | +| `StartsWith` | `STARTSWITH ` | Ends with whitespace blank space. | |
| 75 | +| `EndsWith` | `ENDSWITH ` | Ends with whitespace blank space. | |
| 76 | +| `NULL` | `NULL ` | N/A | |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +``` |
| 81 | +
|
| 82 | +#### License |
| 83 | +
|
| 84 | +<sup> |
| 85 | +Licensed under either of <a href="LICENSE-APACHE">Apache License, Version |
| 86 | +2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. |
| 87 | +</sup> |
| 88 | +
|
| 89 | +<br> |
| 90 | +
|
| 91 | +<sub> |
| 92 | +Unless you explicitly state otherwise, any contribution intentionally submitted |
| 93 | +for inclusion in Proteus by you, as defined in the Apache-2.0 license, shall be |
| 94 | +dual licensed as above, without any additional terms or conditions. |
| 95 | +</sub> |
0 commit comments