spkersten / dart_functional_data Goto Github PK
View Code? Open in Web Editor NEWSimple and non-intrusive code generator for lenses and boilerplate of data types
Home Page: https://pub.dev/packages/functional_data
License: MIT License
Simple and non-intrusive code generator for lenses and boilerplate of data types
Home Page: https://pub.dev/packages/functional_data
License: MIT License
This leads to type errors when the equality class specified with CustomEquality
has specific types:
error: The argument type 'dynamic' can't be assigned to the parameter type 'int'. (argument_type_not_assignable at ...)
@FunctionalData()
class Generic<T> extends $Generic<T> {
final T generic;
const Generic({
@required this.generic,
});
}
generates:
abstract class $Generic {
T get generic;
const $Generic();
Generic copyWith({T generic}) => Generic(generic: generic ?? this.generic);
String toString() => "Generic(generic: $generic)";
bool operator ==(dynamic other) =>
other.runtimeType == runtimeType && generic == other.generic;
@override
int get hashCode {
var result = 17;
result = 37 * result + generic.hashCode;
return result;
}
}
But it should generate:
abstract class $Generic<T> {
T get generic;
const $Generic();
Generic<T> copyWith({T generic}) => Generic(generic: generic ?? this.generic);
...
}
When i try do something like
static const _minCnt = 1;
static const _maxCnt = 6;
class Model extends $Model {
static const _minCnt = 1;
static const _maxCnt = 6;
...
i got errorous implementation :
abstract class $Model {
int get _minCnt;
int get _maxCnt;
const $Model();
Model copyWith(
{int _minCnt,
int _maxCnt,
in your sample:
@FunctionalData()
class Foo with $Foo {
final int number;
final String name;
// Can't be const because of mixin
Foo({this.number, this.name});
}
in real life i got error Class can't be used as mixin because it declares constructor...
@immutable
@FunctionalData()
class Base with $Base{
final int uid;
final int editTime;
Base({this.uid, this.editTime});
}
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()
);
void Function(, {String interactionType}) apply;
When I generate code for the following class
@FunctionalData()
class Bar extends $Bar{
Bar({this.foo});
final String foo;
}
It results in:
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'bar.dart';
// **************************************************************************
// FunctionalDataGenerator
// **************************************************************************
// ignore_for_file: join_return_with_assignment
// ignore_for_file: avoid_classes_with_only_static_members
// ignore_for_file: non_constant_identifier_names
// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes
abstract class $Bar {
const $Bar();
String get foo;
Bar copyWith({String foo}) => Bar(foo: foo ?? this.foo);
@override
String toString() => "Bar(foo: $foo)";
@override
bool operator ==(dynamic other) =>
other.runtimeType == runtimeType && foo == other.foo;
@override
int get hashCode {
var result = 17;
result = 37 * result + foo.hashCode;
return result;
}
}
class Bar$ {
static final foo =
Lens<Bar, String>((s_) => s_.foo, (s_, foo) => s_.copyWith(foo: foo));
}
This generated code has 2 violations to dart lint.
I will submit a pr that will fix this.
There's a weird bug that occurs when using flutter types.
...
import 'package:functional_data/functional_data.dart';
import 'package:flutter/material.dart';
...
part 'blend_mode.g.dart';
@FunctionalData()
class BlendModeData extends $BlendModeData {
final BlendMode mode;
const BlendModeData({
@required this.mode,
});
const BlendModeData.deflt() : mode = BlendMode.srcOver;
}
would lead to
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'blend_mode.dart';
// **************************************************************************
// FunctionalDataGenerator
// **************************************************************************
abstract class $BlendModeData {
dynamic get mode;
const $BlendModeData();
BlendModeData copyWith({dynamic mode}) =>
BlendModeData(mode: mode ?? this.mode);
String toString() => "BlendModeData(mode: $mode)";
bool operator ==(dynamic other) =>
other.runtimeType == runtimeType && mode == other.mode;
@override
int get hashCode {
var result = 17;
result = 37 * result + mode.hashCode;
return result;
}
}
class BlendModeData$ {
static final mode = Lens<BlendModeData, dynamic>(
(s_) => s_.mode, (s_, mode) => s_.copyWith(mode: mode));
}
Where mode should not be dynamic but BlendMode.
Everything works fine with non-flutter types.
Am I doing something wrong or is this a real issue?
=()
generates incorrect code for collection so
@FunctionalData()
class MyList extends $MyList{
final List<int> items;
MyList({this.items});
}
test('list test', (){
final list1 = MyList(items: [1,2,3]);
final list2 = MyList(items: [1,2,3]);
expect(list2, equals(list1));
});
makes error.
It's all because of incorrect collection equality, see https://stackoverflow.com/questions/10404516/how-can-i-compare-lists-for-equality-in-dart
For a class like:
class Holder<T>{
final T t;
Holder(this.t);
}
The generated Lenses remove the type parameter. It would be nice to have a lens of type Lens<Holder<T>,T>
but instead we get Lens<Holder,T>
which does not compile.
The original class _ViewModel
is private, but the generated class is $_ViewModel
, which is not private. It lead to name conflict when export multiple modules that have the same class _ViewModel
, but this should be ok.
hi,
final copyWith = '$className copyWith({${fields.map((f) => '${f.type} ${f.name}').join(', ')}}) => $className(${fields.map((f) => '${f.name}: ${f.name} ?? this.${f.name}').join(', ')});';
Generated code doesn't update nullable properties. E.g. x.copyWith(myProp: null)
ignores myProp.
As of now the embedded Optional<T> does not override the equality test ==
, which complicates testing equality of structures with optionals (not only classes with optional members, but also collections with optional items).
Discovered by @alexei-sintotski, opening a ticket for the sake of documenting the issue.
Our reactive_ble library depends on functional_data. In order to successfully migrate our library to Dart null-safety. We need a null safety compatible version of Functional data.
Currently functional_data_generator
have version constraints:
analyzer: ">=0.38.1 <0.39.0"
This creates issues with pub get
and other dependencies
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.