116 lines
2.2 KiB
Markdown
116 lines
2.2 KiB
Markdown
# gs-predicate
|
|
|
|
[GS Open Source](https://garrity.co/open-source.html) |
|
|
[License (MIT)](./LICENSE)
|
|
|
|
Serializable predicates for Scala 3. [Circe](https://github.com/circe/circe) is
|
|
used for JSON representations.
|
|
|
|
- [Usage](#usage)
|
|
- [Dependency](#dependency)
|
|
- [JSON Predicates](#json-predicates)
|
|
- [List of Predicates](#list-of-predicates)
|
|
- [Donate](#donate)
|
|
|
|
## Usage
|
|
|
|
### Dependency
|
|
|
|
This artifact is available in the Garrity Software Maven repository.
|
|
|
|
```scala
|
|
externalResolvers +=
|
|
"Garrity Software Releases" at "https://maven.garrity.co/gs"
|
|
|
|
val GsPredicate: ModuleID =
|
|
"gs" %% "gs-predicate-api-v0" % "$VERSION"
|
|
```
|
|
|
|
### JSON Predicates
|
|
|
|
The following example evaluates whether some JSON object:
|
|
|
|
- Contains an object `foo`...
|
|
- That contains an array `bar` such that any one element of `bar`...
|
|
- Contains an array `baz` such that all elements of `baz`...
|
|
- Are objects that contain some property `xyz` with value `"example"`.
|
|
|
|
```scala
|
|
import io.circe.Json
|
|
import gs.predicate.v0.api.*
|
|
import gs.predicate.v0.json.*
|
|
import gs.predicate.v0.json.query.JsonQuery
|
|
|
|
val predicate = JsonComparisonPredicate(
|
|
JsonQuery.compile("foo.bar[any].baz[all].xyz"),
|
|
JsonComparison.Eq(Json.fromString("example"))
|
|
)
|
|
|
|
val json = io.circe.parser.parse("""
|
|
{
|
|
"foo": {
|
|
"bar": [
|
|
{
|
|
"baz": [
|
|
{ "xyz": "example" },
|
|
{ "xyz": "example" }
|
|
]
|
|
},
|
|
{
|
|
"baz": [
|
|
{ "xyz": "oops" },
|
|
{ "xyz": "example" }
|
|
]
|
|
}
|
|
]
|
|
}
|
|
}
|
|
""".stripMargin)
|
|
|
|
assertEquals(predicate.eval(json), Predicate.Result.matched())
|
|
```
|
|
|
|
## List of Predicates
|
|
|
|
- `true`
|
|
- `false`
|
|
- `and`
|
|
- `or`
|
|
- string comparison
|
|
- json comparison
|
|
|
|
### List of Comparison Operations
|
|
|
|
- `true`
|
|
- `false`
|
|
- `and`
|
|
- `or`
|
|
- `eq`
|
|
- `neq`
|
|
|
|
#### For Strings
|
|
|
|
- `contains`
|
|
- `prefix`
|
|
- `suffix`
|
|
|
|
#### When Parsed as Integer
|
|
|
|
- `<`
|
|
- `<=`
|
|
- `>`
|
|
- `>=`
|
|
- Inclusive Range
|
|
- Exclusive Range
|
|
|
|
#### When Parsed as Date
|
|
|
|
- `before`
|
|
- `after`
|
|
- Inclusive Range
|
|
- Exclusive Range
|
|
|
|
## Donate
|
|
|
|
Enjoy this project or want to help me achieve my [goals](https://garrity.co)?
|
|
Consider [Donating to Pat on Ko-fi](https://ko-fi.com/gspfm).
|