PolicyEntry.fromJson constructor

PolicyEntry.fromJson(
  1. Map<String, dynamic> map
)

Returns a PolicyEntry with groups (subjects and resources) specified in the map.

Throws a JsonMissingKeyException if there is no policyId key in the map. Throws a InvalidJsonSchemaException if the entry key doesn't contain valid PolicyGroups. Throws a FormatException if some values doesn't match the expected value type.

Implementation

factory PolicyEntry.fromJson(Map<String, dynamic> map) {
  if (map.isEmpty) throw const FormatException('empty map');
  try {
    final String pId = map.containsKey(DittoKeys.policyId)
        ? map[DittoKeys.policyId] as String
        : throw JsonMissingKeyException(DittoKeys.policyId, map.toString());
    final PolicyEntry pE = PolicyEntry(pId);
    try {
      if (map.containsKey(DittoKeys.entries)) {
        final Map<String, dynamic> gro =
            map[DittoKeys.entries] as Map<String, dynamic>;
        for (final String k in gro.keys) {
          pE._groups[k] =
              PolicyGroup.fromJson(k, gro[k] as Map<String, dynamic>);
        }
      }
    } on TypeError catch (e) {
      throw InvalidJsonSchemaException(
          e.stackTrace.toString(), map.toString());
    }
    return pE;
  } on TypeError catch (e) {
    throw FormatException(e.stackTrace.toString());
  }
}