gs-predicate/README.md
Pat Garrity 48c773f449
All checks were successful
/ Build and Release Library (push) Successful in 2m28s
(patch) Adding builds and minor documentation. (#2)
Reviewed-on: #2
2025-11-27 20:41:42 +00:00

2.2 KiB

gs-predicate

GS Open Source | License (MIT)

Serializable predicates for Scala 3. Circe is used for JSON representations.

Usage

Dependency

This artifact is available in the Garrity Software Maven repository.

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".
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? Consider Donating to Pat on Ko-fi.