Module: Ruby::Rego::Builtins::JsonPaths
- Extended by:
- RegistryHelpers
- Defined in:
- lib/ruby/rego/builtins/json_paths.rb
Overview
JSON path projection/redaction helpers (json.filter, json.remove), matching OPA.
Both take an object document and an array or set of paths; each path is a
“/”-separated string (JSON-pointer escaped: ~1 is /, ~0 is ~) or an array of
literal segments. A leading run of slashes in a string path is stripped before
splitting (so /a/b and a/b are equivalent); the empty string is an empty path. A
numeric string segment indexes into an array. A non-object document, a paths
argument that is neither an array nor a set, or a path element that is neither a
string nor an array yields undefined. Operations are pure structural rewrites of the
parsed value (linear in the document size), so there is no unbounded cost.
json.filter keeps only the listed paths (a terminal path keeps the whole subtree; a path that descends past a scalar keeps the scalar; a non-matching child becomes an empty container). json.remove drops the listed paths (removing an array element reindexes; multiple indices under one array are removed against the original positions).
Constant Summary collapse
- JSON_PATH_FUNCTIONS =
{ "json.filter" => { arity: 2, handler: :filter }, "json.remove" => { arity: 2, handler: :remove } }.freeze
- ARRAY_INDEX =
A canonical non-negative array index (no leading zeros), matching how OPA renders an index back to a path segment.
/\A(?:0|[1-9]\d*)\z/
Class Method Summary collapse
-
.filter(document_value, paths_value) ⇒ Hash
-
.register! ⇒ Ruby::Rego::Builtins::BuiltinRegistry
-
.remove(document_value, paths_value) ⇒ Hash
Methods included from RegistryHelpers
Class Method Details
.filter(document_value, paths_value) ⇒ Hash
50 51 52 |
# File 'lib/ruby/rego/builtins/json_paths.rb', line 50 def self.filter(document_value, paths_value) select(object_document(document_value, "json.filter"), path_trie(paths_value, "json.filter")) end |
.register! ⇒ Ruby::Rego::Builtins::BuiltinRegistry
39 40 41 42 43 |
# File 'lib/ruby/rego/builtins/json_paths.rb', line 39 def self.register! registry = BuiltinRegistry.instance register_configured_functions(registry, JSON_PATH_FUNCTIONS) registry end |
.remove(document_value, paths_value) ⇒ Hash
57 58 59 |
# File 'lib/ruby/rego/builtins/json_paths.rb', line 57 def self.remove(document_value, paths_value) prune(object_document(document_value, "json.remove"), path_trie(paths_value, "json.remove")) end |