Files
pipelines/app/lib/features/templates/domain/template_entry.dart
T
alan cd9ec62d28 feat(app): add template catalogue and create-only submit repository
Add the nonvisual templates data/domain slice: TemplateRepository over the
shared ApiGateway for list_templates, validate and create-only submit, and
TemplateEntry preserving every server field in a deeply read-only raw map.

Re-include test/**/data/ in app/.gitignore so the mirroring tests are not
silently dropped from a fresh checkout.

Tests cover exact request shapes, source preservation, blank-name refusal,
malformed replies, server refusals and immutability.
2026-10-21 15:31:10 +01:00

64 lines
2.0 KiB
Dart

import '../../../core/api_gateway.dart';
/// One template from the server catalogue.
///
/// The server owns this record: [key], [name], [description], [source] and
/// [scheduled] are read from it verbatim and every other field is kept in
/// [raw], so a later view or round-trip never loses fields this client does not
/// understand yet. Loading a template is a read; it never publishes or runs it.
class TemplateEntry {
const TemplateEntry({
required this.key,
required this.name,
required this.description,
required this.source,
required this.scheduled,
required this.raw,
});
factory TemplateEntry.fromJson(JsonMap json) {
final raw = _frozenMap(json);
return TemplateEntry(
key: _text(raw['key']),
name: _text(raw['name']),
description: _text(raw['description']),
source: _text(raw['source']),
scheduled: raw['scheduled'] == true,
raw: raw,
);
}
/// The stable catalogue key the gallery selects on.
final String key;
final String name;
final String description;
/// The template's complete, untrimmed definition source.
final String source;
/// Whether the source declares a schedule (the summary's "Includes a schedule").
final bool scheduled;
/// Every field the server sent, including fields this client does not model.
/// Deeply unmodifiable: nested maps and lists are copied into read-only
/// collections too, so a view cannot mutate the server reply through it.
final JsonMap raw;
static String _text(dynamic value) => value == null ? '' : '$value';
static JsonMap _frozenMap(Map<String, dynamic> json) =>
Map<String, dynamic>.unmodifiable(
json.map((key, value) => MapEntry(key, _frozen(value))),
);
static dynamic _frozen(dynamic value) {
if (value is Map) {
return _frozenMap(Map<String, dynamic>.from(value));
}
if (value is List) {
return List<dynamic>.unmodifiable(value.map(_frozen));
}
return value;
}
}