getEntityKeyValue method

Object? getEntityKeyValue(
  1. O o, [
  2. EntityHandler<O>? entityHandler
])

Implementation

Object? getEntityKeyValue(O o, [EntityHandler<O>? entityHandler]) {
  if (o == null) return null;

  dynamic obj = o;
  EntityFieldAccessor fieldAccessor = this;

  for (var key in keys) {
    Object? value;

    if (key is ConditionKeyField) {
      var objEntityHandler = entityHandler?.getEntityHandler(obj: obj);
      value = fieldAccessor.getField(
        obj,
        key.name,
        entityHandler: objEntityHandler,
      );
    } else if (key is ConditionKeyIndex) {
      var index = key.index;

      if (obj is Iterable) {
        value = obj.elementAt(index);
      } else {
        throw StateError(
          "Can't access index[$index] of type: ${(obj as Object?).runtimeTypeNameUnsafe}",
        );
      }
    }

    if (value == null) {
      return null;
    } else {
      obj = value;
      fieldAccessor = _accessorGeneric;
    }
  }

  return obj;
}