polix.registry

Unified registry for namespace resolution in policies.

The registry maps namespace prefixes to their handlers: - :doc — document accessor (built-in) - :fn — builtin functions (built-in) - :self — self-references in let bindings - :param — policy parameters - :event — event data accessor - User-defined modules containing named policies

Example

(require '[polix.registry :as reg])

;; Create and populate a registry
(def my-registry
  (-> (reg/create-registry)
      (reg/register-module :auth
        {:policies {:admin [:= :doc/role "admin"]
                    :has-role [:= :doc/role :param/role]}})
      (reg/register-alias :a :auth)))

;; Resolve references
(reg/resolve-namespace my-registry :auth)
;; => {:type :module, :version 1, :policies {...}}

(reg/resolve-policy my-registry :auth :admin)
;; => [:= :doc/role "admin"]

AccessorEntry

Schema for built-in accessor entries.

alias-namespaces

(alias-namespaces registry)

Returns a set of all alias keys in the registry.

AliasEntry

Schema for an alias entry.

all-policies

(all-policies registry)

Returns a map of qualified policy keys to their expressions.

Example: {:auth/admin [:= :doc/role "admin"], ...}

BuiltinsEntry

Schema for the :fn builtins entry.

create-registry

(create-registry)

Creates a new registry with built-in entries.

Built-ins include: - :doc — document accessor - :fn — builtin functions - :self — self-references - :param — parameter accessor - :event — event accessor

get-global-registry

(get-global-registry)

Returns the global registry, initializing if needed.

init-global-registry!

(init-global-registry!)

Initializes the global registry. Idempotent.

IRegistry

protocol

Protocol for registry operations.

members

registry-version

(registry-version this)

Returns the current version number for cache invalidation.

resolve-namespace

(resolve-namespace this ns-key)

Returns the entry for ns-key, following aliases. Returns nil if not found.

resolve-policy

(resolve-policy this ns-key policy-key)

Returns the policy expression for ns-key/policy-key. Returns nil if the namespace is not a module or policy not found.

module-namespaces

(module-namespaces registry)

Returns a set of all module namespace keys in the registry.

ModuleEntry

Schema for a user-defined module entry.

Policies can be simple expressions or rich definitions with metadata:

;; Simple
{:policies {:admin [:= :doc/role "admin"]}}

;; Rich with defaults and descriptions
{:policies {:has-role {:expr [:= :doc/role :param/role]
                       :description "Checks user role"
                       :params {:role {:default "user"}}}}}

param-defaults

(param-defaults registry ns-key policy-key)

Returns default values for a policy’s parameters.

Returns a map of param-key to default value for params that have defaults. Returns empty map if the policy has no defaults or is not found.

;; Given: {:has-role {:expr [...] :params {:role {:default "user"}}}}
(param-defaults registry :auth :has-role)
;=> {:role "user"}

ParamDef

Schema for parameter definition with optional metadata.

Can be either a simple keyword or a rich map with description/default.

parameterized-policies

(parameterized-policies registry ns-key)

Returns all parameterized policies in a module.

Returns a map of policy-key to param info for policies that require params.

(parameterized-policies registry :auth)
;=> {:has-role {:params #{:role}
;               :param-defs {:role {:description "..."}}
;               :description "Checks role"}}

policy-info

(policy-info registry ns-key policy-key)

Returns information about a policy in the registry.

Returns a map with: - :expr — the policy expression - :params — set of required parameter keys - :param-defs — map of param key to definition (description, default, etc.) - :defaults — map of param key to default value - :description — policy description if provided - :parameterized? — true if policy requires params

Returns nil if the policy is not found.

(policy-info registry :auth :has-role)
;=> {:expr [:= :doc/role :param/role]
;    :params #{:role}
;    :param-defs {:role {:description "Role to check"}}
;    :defaults {}
;    :description "Checks user role"
;    :parameterized? true}

PolicyDef

Schema for a policy definition with optional param metadata.

Can be either a simple expression or a rich map with description/params.

register-alias

(register-alias registry alias-key target-key)

Adds an alias to the registry. Returns a new registry.

alias-key is the alias keyword (e.g., :a). target-key is the namespace it points to (e.g., :auth).

Throws if alias-key is a reserved namespace or if target-key does not exist in the registry.

register-alias!

(register-alias! alias-key target-key)

Registers an alias in the global registry.

register-module

(register-module registry ns-key module-def)

Adds a module to the registry. Returns a new registry.

ns-key is the namespace keyword (e.g., :auth). module-def is a map with: - :policies — map of policy-key to policy expression - :imports — (optional) vector of imported namespace keys

Throws if ns-key is a reserved namespace.

register-module!

(register-module! ns-key module-def)

Registers a module in the global registry.

Registry

Schema for the complete registry.

RegistryEntry

Schema for any registry entry.

RegistryEntryType

Valid entry types in the registry.

reserved-namespace?

(reserved-namespace? ns-key)

Returns true if ns-key is a reserved namespace.

reserved-namespaces

Set of namespace keywords reserved for built-in accessors.

reset-global-registry!

(reset-global-registry!)

Resets the global registry to a fresh state. Useful for testing.

resolve-reference

(resolve-reference registry kw)

Resolves a namespaced keyword to its handler information.

Returns a map with: - :namespace — the namespace keyword - :name — the local name keyword - :entry — the resolved registry entry

Returns nil if the namespace is not found.

Resolution precedence: 1. Built-in namespaces (:doc, :fn, :self, :param, :event) 2. Aliases (followed to target) 3. User modules

unregister-module

(unregister-module registry ns-key)

Removes a module from the registry. Returns a new registry.

Does not remove aliases pointing to this module.

validate-module

(validate-module module-def)

Validates a module definition against the schema.

Returns {:ok module-def} if valid, {:error error-map} if invalid.

validate-registry

(validate-registry registry)

Validates the complete registry against the schema.

Returns {:ok registry} if valid, {:error error-map} if invalid.