identifyAttribute<T, U extends ManagedObject> method Null safety

ManagedAttributeDescription identifyAttribute<T, U extends ManagedObject>(
  1. T propertyIdentifier(
    1. U x
    )
)

Returns an attribute in this entity for a property selector.

Invokes identifyProperties with propertyIdentifier, and ensures that a single attribute on this entity was selected. Returns that attribute.

Implementation

ManagedAttributeDescription identifyAttribute<T, U extends ManagedObject>(
  T Function(U x) propertyIdentifier,
) {
  final keyPaths = identifyProperties(propertyIdentifier);
  if (keyPaths.length != 1) {
    throw ArgumentError(
      "Invalid property selector. Cannot access more than one property for this operation.",
    );
  }

  final firstKeyPath = keyPaths.first;
  if (firstKeyPath.dynamicElements != null) {
    throw ArgumentError(
      "Invalid property selector. Cannot access subdocuments for this operation.",
    );
  }

  final elements = firstKeyPath.path;
  if (elements.length > 1) {
    throw ArgumentError(
      "Invalid property selector. Cannot use relationships for this operation.",
    );
  }

  final propertyName = elements.first!.name;
  final attribute = attributes[propertyName];
  if (attribute == null) {
    if (relationships!.containsKey(propertyName)) {
      throw ArgumentError(
          "Invalid property selection. Property '$propertyName' on "
          "'$name' "
          "is a relationship and cannot be selected for this operation.");
    } else {
      throw ArgumentError(
          "Invalid property selection. Column '$propertyName' does not "
          "exist on table '$tableName'.");
    }
  }

  return attribute;
}