lifecycleBlock static method

Map<String, dynamic>? lifecycleBlock(
  1. LifecycleOptions opts
)

lifecycle { ... } nested block, or null when no fields are set.

Implementation

static Map<String, dynamic>? lifecycleBlock(LifecycleOptions opts) {
  final out = <String, dynamic>{};
  if (opts.createBeforeDestroy case final v?) {
    out['create_before_destroy'] = v;
  }
  if (opts.preventDestroy case final v?) out['prevent_destroy'] = v;
  switch (opts.ignoreChanges) {
    case IgnoreAllChanges():
      out['ignore_changes'] = 'all';
    case IgnoreAttributes(:final attributes) when attributes.isNotEmpty:
      out['ignore_changes'] = List<String>.from(attributes);
    case IgnoreAttributes() || null:
      break;
  }
  final replace = opts.replaceTriggeredBy;
  if (replace != null && replace.isNotEmpty) {
    out['replace_triggered_by'] = replace.map(replaceTriggerAddress).toList();
  }
  for (final post in [false, true]) {
    final blocks = [
      for (final c in opts.conditions ?? const <LifecycleCondition>[])
        if (c.post == post)
          {
            'condition': c.condition.toTfJson(),
            'error_message': c.errorMessage,
          },
    ];
    if (blocks.isNotEmpty) {
      out[post ? 'postcondition' : 'precondition'] = blocks;
    }
  }
  return out.isEmpty ? null : out;
}