proxy method
Implementation
String proxy(Snippet snippet, String kn, String dn, ClassElement k,
List<MethodElement> ms) {
return '''
class MPI$kn implements $kn, Injectable, Introspectable {
final InvocationHandler _h;
late final Map<String, Method> _methods;
MPI$kn(this._h) {
_methods = {
${ms.map((m) {
final pms = m.parameters.where((x) {
return x.name != 'ctx';
}).toList();
final mmm = mpi(ConstantReader(macro.firstAnnotationOf(m)));
final method = m.nameLength < 2
? m.name
: '${m.name[0].toUpperCase()}${m.name.substring(1)}';
final pname = '_Mesh$dn${method}Parameters';
final rname = '_Mesh$dn${method}Returns';
snippet.init('ark.constructor<$pname>(() => $pname());');
snippet.init('ark.constructor<$rname>(() => $rname());');
final List<Pair<String, String>> rs = [
const Pair('code', 'String'),
const Pair('message', 'String'),
const Pair('cause', 'Cause'),
Pair('content', types(m.returnType)),
];
for (var idx = 0; idx < rs.length; idx++) {
final p = rs[idx];
final i = index(idx, p.a, m.returnType, annotation(Index), kn: p.b);
final a = accessor(rname, i, p.a);
snippet.init('''ark.declare<$rname>('${p.a}', $a);''');
}
for (var idx = 0; idx < pms.length; idx++) {
final p = pms[idx];
final i = index(idx, p.name, p.type, annotation(Index, e: p));
final a = accessor(pname, i, p.name);
snippet.init('ark.declare<$pname>(\'${p.name}\', $a);');
}
final i = index(-1, 'attachments', m.type, annotation(Index),
kn: 'Types(Map,[String, String],()=><String,String>{})');
final a = accessor(pname, i, 'attachments');
snippet.init('ark.declare<$pname>(\'attachments\', $a);');
snippet.define(parameters(pname, pms));
snippet.define(returns(rname, m.returnType));
return ''''${m.name}': Method({$mmm}, ${k.name}, $pname, $rname),''';
}).join('\n')}
};
}
@override
void ioc() {
_h.ioc();
}
@override
Map<String, Introspector> methods() {
return _methods;
}
${ms.map((m) {
final rt = m.returnType.getDisplayString();
final pt = m.parameters.map((p) => p.getDisplayString()).join(', ');
final pn = m.parameters
.where((x) {
return x.name != 'ctx';
})
.map((p) => p.name)
.join(', ');
return '''
@override
$rt ${m.name}($pt) async {
return await _h.invoke(this, _methods['${m.name}']!, [$pn], ctx);
}
''';
}).join('\n')}
}
''';
}