spkersten / dart_functional_data

Simple and non-intrusive code generator for lenses and boilerplate of data types
https://pub.dev/packages/functional_data
MIT License
41 stars 15 forks source link

Possible to generate `merge` and `apply` methods too? #13

Open stargazing-dino opened 4 years ago

stargazing-dino commented 4 years ago

Hi! First off, I love this package. It has solved so many problems for me.

My current use case is when a user signs in, I have their configuration for the app saved in Firebase. If it's their first time signing in, I have to populate it with reasonable empty fields. This is what that currently looks like:

// Configuration extends $Configuration and also uses json_serializable
//
// Inside document.get().then((snapshot) =>
final empty = Configuration.empty();

if (snapshot.exists) {
  final configuration = Configuration.fromJson(snapshot.data);

  await configurationDocument.setData(
    Configuration(
      primaryColor: configuration.primaryColor ?? empty.primaryColor,
      // A lot more fields . . .
    ).toJson(),
  );
} else {
  await configurationDocument.setData(empty.toJson());
}

With merge I think could just say something like:

await configurationDocument.setData(
  configuration.merge(empty).toJson()
);
spkersten commented 4 years ago

This seems like a useful feature.