DataEntityKind.fromJson constructor

DataEntityKind.fromJson(
  1. Map<String, Object?> json, {
  2. required String owner,
})

Reads one declaration: {"type": "vent", "requires": ["size"], "collider": "trigger"}. Throws a DocumentFormatException saying what is wrong.

Implementation

factory DataEntityKind.fromJson(
  Map<String, Object?> json, {
  required String owner,
}) {
  final type = json['type'];
  if (type is! String || type.isEmpty) {
    throw DocumentFormatException('an entity kind names no type');
  }
  final requires = json['requires'] ?? const <Object?>[];
  if (requires is! List<Object?> || requires.any((r) => r is! String)) {
    throw DocumentFormatException(
      'entity kind "$type": requires is not a list of '
      'property names',
    );
  }
  final collider = switch (json['collider']) {
    null => null,
    'trigger' => ColliderKind.trigger,
    'static' => ColliderKind.static,
    'kinematic' => ColliderKind.kinematic,
    final Object other => throw DocumentFormatException(
      'entity kind "$type": collider "$other" is not trigger, static or '
      'kinematic',
    ),
  };
  return DataEntityKind(
    type,
    owner: owner,
    requires: List<String>.unmodifiable(requires.cast<String>()),
    collider: collider,
  );
}