Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghaiyang committed Dec 10, 2019
1 parent 7ca7dc0 commit 1685471
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/src/exception/Exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'package:Q/src/exception/MaxUploadSizeExceededException.dart';
export 'package:Q/src/exception/NoMatchRequestResolverException.dart';
export 'package:Q/src/exception/RedirectNotFoundException.dart';
export 'package:Q/src/exception/RequestParamRequiredException.dart';
export 'package:Q/src/exception/RequiredMethodNotFoundException.dart';
export 'package:Q/src/exception/RouterNotFoundException.dart';
export 'package:Q/src/exception/RouterNotFoundOfRouterChainException.dart';
export 'package:Q/src/exception/SizeUnitParseException.dart';
Expand Down
16 changes: 16 additions & 0 deletions lib/src/exception/RequiredMethodNotFoundException.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class RequiredMethodNotFoundException extends Exception {
factory RequiredMethodNotFoundException({String message, String name}) => _RequiredMethodNotFoundException(message: message, name: name);
}

class _RequiredMethodNotFoundException implements RequiredMethodNotFoundException {
final message;

final String name;

_RequiredMethodNotFoundException({this.message, this.name});

String toString() {
if (message == null) return "Exception: required method not found: '${this.name}'";
return "Exception: $message";
}
}
26 changes: 20 additions & 6 deletions lib/src/helpers/reflect/ClassTransformer.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import 'dart:mirrors';

import 'package:Q/Q.dart';
import 'package:Q/src/utils/SymbolUtil.dart';

class ClassTransformer {
static String MODEL_METHOD = '_model';

// TODO reflect _.model method
static dynamic fromMap(Map data, Type clazz) async {
ClassMirror classMirror = reflectClass(clazz);
Map<Symbol, DeclarationMirror> declarations = classMirror.declarations;
InstanceMirror instanceMirror = classMirror.newInstance(Symbol.empty, []);
Symbol MODEL_METHOD_SYMBOL = Symbol('${SymbolUtil.toChars(classMirror.simpleName)}.${MODEL_METHOD}');
if (!declarations.keys.map((Symbol key) {
return key.toString();
}).contains(MODEL_METHOD_SYMBOL.toString())) {
throw RequiredMethodNotFoundException(name: MODEL_METHOD_SYMBOL.toString());
}
// why method parameters is null
MethodMirror modelMethodMirror;
for (Symbol key in declarations.keys) {
if (key.toString().endsWith(MODEL_METHOD_SYMBOL.toString())) {
modelMethodMirror = declarations[key];
}
}
List positionalArguments = List();
InstanceMirror instanceMirror = classMirror.newInstance(MODEL_METHOD_SYMBOL, []);
for (var key in data.keys) {
dynamic value = data[key];
ClassMirror valueMirror = reflect(value).type;
Symbol fieldName = Symbol(key);
// check field name
if (declarations.containsKey(fieldName)) {
// check field type
instanceMirror.setField(fieldName, value);
}
}
return instanceMirror.reflectee;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/src/utils/SymbolUtil.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SymbolUtil {
static toChars(Symbol symbol) {
String str = symbol.toString();
str = str.replaceRange(str.length - 2, str.length, '');
return str.replaceRange(0, 8, '');
}
}

0 comments on commit 1685471

Please # to comment.