Skip to content

Commit

Permalink
feat(luthor_generator): Support of freezed's Default (#87)
Browse files Browse the repository at this point in the history
* feat(luthor_generator): Add support for freezed's Default

* fix(luthor_generator): Missing logical negation in fromJson check
  • Loading branch information
kasefuchs authored Jul 14, 2024
1 parent e639cdb commit 360f12d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/luthor_generator/example/lib/another_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AnotherSample with _$AnotherSample {
@JsonKey(name: 'full_name') String? name,
@IsEmail(message: "Invalid email") required String email,
@HasMin(8) required String password,
@Default('user') String type,
}) = _AnotherSample;

factory AnotherSample.fromJson(Map<String, dynamic> json) =>
Expand Down
56 changes: 45 additions & 11 deletions packages/luthor_generator/example/lib/another_sample.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ mixin _$AnotherSample {
String get email => throw _privateConstructorUsedError;
@HasMin(8)
String get password => throw _privateConstructorUsedError;
String get type => throw _privateConstructorUsedError;

/// Serializes this AnotherSample to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)

/// Create a copy of AnotherSample
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$AnotherSampleCopyWith<AnotherSample> get copyWith =>
throw _privateConstructorUsedError;
}
Expand All @@ -44,7 +49,8 @@ abstract class $AnotherSampleCopyWith<$Res> {
{int id,
@JsonKey(name: 'full_name') String? name,
@IsEmail(message: "Invalid email") String email,
@HasMin(8) String password});
@HasMin(8) String password,
String type});
}

/// @nodoc
Expand All @@ -57,13 +63,16 @@ class _$AnotherSampleCopyWithImpl<$Res, $Val extends AnotherSample>
// ignore: unused_field
final $Res Function($Val) _then;

/// Create a copy of AnotherSample
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = null,
Object? name = freezed,
Object? email = null,
Object? password = null,
Object? type = null,
}) {
return _then(_value.copyWith(
id: null == id
Expand All @@ -82,6 +91,10 @@ class _$AnotherSampleCopyWithImpl<$Res, $Val extends AnotherSample>
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as String,
type: null == type
? _value.type
: type // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}
}
Expand All @@ -98,7 +111,8 @@ abstract class _$$AnotherSampleImplCopyWith<$Res>
{int id,
@JsonKey(name: 'full_name') String? name,
@IsEmail(message: "Invalid email") String email,
@HasMin(8) String password});
@HasMin(8) String password,
String type});
}

/// @nodoc
Expand All @@ -109,13 +123,16 @@ class __$$AnotherSampleImplCopyWithImpl<$Res>
_$AnotherSampleImpl _value, $Res Function(_$AnotherSampleImpl) _then)
: super(_value, _then);

/// Create a copy of AnotherSample
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = null,
Object? name = freezed,
Object? email = null,
Object? password = null,
Object? type = null,
}) {
return _then(_$AnotherSampleImpl(
id: null == id
Expand All @@ -134,6 +151,10 @@ class __$$AnotherSampleImplCopyWithImpl<$Res>
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as String,
type: null == type
? _value.type
: type // ignore: cast_nullable_to_non_nullable
as String,
));
}
}
Expand All @@ -145,7 +166,8 @@ class _$AnotherSampleImpl implements _AnotherSample {
{required this.id,
@JsonKey(name: 'full_name') this.name,
@IsEmail(message: "Invalid email") required this.email,
@HasMin(8) required this.password});
@HasMin(8) required this.password,
this.type = 'user'});

factory _$AnotherSampleImpl.fromJson(Map<String, dynamic> json) =>
_$$AnotherSampleImplFromJson(json);
Expand All @@ -161,10 +183,13 @@ class _$AnotherSampleImpl implements _AnotherSample {
@override
@HasMin(8)
final String password;
@override
@JsonKey()
final String type;

@override
String toString() {
return 'AnotherSample(id: $id, name: $name, email: $email, password: $password)';
return 'AnotherSample(id: $id, name: $name, email: $email, password: $password, type: $type)';
}

@override
Expand All @@ -176,14 +201,17 @@ class _$AnotherSampleImpl implements _AnotherSample {
(identical(other.name, name) || other.name == name) &&
(identical(other.email, email) || other.email == email) &&
(identical(other.password, password) ||
other.password == password));
other.password == password) &&
(identical(other.type, type) || other.type == type));
}

@JsonKey(ignore: true)
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, id, name, email, password);
int get hashCode => Object.hash(runtimeType, id, name, email, password, type);

@JsonKey(ignore: true)
/// Create a copy of AnotherSample
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$AnotherSampleImplCopyWith<_$AnotherSampleImpl> get copyWith =>
Expand All @@ -202,7 +230,8 @@ abstract class _AnotherSample implements AnotherSample {
{required final int id,
@JsonKey(name: 'full_name') final String? name,
@IsEmail(message: "Invalid email") required final String email,
@HasMin(8) required final String password}) = _$AnotherSampleImpl;
@HasMin(8) required final String password,
final String type}) = _$AnotherSampleImpl;

factory _AnotherSample.fromJson(Map<String, dynamic> json) =
_$AnotherSampleImpl.fromJson;
Expand All @@ -219,7 +248,12 @@ abstract class _AnotherSample implements AnotherSample {
@HasMin(8)
String get password;
@override
@JsonKey(ignore: true)
String get type;

/// Create a copy of AnotherSample
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$AnotherSampleImplCopyWith<_$AnotherSampleImpl> get copyWith =>
throw _privateConstructorUsedError;
}
3 changes: 3 additions & 0 deletions packages/luthor_generator/example/lib/another_sample.g.dart

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

28 changes: 22 additions & 6 deletions packages/luthor_generator/example/lib/sample.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ mixin _$Sample {
String get custom => throw _privateConstructorUsedError;
List<int> get numbers => throw _privateConstructorUsedError;

/// Serializes this Sample to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)

/// Create a copy of Sample
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$SampleCopyWith<Sample> get copyWith => throw _privateConstructorUsedError;
}

Expand Down Expand Up @@ -114,6 +118,8 @@ class _$SampleCopyWithImpl<$Res, $Val extends Sample>
// ignore: unused_field
final $Res Function($Val) _then;

/// Create a copy of Sample
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Expand Down Expand Up @@ -242,6 +248,8 @@ class _$SampleCopyWithImpl<$Res, $Val extends Sample>
) as $Val);
}

/// Create a copy of Sample
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$AnotherSampleCopyWith<$Res> get anotherSample {
Expand Down Expand Up @@ -296,6 +304,8 @@ class __$$SampleImplCopyWithImpl<$Res>
_$SampleImpl _value, $Res Function(_$SampleImpl) _then)
: super(_value, _then);

/// Create a copy of Sample
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Expand Down Expand Up @@ -594,7 +604,7 @@ class _$SampleImpl implements _Sample {
const DeepCollectionEquality().equals(other._numbers, _numbers));
}

@JsonKey(ignore: true)
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hashAll([
runtimeType,
Expand Down Expand Up @@ -624,7 +634,9 @@ class _$SampleImpl implements _Sample {
const DeepCollectionEquality().hash(_numbers)
]);

@JsonKey(ignore: true)
/// Create a copy of Sample
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$SampleImplCopyWith<_$SampleImpl> get copyWith =>
Expand Down Expand Up @@ -679,9 +691,10 @@ abstract class _Sample implements Sample {
@override
int get intValue;
@override
List<String> get listValue;
@override // The following will work with luthor_generator, but not with json_serializable
List<String>
get listValue; // The following will work with luthor_generator, but not with json_serializable
// Null? nullValue,
@override
num get numValue;
@override
String get stringValue;
Expand Down Expand Up @@ -737,8 +750,11 @@ abstract class _Sample implements Sample {
String get custom;
@override
List<int> get numbers;

/// Create a copy of Sample
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(ignore: true)
@JsonKey(includeFromJson: false, includeToJson: false)
_$$SampleImplCopyWith<_$SampleImpl> get copyWith =>
throw _privateConstructorUsedError;
}
3 changes: 2 additions & 1 deletion packages/luthor_generator/lib/checkers.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:luthor/luthor.dart';
import 'package:source_gen/source_gen.dart';

const luthorChecker = TypeChecker.fromRuntime(Luthor);
const jsonKeyChecker = TypeChecker.fromRuntime(JsonKey);
const defaultChecker = TypeChecker.fromRuntime(Default);

const isEmailChecker = TypeChecker.fromRuntime(IsEmail);
const isDateTimeChecker = TypeChecker.fromRuntime(IsDateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ String getValidations(ParameterElement param) {

getCustomValidations(param, buffer);

if (param.type is! DynamicType && !isNullable) buffer.write('.required()');
final hasDefaultAnnotation = getAnnotation(defaultChecker, param) != null;
if (param.type is! DynamicType && !isNullable && !hasDefaultAnnotation) {
buffer.write('.required()');
}

return buffer.toString();
}
Expand Down
1 change: 1 addition & 0 deletions packages/luthor_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
analyzer: ^6.3.0
build: ^2.4.1
collection: ^1.18.0
freezed_annotation: ^2.4.3
json_annotation: ^4.8.1
luthor: ^0.4.2+1
source_gen: ^1.5.0
Expand Down

0 comments on commit 360f12d

Please sign in to comment.