diff --git a/lib/src/exception/Exception.dart b/lib/src/exception/Exception.dart index 5f20347..f145428 100644 --- a/lib/src/exception/Exception.dart +++ b/lib/src/exception/Exception.dart @@ -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'; diff --git a/lib/src/exception/RequiredMethodNotFoundException.dart b/lib/src/exception/RequiredMethodNotFoundException.dart new file mode 100644 index 0000000..8b2bd81 --- /dev/null +++ b/lib/src/exception/RequiredMethodNotFoundException.dart @@ -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"; + } +} diff --git a/lib/src/helpers/reflect/ClassTransformer.dart b/lib/src/helpers/reflect/ClassTransformer.dart index bfc563d..7d83c4e 100644 --- a/lib/src/helpers/reflect/ClassTransformer.dart +++ b/lib/src/helpers/reflect/ClassTransformer.dart @@ -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 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; } diff --git a/lib/src/utils/SymbolUtil.dart b/lib/src/utils/SymbolUtil.dart new file mode 100644 index 0000000..239dea2 --- /dev/null +++ b/lib/src/utils/SymbolUtil.dart @@ -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, ''); + } +}