Skip to content

Add explicit unique ResourceTag-like keys #107

@zainab-ali

Description

@zainab-ali

This issue was copied over from: https://github.com/disneystreaming/weaver-test/issues/266
It was opened by: nigredo-tori


This is inspired by the vault library.

At the moment global resources are indexed by ResourceTags and labels. This has its issues:

  1. The default ResourceTag implementation derives its identity from ClassTag. This leads to a possibility of collisions, as stated in the documentation. The suggested workarounds are monomorphic wrappers or custom ResourceTag instances.
  2. Labels mitigate the collision problem somewhat (or, rather, leave it up to discipline). But the labels are not tied to resource types, so it's easy to mix up which labels correspond to which resource.

Instead, we can create objects that are guaranteed to be unique, and which are associated with a specific type. Consider something like this:

// default `equals` - different objects are never equal.
final private class UniqueResourceTag[A](
  val description: String
) extends ResourceTag[A] {
  def cast(obj: Any): Option[A] = Some(obj.asInstanceOf[A])
}
def unsafeCreateUniqueTag[A](description: String): ResourceTag[A] =
  new UniqueResourceTag[A](description)

This can be used as follows:

// A "constant" somewhere in a common object
val fooTag = unsafeCreateUniqueTag[Foo]("")
// Inside a suite
global.getR()(fooTag)

The current API is obviously not perfect for this, but the general idea should be clear enough. Note that there's no way for fooTag to collide with anything else (assuming no broken equals implentations), and there's no way to mistake the type of the resource it points to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions