Skip to content

Commit b47f57b

Browse files
committed
feat: add unwrapType and unwrapFields methods to Projection class
1 parent aed5baa commit b47f57b

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

packages/dogs/lib/src/projections.dart

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,24 @@ final class Projection<T> {
304304
return this;
305305
}
306306

307+
/// Sets the value at the given [path] to the native map representation of [instance].
308+
Projection<T> set<S>(String path, S value,
309+
{IterableKind kind = IterableKind.none, Type? type, TypeTree? tree}) {
310+
final native =
311+
engine.toNative<S>(value, kind: kind, type: type, tree: tree);
312+
transformers.add((v) => Projections.$set(v, path, native));
313+
return this;
314+
}
315+
316+
/// Sets the value at the given [path] to the field map representation of [value].
317+
Projection<T> setFields<S>(String path, S value,
318+
{IterableKind kind = IterableKind.none, Type? type, TypeTree? tree}) {
319+
final fieldMap =
320+
engine.toFieldMap<S>(value, kind: kind, type: type, tree: tree);
321+
transformers.add((v) => Projections.$set(v, path, fieldMap));
322+
return this;
323+
}
324+
307325
/// Sets the value at [path] to the [value].
308326
Projection<T> setValue(String path, dynamic value) {
309327
transformers.add((v) => Projections.$set(v, path, value));
@@ -322,29 +340,39 @@ final class Projection<T> {
322340
return this;
323341
}
324342

325-
/// Sets the value at the given [path] to the native map representation of [instance].
326-
Projection<T> set<S>(String path, S value,
327-
{IterableKind kind = IterableKind.none, Type? type, TypeTree? tree}) {
328-
final native =
329-
engine.toNative<S>(value, kind: kind, type: type, tree: tree);
330-
transformers.add((v) => Projections.$set(v, path, native));
343+
/// Adds a transformer to the projection.
344+
Projection<T> addTransformer(ProjectionTransformer transformer) {
345+
transformers.add(transformer);
331346
return this;
332347
}
333348

334-
Projection<T> setFields<S>(String path, S value,
335-
{IterableKind kind = IterableKind.none, Type? type, TypeTree? tree}) {
336-
final fieldMap =
337-
engine.toFieldMap<S>(value, kind: kind, type: type, tree: tree);
338-
transformers.add((v) => Projections.$set(v, path, fieldMap));
349+
/// Unwraps the value at the given [path] and sets it to its native representation.
350+
Projection<T> unwrapType<S>(String path, {
351+
IterableKind kind = IterableKind.none, Type? type, TypeTree? tree,
352+
}) {
353+
transformers.add((v) {
354+
final result = Projections.$get(v, path);
355+
if (!result.exists) return v;
356+
final newValue = engine.toNative(result.value, kind: kind, type: type, tree: tree);
357+
return Projections.$set(v, path, newValue);
358+
});
339359
return this;
340360
}
341361

342-
/// Adds a transformer to the projection.
343-
Projection<T> addTransformer(ProjectionTransformer transformer) {
344-
transformers.add(transformer);
362+
/// Unwraps the value at the given [path] and converts it to a field map.
363+
Projection<T> unwrapFields<S>(String path, {
364+
IterableKind kind = IterableKind.none, Type? type, TypeTree? tree,
365+
}) {
366+
transformers.add((v) {
367+
final result = Projections.$get(v, path);
368+
if (!result.exists) return v;
369+
final newValue = engine.toFieldMap(result.value, kind: kind, type: type, tree: tree);
370+
return Projections.$set(v, path, newValue);
371+
});
345372
return this;
346373
}
347374

375+
348376
/// Applies the projection to the given optional [initial] map and returns the result.
349377
T perform([Map<String, dynamic>? initial]) {
350378
var result = initial ?? <String, dynamic>{};

0 commit comments

Comments
 (0)