AutomationUpdateSummary.fromJson constructor

AutomationUpdateSummary.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory AutomationUpdateSummary.fromJson(Map<String, dynamic> json) {
  DateTime? parseDate(List<String> keys) {
    dynamic value;
    for (final key in keys) {
      if (!json.containsKey(key)) {
        continue;
      }
      value = json[key];
      break;
    }
    if (value == null) {
      return null;
    }
    return DateTime.tryParse('$value');
  }

  final environment = <String, String>{};
  final rawEnvironment = json['environment'] ?? json['Environment'];
  if (rawEnvironment is Map) {
    for (final entry in rawEnvironment.entries) {
      environment['${entry.key}'] = '${entry.value}';
    }
  }

  final config = <String, AutomationConfigValue>{};
  final rawConfig = json['config'] ?? json['Config'];
  if (rawConfig is Map) {
    for (final entry in rawConfig.entries) {
      final key = '${entry.key}';
      final value = entry.value;
      if (value is Map) {
        final secret = value['secret'] == true;
        final parsedValue = secret
            ? '${value['value'] ?? '[secret]'}'
            : '${value['value'] ?? ''}';
        config[key] = AutomationConfigValue(
          value: parsedValue,
          secret: secret,
        );
        continue;
      }
      config[key] = AutomationConfigValue(value: '$value', secret: false);
    }
  }

  final resourceChanges = <String, int>{};
  final rawResourceChanges =
      json['resourceChanges'] ?? json['ResourceChanges'];
  if (rawResourceChanges is Map) {
    for (final entry in rawResourceChanges.entries) {
      final value = entry.value;
      if (value is int) {
        resourceChanges['${entry.key}'] = value;
        continue;
      }
      if (value is num) {
        resourceChanges['${entry.key}'] = value.toInt();
        continue;
      }
      final parsed = int.tryParse('$value');
      if (parsed != null) {
        resourceChanges['${entry.key}'] = parsed;
      }
    }
  }

  int? version;
  final rawVersion = json['version'] ?? json['Version'];
  if (rawVersion is int) {
    version = rawVersion;
  } else if (rawVersion is num) {
    version = rawVersion.toInt();
  } else if (rawVersion != null) {
    version = int.tryParse('$rawVersion');
  }

  final policyPacks = <String, String>{};
  final rawPolicyPacks = json['policyPacks'] ?? json['PolicyPacks'];
  if (rawPolicyPacks is Map) {
    for (final entry in rawPolicyPacks.entries) {
      policyPacks['${entry.key}'] = '${entry.value}';
    }
  }

  final dynamic deployment = json['deployment'] ?? json['Deployment'];
  Map<String, dynamic>? deploymentMap;
  if (deployment is Map) {
    deploymentMap = Map<String, dynamic>.from(
      deployment.map((key, value) => MapEntry('$key', value)),
    );
  } else if (deployment is String) {
    try {
      final decoded = jsonDecode(deployment);
      if (decoded is Map) {
        deploymentMap = Map<String, dynamic>.from(
          decoded.map((key, value) => MapEntry('$key', value)),
        );
      }
    } on FormatException {
      // Keep raw deployment text when payload is not valid JSON.
    }
  }

  return AutomationUpdateSummary(
    kind: (json['kind'] ?? json['Kind']) == null
        ? null
        : '${json['kind'] ?? json['Kind']}',
    startTime: parseDate(<String>['startTime', 'StartTime']),
    endTime: parseDate(<String>['endTime', 'EndTime']),
    message: (json['message'] ?? json['Message']) == null
        ? null
        : '${json['message'] ?? json['Message']}',
    environment: environment,
    config: config,
    result: (json['result'] ?? json['Result']) == null
        ? null
        : '${json['result'] ?? json['Result']}',
    version: version,
    deployment: deployment,
    deploymentMap: deploymentMap,
    policyPacks: policyPacks,
    resourceChanges: resourceChanges,
    raw: json,
  );
}