Skip to content

Commit

Permalink
fix: Added ModelRef addition mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Dec 16, 2022
1 parent 2c83d9c commit 3fdc19a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/masamune_builder/lib/model/model_class.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of masamune_builder;

final _regExpModelRef = RegExp(r"ModelRef<([^>]+)>");
final _regExpModelRef = RegExp(r"ModelRef(Base)?<([^>]+)>");
final _regExpRef = RegExp(r"(.+)Ref");

List<Spec> modelClass(
Expand Down Expand Up @@ -94,32 +94,33 @@ List<Spec> modelClass(
..name = "builder"
..lambda = true
..type = MethodType.getter
..returns = Reference("List<ModelRefBuilder<${model.name}>>")
..returns =
Reference("List<ModelRefBuilderBase<${model.name}>>")
..annotations.addAll([const Reference("override")])
..body = Code("[${referencable.map((e) {
if (e.type.toString().endsWith("Ref")) {
final match = _regExpRef.firstMatch(e.type.toString());
if (match == null) {
throw Exception(
"@refParam can only be given to ModelRef<T> types. \r\n\r\n${e.type} ${e.name}",
"@refParam can only be given to ModelRef<T> / ModelRefBase<T>? / XXXRef types. \r\n\r\n${e.type} ${e.name}",
);
}
final doc = "\$${match.group(1)}Document";
return "ModelRefBuilder( modelRef: (value) => value.${e.name}, document: (modelQuery) => $doc(modelQuery), value: (value, doc) => value.copyWith( ${e.name}: doc as ${match.group(1)}Ref ),)";
return "ModelRefBuilder( modelRef: (value) => value.${e.name}, document: (modelQuery) => $doc(modelQuery), value: (value, doc) => value.copyWith( ${e.name}: doc ),)";
} else {
if (!e.type.toString().endsWith("?")) {
throw Exception(
"ModelRef<T> must be nullable. \r\n\r\n${e.type} ${e.name}",
"ModelRefBase<T> must be nullable. \r\n\r\n${e.type} ${e.name}",
);
}
final match = _regExpModelRef.firstMatch(e.type.toString());
if (match == null) {
throw Exception(
"@refParam can only be given to ModelRef<T> types. \r\n\r\n${e.type} ${e.name}",
"@refParam can only be given to ModelRef<T> / ModelRefBase<T>? / XXXRef types. \r\n\r\n${e.type} ${e.name}",
);
}
final doc = "\$${match.group(1)}Document";
return "ModelRefBuilder( modelRef: (value) => value.${e.name}, document: (modelQuery) => $doc(modelQuery), value: (value, doc) => value.copyWith( ${e.name}: doc as ${match.group(1)}Ref ),)";
final doc = "\$${match.group(2)}Document";
return "ModelRefBuilder( modelRef: (value) => value.${e.name}, document: (modelQuery) => $doc(modelQuery), value: (value, doc) => value.copyWith( ${e.name}: doc ),)";
}
}).join(",")}]"),
),
Expand Down

0 comments on commit 3fdc19a

Please sign in to comment.