Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/integration_class_to_string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1
with:
sdk: 3.0.0
sdk: 3.8.0

- name: Resolve dependencies
run: dart pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/integration_mek_data_class.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1
with:
sdk: 3.0.0
sdk: 3.8.0

- name: Resolve dependencies
run: dart pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/integration_mek_data_class_generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dart-lang/setup-dart@v1
with:
sdk: 3.0.0
sdk: 3.8.0

- name: Resolve dependencies
run: dart pub get
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Auto generation of:
- [x] `hashCode` and `==` methods
- [x] pretty `toString` method
- [x] `copyWith` method with support a `null` values
- [x] `merge` to merge current instance values with another
- [x] `*Changes` class to updated your data class
- [x] `*Builder` class to build or update your data class

Expand Down
10 changes: 7 additions & 3 deletions class_to_string/lib/src/class_to_flat_string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:class_to_string/src/utils.dart';
class ClassToFlatString extends ClassToStringBase {
StringBuffer? _result = StringBuffer();

var _hasPreviousField = false;
var _isEmpty = true;

ClassToFlatString(String className, [Iterable<Type> types = const []]) {
_result!.write(className);
Expand All @@ -24,19 +24,23 @@ class ClassToFlatString extends ClassToStringBase {

@override
void add(String name, Object? value) {
if (_hasPreviousField) _result!.write(',');
if (_isEmpty) {
_isEmpty = false;
} else {
_result!.write(',');
}
_result!
..write(name)
..write(':')
..writeValue(value);
_hasPreviousField = true;
}

@override
String toString() {
_result!.write(')');
final stringResult = _result.toString();
_result = null;
_isEmpty = true;
return stringResult;
}
}
1 change: 1 addition & 0 deletions class_to_string/lib/src/class_to_indent_string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ClassToIndentString extends ClassToStringBase {
_result!.write(')');
final stringResult = _result.toString();
_result = null;
_isEmpty = true;
return stringResult;
}
}
2 changes: 1 addition & 1 deletion example/lib/basic_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ProductEquality implements Equality<Product> {
bool isValidKey(Object? o) => throw UnimplementedError();
}

@DataClass(changeable: true)
@DataClass(buildable: true, copyable: true, mergeable: true, changeable: true)
class EmptyClass with _$EmptyClass {
const EmptyClass._();
}
19 changes: 19 additions & 0 deletions example/lib/basic_example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/lib/edge_cases.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:mek_data_class/mek_data_class.dart';

part 'edge_cases.g.dart';

@DataClass(changeable: true, buildable: true, copyable: true)
@DataClass(buildable: true, copyable: true, mergeable: true, changeable: true)
class $Dollar with _$$Dollar {
final $Dollar $dollar;
final $Dollar? euro;
Expand Down
10 changes: 10 additions & 0 deletions example/lib/edge_cases.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ dependencies:
json_annotation: ^4.9.0

dependency_overrides:
class_to_string:
path: ../class_to_string
mek_data_class:
path: ../mek_data_class
class_to_string: { path: ../class_to_string }
mek_data_class: { path: ../mek_data_class }

dev_dependencies:
mek_lints: ^2.0.0
test: ^1.21.6

build_runner: ^2.8.0
mek_data_class_generator:
path: ../mek_data_class_generator
mek_data_class_generator: { path: ../mek_data_class_generator }
json_serializable: ^6.11.1
3 changes: 3 additions & 0 deletions mek_data_class/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

## 2.2.0
- feat: added `DataClass.mergeable` which generates the `merge` method to copy the values of another instance into the current instance

## 2.1.0
- chore: bumped min dart version to `3.8.0`

Expand Down
7 changes: 6 additions & 1 deletion mek_data_class/lib/mek_data_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class DataClass {
/// Default: `false`
final bool? copyable;

/// Adds the `merge` method to the class.
/// Default: `false`
final bool? mergeable;

/// Adds the `change` and `toChanges` method to the class.
/// Default: `false`
final bool? changeable;
Expand All @@ -46,8 +50,9 @@ class DataClass {
const DataClass({
this.comparable,
this.stringify,
this.copyable,
this.buildable,
this.copyable,
this.mergeable,
this.changeable,
this.equalities = const [],
});
Expand Down
2 changes: 1 addition & 1 deletion mek_data_class/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: mek_data_class
description: >
Generate `hashCode`, `==`, `toString`, `copyWith`, `change` methods and `Builder` class with low code
version: 2.1.0
version: 2.2.0
homepage: https://github.com/BreX900/data_class/tree/main/mek_data_class
repository: https://github.com/BreX900/data_class
topics:
Expand Down
7 changes: 7 additions & 0 deletions mek_data_class_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@


## 4.2.0
- feat: added the ability to generate the `merge` method to copy the values of another instance into the current instance
- fix: fixed missing space when generating an empty const constructor
- chore: `copyWith` and `merge` methods return the original data class instance if constructor has no parameters
- chore: allowed analyzer `>=8.1.1 <10.0.0`

## 4.1.1
- build: require `analyzer: ^9.0.0`
- build: require `build: ^4.0.3`
Expand Down
3 changes: 2 additions & 1 deletion mek_data_class_generator/lib/mek_data_class_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:mek_data_class_generator/src/helpers/change_helper.dart';
import 'package:mek_data_class_generator/src/helpers/copy_with_helper.dart';
import 'package:mek_data_class_generator/src/helpers/equatable_helper.dart';
import 'package:mek_data_class_generator/src/helpers/helper_core.dart';
import 'package:mek_data_class_generator/src/helpers/merge_helper.dart';
import 'package:mek_data_class_generator/src/helpers/to_string_helper.dart';
import 'package:source_gen/source_gen.dart' hide LibraryBuilder;
import 'package:source_helper/source_helper.dart';
Expand Down Expand Up @@ -44,7 +45,7 @@ class DataClassGenerator extends GeneratorForAnnotation<DataClass> {
}

class _RegistryHelper extends HelperCore
with CopyWithHelper, ChangeHelper, BuilderHelper, EquatableHelper, ToStringHelper {
with CopyWithHelper, MergeHelper, ChangeHelper, BuilderHelper, EquatableHelper, ToStringHelper {
final _libraryBody = <Spec>[];
final _mixinMethods = <Method>[];
var _shouldCreateSelfMixinGetter = false;
Expand Down
6 changes: 5 additions & 1 deletion mek_data_class_generator/lib/src/configs/class_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class ClassConfig {
final bool stringify;
final bool buildable;
final bool copyable;
final bool mergeable;
final bool changeable;
final List<DartObject> equalities;

const ClassConfig({
required this.comparable,
required this.stringify,
required this.copyable,
required this.buildable,
required this.copyable,
required this.mergeable,
required this.changeable,
required this.equalities,
});
Expand All @@ -32,6 +34,7 @@ class ClassConfig {
stringify: false,
buildable: false,
copyable: false,
mergeable: false,
changeable: false,
equalities: [],
);
Expand All @@ -45,6 +48,7 @@ class ClassConfig {
stringify: annotation.get('stringify')?.boolValue ?? config.stringify,
buildable: annotation.get('buildable')?.boolValue ?? config.buildable,
copyable: annotation.get('copyable')?.boolValue ?? config.copyable,
mergeable: annotation.get('mergeable')?.boolValue ?? config.mergeable,
changeable: annotation.get('changeable')?.boolValue ?? config.changeable,
equalities: annotation.read('equalities').listValue,
);
Expand Down
23 changes: 16 additions & 7 deletions mek_data_class_generator/lib/src/configs/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ part 'options.g.dart';
@JsonSerializable()
class Options {
final Object? runOnlyIfTriggered;
@JsonKey(defaultValue: true)
final bool equatable;
@JsonKey(defaultValue: true)
final bool stringify;
@JsonKey(defaultValue: true)
final bool stringifyIfNull;
@JsonKey(defaultValue: false)
final bool buildable;
@JsonKey(defaultValue: false)
final bool copyable;
@JsonKey(defaultValue: false)
final bool mergeable;
@JsonKey(defaultValue: false)
final bool changeable;

const Options({
this.runOnlyIfTriggered,
this.equatable = true,
this.stringify = true,
this.stringifyIfNull = true,
this.buildable = false,
this.copyable = false,
this.changeable = false,
required this.runOnlyIfTriggered,
required this.equatable,
required this.stringify,
required this.stringifyIfNull,
required this.buildable,
required this.copyable,
required this.mergeable,
required this.changeable,
});

factory Options.fromJson(Map<String, dynamic> map) => _$OptionsFromJson(map);
Expand Down
7 changes: 6 additions & 1 deletion mek_data_class_generator/lib/src/configs/options.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 25 additions & 20 deletions mek_data_class_generator/lib/src/helpers/copy_with_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,35 @@ mixin CopyWithHelper on HelperCore {
Method _createCopyWithMethod(Iterable<FormalParameterElement> parameters) {
Code? body;
if (!element.isAbstract) {
body = lazyCode(() {
final buffer = StringBuffer('return ');
writeNewInstance(buffer, (parameter) {
if (parameterConfigOf(parameter).updatable) {
buffer.write('Unspecified.resolve(_self.');
buffer.write(parameterConfigOf(parameter).accessor);
buffer.write(', ');
buffer.write(parameter.displayName);
buffer.write(')');
} else {
buffer.write('_self.');
buffer.write(parameterConfigOf(parameter).accessor);
}
if (parameters.isEmpty) {
body = const Code('_self');
} else {
body = lazyCode(() {
final buffer = StringBuffer('return ');
writeNewInstance(buffer, (parameter) {
if (parameterConfigOf(parameter).updatable) {
buffer.write('Unspecified.resolve(_self.');
buffer.write(parameterConfigOf(parameter).accessor);
buffer.write(', ');
buffer.write(parameter.displayName);
buffer.write(')');
} else {
buffer.write('_self.');
buffer.write(parameterConfigOf(parameter).accessor);
}
});
buffer.write(';');
return Code(buffer.toString());
});
buffer.write(';');
return Code(buffer.toString());
});
}
}
return Method(
(b) => b
..returns = Reference(element.thisType.getDisplayString())
..name = 'copyWith'
..optionalParameters.addAll(
parameters.map(
(parameter) => Parameter(
parameters.map((parameter) {
return Parameter(
(b) => b
..named = true
..type = TypeReference(
Expand All @@ -53,9 +57,10 @@ mixin CopyWithHelper on HelperCore {
)
..name = parameter.displayName
..defaultTo = const Code('const Unspecified()'),
),
),
);
}),
)
..lambda = body is StaticCode
..body = body,
);
}
Expand Down
Loading