amelinium.identity

added in 1.0.0

Basic identity management for amelinium.

->db

macro

(->db user-identity)(->db identity-type user-identity)

For the given user identity user-identity and optional identity type identity-type tries to express the given identity’s value in a database suitable format.

Uses to-db* multimethod to perform user-identity transformation on a basis of its identity type.

If the given identity is not a kind of amelinium.Identity record, it will be converted to it first. If the given value cannot be used as valid identity, nil is returned.

If the identity-type is given, it should be a valid identity type. It instructs the function to treat the given identity as of this type during pre-conversion. If the identity is already an amelinium.Identity record but its type is different, nil will be returned.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If the identity-type is given as literal keyword, string or nil, then a specific conversion function will be obtained at compile-time.

If identity-type is an acceptable literal (or not given at all) and user-identity expression is a value for which the function amelinium.proto.identity/literal? returns true then the conversion is done immediately, and its result replaces the macro call at compile-time.

However, if the immediate conversion result is nil, or it is not a value for which the function amelinium.proto.identity/literal? returns true, an expression with call to to-db or to-db* will be generated as fallback to perform the conversion at run-time.

->str

macro

(->str user-identity)(->str identity-type user-identity)

For the given user identity user-identity and optional identity type identity-type tries to express the given identity’s value as a string.

Uses to-str* multimethod to perform user-identity transformation on a basis of its identity type.

If the given identity is not a kind of amelinium.Identity record, it will be converted to it first. If the given value cannot be used as valid identity, nil is returned.

If the identity-type is given, it should be a valid identity type. It instructs the function to treat the given identity as of this type during pre-conversion. If the identity is already an amelinium.Identity record but its type is different, nil will be returned.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If the identity-type is given as literal keyword, string or nil, then a specific conversion function will be obtained at compile-time.

If identity-type is an acceptable literal (or not given at all) and user-identity expression is a value for which the function amelinium.proto.identity/literal? returns true then the conversion is done immediately, and its result replaces the macro call at compile-time.

However, if the immediate conversion result is nil, or it is not a value for which the function amelinium.proto.identity/literal? returns true, an expression with call to to-str or to-str* will be generated as fallback to perform the conversion at run-time.

acceptable-type

(acceptable-type user-identity acceptable-tag)(acceptable-type user-identity identity-type acceptable-tag)

Returns a keyword describing identity type detected by analyzing the given user identity user-identity and optional identity type identity-type given explicitly (e.g. :phone for a phone number, :email for e-mail address, :id for numeric user ID, :uid for UUID).

If the type is not given, or if it is nil or false, analysis of the given identity will be performed.

The given identity type must always be valid (registered with amelinium.proto.identity/add-type!) and the identity must not be nil nor false. If any of these happens, nil will be returned.

The acceptable-tag should be a tag type from which the given type is derived within the hierarchy amelinium.proto.identity/type-hierarchy.

To add acceptable type(s) use amelinium.proto.identity/add-acceptable-type.

add-acceptable-type!

(add-acceptable-type! acceptable-tag t)(add-acceptable-type! acceptable-tag t & more)

For the given parent tag acceptable-tag (which should be a qualified keyword) and an identity type t, creates a relation so that the identity type is a descendant of the given parent. It also ensures that the parent itself is a descendant of :amelinium.identity/valid tag.

Additionally, it makes an extra parent for the given acceptable tag in amelinium.proto.identity/type-hierarchy which is :amelinium.proto.identity/group. This special value is a valid dispatch for the parser multimethod which uses parse-group to handle identity parsing with acceptable tag given explicitly as its identity type specification. If you don’t want that, use amelinium.proto.identity/add-acceptable-type! directly.

Useful when there is a need to accept a limited set of recognized identity types. Then the detection function can check whether an identity belongs to a parent.

Makes changes in the global identity type hierarchy amelinium.identity.proto/type-hierarchy.

check-type

(check-type id-type)(check-type id-type acceptable-tag)

Returns a keyword for the given identity type identity-type if it is a valid identity. Otherwise it returns nil. To add acceptable type(s) use amelinium.proto.identity/add-acceptable-type.

If acceptable parent tag acceptable-tag is given, it must be a parent of the given identity type tag.

id-email-string

(id-email-string v)(id-email-string v t)

Returns true if the given string looks like an e-mail address. If identity type or acceptable type t is given, then it must be a parent or be equal to :email.

id-id-string

(id-id-string v)(id-id-string v t)

Returns true if the given string looks like a user ID. If identity type or acceptable type t is given, then it must be a parent or be equal to :id.

id-phone-string

(id-phone-string v)(id-phone-string v t)

Returns true if the given string looks like a phone number. If identity type or acceptable type t is given, then it must be a parent or be equal to :phone.

id-uid-string

(id-uid-string v)(id-uid-string v t)

Returns true if the given string looks like a UID (UUID). If identity type or acceptable type t is given, then it must be a parent or be equal to :uid.

identity-map-keys

Commonly known map keys which may contain user identity.

identity-map-keys-by-type

Commonly known map keys which may contain user identity grouped by identity types.

of

(of user-identity)(of identity-type user-identity)(of identity-type user-identity & user-identities)(of user-identity & user-identities)

For the given user identity user-identity tries to parse the identity and return an amelinium.Identity record containing a detected identity type and identity value in a form it expresses it best. If the identity type cannot be established and it was not given, nil is returned.

Optional identity type identity-type (which must be a keyword) may be given as a first argument. It is used to pick the right parser (if parsing is needed) or to simply reject wrong identity type (if amelinium.Identity record is given).

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If there is a need to give list of keywords as user identities (including the first), and not to give any identity type, then first argument’s value needs to be converted to a string, or of-value or of-type should be used as an alternative.

If multiple identities are given it will return a sequence of these identities parsed with parsing functions chosen for detected identity types. If an identity type is cannot be detected and/or is not valid, nil value will be inserted into the corresponding location of generated output sequence.

of-known-type?

(of-known-type? v)(of-known-type? v acceptable-tag)

Returns true if the given value v is a user identity of the known type.

If the acceptable-tag is present then it should be a tag type from which the given type is derived within hierarchy amelinium.proto.identity/type-hierarchy.

To add acceptable type(s) use amelinium.proto.identity/add-acceptable-type.

of-seq

(of-seq user-identities)(of-seq identity-type user-identities)

For the given user identities user-identities tries to parse each identity and return an amelinium.Identity record containing a detected identity type and identity value in a form it expresses it best. If the given identity type was given as an optional identity-type argument, it will be assumed as the expected type for all input values.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If an identity type was not given but it cannot be detected and/or is not valid, nil value will be inserted into the corresponding location of generated output sequence. Same with the given identity type which is not applicable to a particular value or invalid.

of-type

(of-type identity-type user-identity)(of-type identity-type user-identity & ids)

For the given user identity user-identity and identity type identity-type it tries to parse the identity and return an amelinium.Identity record containing an identity type and identity value in a form it expresses it best. If the identity type is nil then nil is returned.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If multiple identities are given it will return a sequence of these identities parsed with parsing functions chosen for the given identity type. If the identity type is not valid or the parsing cannot be applied for an input value, nil will be inserted into corresponding location of the output sequence.

of-value

(of-value user-identity)(of-value user-identity & ids)

For the given user identity user-identity tries to parse the identity and return an amelinium.Identity record containing a detected identity type and identity value in a form it expresses it best. If the identity type cannot be established and it was not given, nil is returned.

If multiple identities are given it will return a sequence of these identities parsed with parsing functions chosen for detected identity types. If an identity type is cannot be detected and/or is not valid, nil value will be inserted into the corresponding location of generated output sequence.

opt-type

(opt-type identity-type user-identity)(opt-type identity-type user-identity & ids)

For the given user identity user-identity and identity type identity-type it tries to parse the identity and return an amelinium.Identity record containing an identity type and identity value in a form it expresses it best. If the identity type is nil or empty then any identity type is accepted.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If multiple identities are given it will return a sequence of these identities parsed with parsing functions chosen for the given identity type. If the identity type is not valid or the parsing cannot be applied for an input value, nil will be inserted into corresponding location of the output sequence.

parse

(parse user-identity)(parse identity-type user-identity)

Parses the given identity user-identity with optional identity-type predefined. Returns an identity record of type amelinium.Identity. Memoized proxy for parsing strings and other non-native data.

Do not use it directly, use of or of-type instead.

Caution: The identity type must be a keyword (it will not be coerced).

parse-email

(parse-email v)

Parses e-mail by doing basic checks and transforming it to an Identity object.

parse-group

(parse-group acceptable-type user-identity)

Parses identity identity-type with acceptable-type tag which should be a parent tag grouping identity types (like :amelinium.identity/any or another). Will use amelinium.proto.identity/type passing a tag to it to limit the parsers tried and constrain the identity type to one that belongs to a parent tag in amelinium.proto.identity/type-hierarchy. Hint: new parent tags can be registered with amelinium.proto.identity/add-acceptable-type!.

This is internal function. Use of-type instead, which will choose the appropriate parser via multimethod and cache the results.

parse-id

(parse-id v)

Parses user ID by doing basic checks and transforming it to an Identity record.

parse-map

(parse-map m)(parse-map identity-type m)

Tries to extract identity from a map m by searching for commonly known identity keys.

Optional identity type identity-type will be used to constrain the conversion. Its value may also be an acceptable type tag used to group identity types semantically.

If a known key is found but its associated value cannot be converted to Identity object, process continues and other keys are tried.

It uses identity-map-keys when no identity type is given, or it is set to :amelinium.identity/any.

Uses identity-map-keys-by-type when an identity type is given to select a group of keys to be tried out. It will also look for a key that matches the given acceptable identity type tag (if passed as identity-type instead of a type). If that fails it performs a series of lookups in the map by taking each direct descendant of the given tag associated within amelinium.proto.identity/type-hierarchy.

When a group is not found for the given identity type or acceptable type tag, nil is returned.

parse-phone

(parse-phone v)

Tries to interpret v as a phone number and returns an Identity record.

parse-single

(parse-single identity-type user-identity)(parse-single user-identity)

Parses identity user-identity by choosing the parser with parser multimethod on a basis of the given identity-type or a detected identity type (by getting it with amelinium.proto.identity/type). Returns user identity object or nil.

This is internal function. Use of or of-type instead, which will cache the results.

parse-uid

(parse-uid v)

Tries to interpret v as a UUID and returns an Identity record.

parser

multimethod

Takes an identity type expressed as keyword and returns a parser suitable for that identity type. The parser function takes 1 argument and converts the given value to identity record (of type amelinium.Identity).

Caution: The identity type must be a keyword (it will not be coerced).

preparse-email

(preparse-email v)

Parses e-mail by doing basic checks and transforming it to a string.

preparse-id

(preparse-id v)

Parses user ID by doing basic checks and transforming it to a long number.

preparse-phone

(preparse-phone v)

Tries to create an object representing phone number. Returns nil if the input cannot be parsed.

preparse-uid

(preparse-uid v)

Tries to create UUID. Returns nil if the input cannot be converted to it.

some-seq

(some-seq user-identities)(some-seq identity-type user-identities)

Tries to coerce identities to amelinium.Identity objects and filters out those who could not be coerced.

to-db

(to-db user-identity)(to-db identity-type user-identity)

For the given user identity user-identity tries to express the identity in a database suitable format.

Uses to-db* multimethod to perform user-identity transformation on a basis of its identity type.

If the given identity is not a kind of amelinium.Identity record it will be converted to it first. If the given value cannot be used as valid identity, nil is returned.

If the identity-type is given, it should be a valid identity type. It instructs the function to treat the given identity as of this type during pre-conversion.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If the identity is already an amelinium.Identity record but its type is different, nil will be returned.

If possible, use ->db macro instead to get some compile-time optimizations.

to-db*

multimethod

(to-db* user-identity)(to-db* identity-type user-identity)

For the given user identity user-identity which must be of type amelinium.Identity, tries to express the identity in a database suitable format.

If the given value cannot be used as valid identity, nil is returned.

If the identity-type is given, it must be a keyword and it should be a valid identity type. It instructs the function to treat the given identity as of this type.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

This is internal multimethod which does not perform conversions or checks. Use to-db or ->db instead.

to-str

(to-str user-identity)(to-str identity-type user-identity)

Takes a user identity user-identity and optional identity type identity-type, and converts it to a string.

If the given identity is not a kind of amelinium.Identity record it will be converted to it first. If the given value cannot be used as valid identity, nil is returned.

If the identity-type is given, it should be a valid identity type. It instructs the function to treat the given identity as of this type.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

If the identity is an amelinium.Identity record but its type is different, nil will be returned.

to-str*

multimethod

(to-str* user-identity)(to-str* identity-type user-identity)

Takes a user identity user-identity expressed as Identity record, and converts it to a string.

If the identity-type is given, it should be a valid identity type expressed with a keyword. It instructs the function to treat the given identity as of this type. If the identity type is different than in the given object, nil is returned.

A value of identity-type may also be an acceptable type tag (registered with add-acceptable-type!) which groups identity types. In such case parsing and detection will be constrained to identity types being its descendants.

This is internal multimethod which does not perform conversions or checks. Use to-str function or ->str macro instead.

type

(type user-identity)(type identity-type user-identity)

Returns a keyword describing identity type detected by analyzing the given user identity user-identity and optional identity type identity-type given explicitly (e.g. :phone for a phone number, :email for e-mail address, :id for numeric user ID, :uid for UUID). If the type is not given, analysis of the given identity will be performed.

Value of the given identity type identity-type must match the identity type, and not be nil nor false. If any of these happens, nil will be returned.

type-opt

(type-opt user-identity)(type-opt identity-type user-identity)

Returns a keyword describing identity type detected by analyzing the given user identity user-identity and optional identity type identity-type given explicitly (e.g. :phone for a phone number, :email for e-mail address, :id for numeric user ID, :uid for UUID).

If the type is not given or it is nil, analysis of the given identity will be performed.

Value of the given identity type identity-type must match the identity type. If it does not, nil will be returned.

type?

(type? t)(type? t acceptable-tag)

Returns true if the given identity type identifier t exists and is valid.

If acceptable-tag is given this function will in addition check if the given tag is a parent of the given type identifier or is :amelinium.identity/any. To add acceptable type(s) use amelinium.proto.identity/add-acceptable-type.

unaccept-type!

(unaccept-type! acceptable-tag t)(unaccept-type! acceptable-tag t & more)

Removes identity type t from the given parent acceptable-tag and removes parent :amelinium.proto.identity/group from the acceptable tag in the amelinium.proto. hierarchy. Makes changes in the global identity type hierarchy amelinium.identity.proto/type-hierarchy.

value

(value user-identity)(value identity-type user-identity)

Returns a value of the given identity type user-identity and optional identity type identity-type. If the type is not given, analysis of the given identity will be performed to establish the type first. If the type is given, it must be a valid type, otherwise nil will be returned.