Warden Help

Attributes

warden-atts turns your classes into the attribute maps that access requests are made of. The core modules do not depend on it; it is a convenience for the code that builds requests.

dependencies { implementation("codes.laurence.warden:warden-atts:0.5.0") }

HasAttsI and Atts

Atts is a type alias for Map<String, Any?>. HasAttsI is the one-method interface for anything that can produce its attributes. Request-building code depends on these two and nothing else.

Extend HasAtts

HasAtts implements HasAttsI by reflection over the class's public properties. Give it an AttType and the type is added to the map:

/** Shared by every class that represents an account, so policies can match on `type`. */ val AccountType = AttType(type = "Account", typeKeyword = "type") /** Extending `HasAtts` is all a class needs. Its public properties become its attributes. */ data class Account(val id: String, val email: String, val verified: Boolean) : HasAtts(AccountType)
class AttsTest { private val ada = Account(id = "ada", email = "ada@example.com", verified = true) @Test fun `HasAtts maps public properties and adds the type`() { assertThat(ada.atts()) .isEqualTo( mapOf( "type" to "Account", "id" to "ada", "email" to "ada@example.com", "verified" to true, )) } @Test fun `attsOf does the same for a class with another superclass`() { val signup = Signup(email = "bob@example.com", handle = "bob") assertThat(signup.atts()) .isEqualTo(mapOf("type" to "Account", "email" to "bob@example.com", "handle" to "bob")) } @Test fun `nested HasAttsI values become nested maps`() { val team = Team(name = "Docs", owner = ada, members = listOf(ada)) assertThat(team.atts()) .isEqualTo(mapOf("name" to "Docs", "owner" to ada.atts(), "members" to listOf(ada.atts()))) } @Test fun `attributes can be merged and built without an instance`() { assertThat(accountInSession(ada, "s1")).isEqualTo(ada.atts() + ("sessionId" to "s1")) assertThat(accountReference("ada").atts()).isEqualTo(mapOf("type" to "Account", "id" to "ada")) assertThat(accountFromClaims(mapOf("sub" to "ada")).atts()).isEqualTo(mapOf("sub" to "ada")) assertThat(AccountType.atts()).isEqualTo(mapOf("type" to "Account")) } }

Implement HasAttsI with attsOf

A class that already has a superclass cannot extend HasAtts. attsOf is the same reflection as a function:

abstract class Form { abstract fun validate(): List<String> } /** A class that already has a superclass implements `HasAttsI` with `attsOf` instead. */ data class Signup(val email: String, val handle: String) : Form(), HasAttsI { override fun validate(): List<String> = if ("@" in email) emptyList() else listOf("Invalid email") override fun atts(): Atts = AccountType.withAtts(attsOf(this)) }

Nested values

Properties, collection elements and map values that implement HasAttsI are converted to maps too, so nested attributes can be reached with a path such as resource("owner", "id"):

/** Nested `HasAttsI` values, in properties, collections and maps, are converted too. */ data class Team(val name: String, val owner: Account, val members: List<Account>) : HasAtts()

AttType

Several classes often describe the same kind of thing: an entity, a form that creates one, a reference to one by ID. AttType gives them one type attribute. typeKeyword is the key it is stored under, attributeType by default; the samples use type.

Beyond tagging a HasAtts, an AttType builds attributes when there is no instance at hand. withAtts merges the type with other attributes, and asHasAtts wraps the result in a HasAttsI:

/** Attributes for an account you only know the ID of. */ fun accountReference(id: String): HasAttsI = AccountType.asHasAtts(mapOf("id" to id))

Merging and wrapping

withAtts on any HasAttsI adds attributes without changing the object. HasAttsMapWrapper makes a HasAttsI from a map that already exists:

/** Merge extra attributes into an object's attributes without changing the object. */ fun accountInSession(account: Account, sessionId: String): Atts = account.withAtts("sessionId" to sessionId) /** Attributes that arrived as a map, for example from a token or a message. */ fun accountFromClaims(claims: Map<String, Any?>): HasAttsI = HasAttsMapWrapper(claims)
14 November 2026