Skip to content

Commit

Permalink
Release 5.0.0 (#71)
Browse files Browse the repository at this point in the history
* removing global Settings object + testing global settings

* sort_constructors_first

* tests fix

* minor corrections

* version bump to 5.0.0 to avoid compatibility issues

* minor corrections
  • Loading branch information
numen31337 committed Dec 2, 2022
1 parent 0429dc5 commit 794f49e
Show file tree
Hide file tree
Showing 26 changed files with 239 additions and 121 deletions.
4 changes: 4 additions & 0 deletions copy_with_extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.0
* Allow positioned constructor parameters (thanks [@mrgnhnt96](https://github.com/mrgnhnt96)).
* Ability to define library defaults settings globally (thanks [@mrgnhnt96](https://github.com/mrgnhnt96)).

## 4.0.4
* Updating `analyzer` to `>=2.0.0 <6.0.0`

Expand Down
7 changes: 6 additions & 1 deletion copy_with_extension/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ include: package:flutter_lints/flutter.yaml
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
implicit-dynamic: false

linter:
rules:
cast_nullable_to_non_nullable: true
sort_constructors_first: true
15 changes: 6 additions & 9 deletions copy_with_extension/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@ import 'package:copy_with_extension/copy_with_extension.dart';
/// Or like this: `SimpleObject(id: "test").copyWith.id("new value")`.
@CopyWith(copyWithNull: true)
class SimpleObjectOldStyle {
const SimpleObjectOldStyle({required this.id, this.intValue});

final String id;
final int? intValue;

/// Make sure that constructor has named parameters (wrapped in curly braces)
const SimpleObjectOldStyle({required this.id, this.intValue});
}

/// Won't allow you to copy this object with a modified `id` field after object creation. It will always copy it from the original instance.
@CopyWith()
class SimpleObjectImmutableField {
const SimpleObjectImmutableField({this.id, this.intValue});

@CopyWithField(immutable: true)
final String? id;
final int? intValue;

/// Make sure that constructor has named parameters (wrapped in curly braces)
const SimpleObjectImmutableField({this.id, this.intValue});
}

/// Allows the use of a private constructor.
@CopyWith(constructor: "_")
class SimpleObjectPrivateConstructor {
const SimpleObjectPrivateConstructor._({this.id, this.intValue});

@CopyWithField(immutable: true)
final String? id;
final int? intValue;

/// Make sure that constructor has named parameters (wrapped in curly braces)
const SimpleObjectPrivateConstructor._({this.id, this.intValue});
}
12 changes: 6 additions & 6 deletions copy_with_extension/lib/copy_with_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library copy_with_extension;

import 'package:meta/meta_meta.dart';

/// Annotation used to indicate that the `copyWith` extension should be generated.
/// Annotation used to indicate that the `copyWith` extension should be generated for the given class.
@Target({TargetKind.classType})
class CopyWith {
const CopyWith({
Expand All @@ -12,22 +12,22 @@ class CopyWith {
this.constructor,
});

/// Set `copyWithNull` to `true` if you want to use `copyWithNull` function that allows you to nullify the fields. E.g. `myInstance.copyWithNull(id: true, name: true)`.
/// Set `copyWithNull` to `true` if you want to use `copyWithNull` function that allows you to nullify the fields. E.g. `myInstance.copyWithNull(id: true, name: true)`. Default is `false`.
final bool? copyWithNull;

/// Prevent the library from generating `copyWith` functions for individual fields e.g. `instance.copyWith.id("123")`. If you want to use only copyWith(...) function.
/// Prevent the library from generating `copyWith` functions for individual fields e.g. `instance.copyWith.id("123")`. If you want to use only copyWith(...) function. Default is `false`.
final bool? skipFields;

/// Set `constructor` if you want to use a named constructor. The generated fields will be derived from this constructor.
/// Set `constructor` if you want to use a named constructor. The generated fields will be derived from this constructor. If not set, the unnamed constructor is used.
final String? constructor;
}

/// Additional field related options for the `CopyWith`.
/// Field related options for the class's `CopyWith` annotation.
@Target({TargetKind.field})
class CopyWithField {
const CopyWithField({this.immutable});

/// Indicates that the field should be hidden in the generated `copyWith` method. By setting this flag to `true` the field will always be copied as it and excluded from `copyWith` interface.
/// Indicates that the field should be hidden in the generated `copyWith` method. By setting this flag to `true` the field will always be copied as it is and excluded from `copyWith` interface. Default is `false`.
final bool? immutable;
}

Expand Down
2 changes: 1 addition & 1 deletion copy_with_extension/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: copy_with_extension
version: 4.0.4
version: 5.0.0
description: Annotation for generating `copyWith` extensions code using `copy_with_extension_gen`.
homepage: https://github.com/numen31337/copy_with_extension/tree/master/copy_with_extension
repository: https://github.com/numen31337/copy_with_extension
Expand Down
4 changes: 4 additions & 0 deletions copy_with_extension_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.0
* Allow positioned constructor parameters (thanks [@mrgnhnt96](https://github.com/mrgnhnt96)).
* Ability to define library defaults settings globally (thanks [@mrgnhnt96](https://github.com/mrgnhnt96)).

## 4.0.4
* Updating `analyzer` to `>=2.0.0 <6.0.0`

Expand Down
11 changes: 5 additions & 6 deletions copy_with_extension_gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ myInstance.copyWithNull(fieldName: true, anotherField: true) // Nullify multiple

## Usage

#### In your `pubspec.yaml` file:
#### In your `pubspec.yaml` file
- Add to `dependencies` section `copy_with_extension: ^4.0.0`
- Add to `dev_dependencies` section `copy_with_extension_gen: ^4.0.0`
- Add to `dev_dependencies` section `build_runner: ^2.1.7`
Expand All @@ -41,7 +41,7 @@ dev_dependencies:
copy_with_extension_gen: ^4.0.0
```

#### Annotate your class with `CopyWith` annotation:
#### Annotate your class with `CopyWith` annotation

```dart
import 'package:copy_with_extension/copy_with_extension.dart';
Expand All @@ -59,9 +59,9 @@ class BasicClass {

Make sure that you set the part file as in the example above `part 'your_file_name.g.dart';`.

#### Launch code generation:
#### Launch code generation

```
```bash
flutter pub run build_runner build
```

Expand Down Expand Up @@ -131,12 +131,11 @@ To globally configure the library, you can add a `build.yaml` file to your proje
targets:
$default:
builders:

copy_with_extension_gen:
enabled: true
options:
copy_with_null: true # default is false
skip_fields: true # default is false
skip_fields: true # default is false
```

## How this library is better than `freezed`?
Expand Down
3 changes: 2 additions & 1 deletion copy_with_extension_gen/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ analyzer:

linter:
rules:
cast_nullable_to_non_nullable: true
cast_nullable_to_non_nullable: true
sort_constructors_first: true
15 changes: 6 additions & 9 deletions copy_with_extension_gen/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@ import 'package:copy_with_extension/copy_with_extension.dart';
/// Or like this: `SimpleObject(id: "test").copyWith.id("new value")`.
@CopyWith(copyWithNull: true)
class SimpleObjectOldStyle {
const SimpleObjectOldStyle({required this.id, this.intValue});

final String id;
final int? intValue;

/// Make sure that constructor has named parameters (wrapped in curly braces)
const SimpleObjectOldStyle({required this.id, this.intValue});
}

/// Won't allow you to copy this object with a modified `id` field after object creation. It will always copy it from the original instance.
@CopyWith()
class SimpleObjectImmutableField {
const SimpleObjectImmutableField({this.id, this.intValue});

@CopyWithField(immutable: true)
final String? id;
final int? intValue;

/// Make sure that constructor has named parameters (wrapped in curly braces)
const SimpleObjectImmutableField({this.id, this.intValue});
}

/// Allows the use of a private constructor.
@CopyWith(constructor: "_")
class SimpleObjectPrivateConstructor {
const SimpleObjectPrivateConstructor._({this.id, this.intValue});

@CopyWithField(immutable: true)
final String? id;
final int? intValue;

/// Make sure that constructor has named parameters (wrapped in curly braces)
const SimpleObjectPrivateConstructor._({this.id, this.intValue});
}
11 changes: 1 addition & 10 deletions copy_with_extension_gen/lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,16 @@ library copy_with_extension_gen.builder;

import 'package:build/build.dart' show Builder, BuilderOptions;
import 'package:copy_with_extension_gen/src/settings.dart';
import 'package:meta/meta.dart';
import 'package:source_gen/source_gen.dart' show SharedPartBuilder;
import 'package:copy_with_extension_gen/src/copy_with_generator.dart';

late Settings _settings;
Settings get settings => _settings;

@visibleForTesting
set settings(Settings value) => _settings = value;

/// Supports `package:build_runner` creation and configuration of
/// `copy_with_extension_gen`.
///
/// Not meant to be invoked by hand-authored code.
Builder copyWith(BuilderOptions config) {
_settings = Settings.fromConfig(config.config);

return SharedPartBuilder(
[CopyWithGenerator()],
[CopyWithGenerator(Settings.fromConfig(config.config))],
'copyWith',
);
}
1 change: 1 addition & 0 deletions copy_with_extension_gen/lib/src/copy_with_annotation.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:copy_with_extension/copy_with_extension.dart';

/// The internal representation of parameters entered by the library's user.
class CopyWithAnnotation implements CopyWith {
const CopyWithAnnotation({
required this.constructor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:copy_with_extension/copy_with_extension.dart';

/// The internal representation of parameters entered by the library's user.
class CopyWithFieldAnnotation implements CopyWithField {
const CopyWithFieldAnnotation({
required this.immutable,
Expand Down
24 changes: 12 additions & 12 deletions copy_with_extension_gen/lib/src/copy_with_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import 'package:build/build.dart' show BuildStep;
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:copy_with_extension_gen/src/field_info.dart';
import 'package:copy_with_extension_gen/src/helpers.dart';
import 'package:copy_with_extension_gen/src/settings.dart';
import 'package:source_gen/source_gen.dart'
show ConstantReader, GeneratorForAnnotation, InvalidGenerationSourceError;

/// A `Generator` for `package:build_runner`
class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {
CopyWithGenerator(this.settings) : super();

Settings settings;

@override
String generateForAnnotatedElement(
Element element,
Expand All @@ -23,7 +28,7 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {

final ClassElement classElement = element;
final privacyPrefix = element.isPrivate ? "_" : "";
final classAnnotation = readClassAnnotation(annotation);
final classAnnotation = readClassAnnotation(settings, annotation);

final sortedFields =
sortedConstructorFields(classElement, classAnnotation.constructor);
Expand All @@ -32,8 +37,6 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {
final typeAnnotation = classElement.name + typeParametersNames;

return '''
// ignore_for_file: unnecessary_non_null_assertion, duplicate_ignore
${_copyWithProxyPart(
classAnnotation.constructor,
classElement.name,
Expand Down Expand Up @@ -133,10 +136,10 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOf$type.copyWith(...)`.${skipFields ? "" : " Additionally contains functions for specific fields e.g. `instanceOf$type.copyWith.fieldName(...)`"}
class _\$${type}CWProxyImpl$typeParameters implements _\$${type}CWProxy$typeParameterNames {
final $type$typeParameterNames _value;
const _\$${type}CWProxyImpl(this._value);
final $type$typeParameterNames _value;
$nonNullableFunctions
@override
Expand Down Expand Up @@ -166,6 +169,7 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {
}
},
);

final paramsInput = sortedFields.fold<String>(
'',
(r, v) {
Expand All @@ -176,14 +180,10 @@ class CopyWithGenerator extends GeneratorForAnnotation<CopyWith> {
final nullCheckForNonNullable =
v.nullable ? "" : "|| ${v.name} == null";

var paramEntry = '$r ';

if (!v.isPositioned) {
paramEntry += '${v.name}:';
}

return paramEntry += '''
/// The `!` operator is needed to overcome a possible issue described here https://github.com/numen31337/copy_with_extension/pull/69#issue-1433703875
return '''$r ${v.isPositioned ? "" : '${v.name}:'}
${v.name} == const \$CopyWithPlaceholder() $nullCheckForNonNullable
${v.nullable ? '' : '// ignore: unnecessary_non_null_assertion'}
? _value.${v.name}${v.nullable ? '' : '!'}
// ignore: cast_nullable_to_non_nullable
: ${v.name} as ${v.type},''';
Expand Down
7 changes: 5 additions & 2 deletions copy_with_extension_gen/lib/src/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:analyzer/dart/element/element.dart'
show ClassElement, ConstructorElement;
import 'package:copy_with_extension_gen/builder.dart';
import 'package:copy_with_extension_gen/src/copy_with_annotation.dart';
import 'package:copy_with_extension_gen/src/field_info.dart';
import 'package:copy_with_extension_gen/src/settings.dart';
import 'package:source_gen/source_gen.dart'
show ConstantReader, InvalidGenerationSourceError;

Expand Down Expand Up @@ -54,7 +54,10 @@ List<FieldInfo> sortedConstructorFields(
}

/// Restores the `CopyWith` annotation provided by the user.
CopyWithAnnotation readClassAnnotation(ConstantReader reader) {
CopyWithAnnotation readClassAnnotation(
Settings settings,
ConstantReader reader,
) {
final generateCopyWithNull = reader.peek('copyWithNull')?.boolValue;
final skipFields = reader.peek('skipFields')?.boolValue;
final constructor = reader.peek('constructor')?.stringValue;
Expand Down
1 change: 1 addition & 0 deletions copy_with_extension_gen/lib/src/settings.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Global settings for the library. Allows users to globally define default values.
class Settings {
const Settings({
required this.copyWithNull,
Expand Down
4 changes: 2 additions & 2 deletions copy_with_extension_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: copy_with_extension_gen
version: 4.0.4
version: 5.0.0
description: Automatically generating `copyWith` extensions code for classes with `@CopyWith()` annotation.
repository: https://github.com/numen31337/copy_with_extension
homepage: https://github.com/numen31337/copy_with_extension/tree/master/copy_with_extension_gen
Expand All @@ -12,7 +12,7 @@ dependencies:
analyzer: ">=2.0.0 <6.0.0"
build: ^2.0.0
source_gen: ^1.0.0
copy_with_extension: ^4.0.0
copy_with_extension: ^5.0.0
meta: ^1.8.0

dev_dependencies:
Expand Down
3 changes: 0 additions & 3 deletions copy_with_extension_gen/pubspec_overrides.yaml

This file was deleted.

0 comments on commit 794f49e

Please sign in to comment.