Skip to content

Commit

Permalink
feat: Support non-freezed classes by ignoring previous requirement fo…
Browse files Browse the repository at this point in the history
…r factory ctors (#82)
  • Loading branch information
exaby73 authored Jul 13, 2024
1 parent 177fc83 commit fdbbf57
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/luthor_generator/build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
builders:
luthor:
import: "package:luthor_generator/builder.dart"
builder_factories: [ "luthorBuilder" ]
build_extensions: { ".dart": [ ".luthor.g.part" ] }
builder_factories: ["luthorBuilder"]
build_extensions: { ".dart": [".luthor.g.part"] }
auto_apply: dependents
build_to: cache
7 changes: 1 addition & 6 deletions packages/luthor_generator/example/lib/another_sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ class AnotherSample with _$AnotherSample {
@HasMin(8) required String password,
}) = _AnotherSample;

static SchemaValidationResult<AnotherSample> validate(
Map<String, dynamic> json,
) =>
_$AnotherSampleValidate(json);

factory AnotherSample.fromJson(Map<String, dynamic> json) =>
_$AnotherSampleFromJson(json);
}

void main() {
final json = {'id': 0};
final result = AnotherSample.validate(json);
final result = $AnotherSampleValidate(json);
switch (result) {
case SchemaValidationSuccess(data: final data):
print(data.validateSelf());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ part of 'another_sample.dart';
T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

AnotherSample _$AnotherSampleFromJson(Map<String, dynamic> json) {
return _AnotherSample.fromJson(json);
Expand Down Expand Up @@ -168,8 +168,7 @@ class _$AnotherSampleImpl implements _AnotherSample {
}

@override
// ignore: non_nullable_equals_parameter
bool operator ==(dynamic other) {
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$AnotherSampleImpl &&
Expand Down
6 changes: 3 additions & 3 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.

9 changes: 3 additions & 6 deletions packages/luthor_generator/example/lib/sample.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ class Sample with _$Sample {
required List<int> numbers,
}) = _Sample;

static SchemaValidationResult<Sample> validate(Map<String, dynamic> json) =>
_$SampleValidate(json);

factory Sample.fromJson(Map<String, dynamic> json) => _$SampleFromJson(json);
}

void main() {
final result = Sample.validate({});
final result = $SampleValidate({});
switch (result) {
case SchemaValidationError(errors: final errors):
print('Error: ');
Expand All @@ -65,7 +62,7 @@ void main() {
}

print('*' * 10);
final result2 = Sample.validate({
final result2 = $SampleValidate({
"minAndMaxInt": 1,
"minAndMaxDouble": 1.0,
"minAndMaxNumber": 1,
Expand All @@ -83,7 +80,7 @@ void main() {
}

print('*' * 10);
final result3 = Sample.validate({
final result3 = $SampleValidate({
"minAndMaxInt": 5,
"minAndMaxDouble": 5.0,
"minAndMaxNumber": 5,
Expand Down
4 changes: 2 additions & 2 deletions packages/luthor_generator/example/lib/sample.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ part of 'sample.dart';
T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods');
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

Sample _$SampleFromJson(Map<String, dynamic> json) {
return _Sample.fromJson(json);
Expand Down Expand Up @@ -546,7 +546,7 @@ class _$SampleImpl implements _Sample {
}

@override
bool operator ==(dynamic other) {
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$SampleImpl &&
Expand Down
12 changes: 7 additions & 5 deletions packages/luthor_generator/example/lib/sample.g.dart

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

21 changes: 21 additions & 0 deletions packages/luthor_generator/example/lib/without_freezed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:luthor/luthor.dart';

part 'without_freezed.g.dart';

@luthor
class WithoutFreezed {
final String name;
final int age;

const WithoutFreezed({
@isEmail required this.name,
required this.age,
});

factory WithoutFreezed.fromJson(Map<String, dynamic> json) {
return WithoutFreezed(
name: json['name'] as String,
age: json['age'] as int,
);
}
}
22 changes: 22 additions & 0 deletions packages/luthor_generator/example/lib/without_freezed.g.dart

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

11 changes: 7 additions & 4 deletions packages/luthor_generator/lib/generators/luthor_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ class LuthorGenerator extends GeneratorForAnnotation<Luthor> {
}

final name = element.name;
final constructor =
element.constructors.firstWhereOrNull((c) => c.isFactory);
if (constructor == null) {
final constructor = element.constructors.first;

final hasFromJsonCtor = element.constructors.any(
(element) => element.isFactory && element.name == 'fromJson',
);
if (hasFromJsonCtor) {
throw InvalidGenerationSourceError(
'Luthor can only be applied to classes with a factory constructor.',
'Luthor can only be applied to classes with a factory fromJson constructor',
element: element,
);
}
Expand Down

0 comments on commit fdbbf57

Please sign in to comment.