unionForm function

String? unionForm(
  1. PropType type,
  2. Object? v
)

The form a union-typed value v of type is written in (what decides whether a patch merges into it or replaces it), or null when type has one form only, or v names none.

Implementation

String? unionForm(PropType type, Object? v) {
  if (v == null) return null;
  switch (type.kind) {
    case PropKind.value:
      switch (type.name) {
        case 'AxisSize':
          if (v is num) return 'fixed';
          if (v is Map) {
            // A size object has one key: its mode (nulls are removals).
            final keys = [for (final e in v.entries) if (e.value != null) '${e.key}'];
            return keys.length == 1 ? keys.first : (keys.isEmpty ? null : keys.join('+'));
          }
          return null;
        case 'AxisPosition':
          if (v is num) return 'number';
          if (v is Map) {
            final keys = {for (final e in v.entries) if (e.value != null) '${e.key}'};
            if (keys.contains('align')) return 'align';
            if (keys.contains('relative')) return 'relative';
            return keys.isEmpty ? null : 'edges';
          }
          return null;
        case 'IconData':
          if (v is String) return 'name';
          if (v is Map) return v['type'] is String ? 'type:${v['type']}' : null;
          return null;
      }
      return null;
    case PropKind.object:
      if (v is Map && type.fields.any((f) => f.key == 'type')) {
        final t = v['type'];
        return t is String ? 'type:$t' : null;
      }
      return null;
    default:
      return null;
  }
}