From f998dc01d68973eba18a9b2ea2d03b61c6ffb2fb Mon Sep 17 00:00:00 2001 From: Mohammed Salman Date: Fri, 21 Jan 2022 22:48:36 +0300 Subject: [PATCH 1/3] Add a flag for enabling named Arguments --- bin/esiur.dart | 25 +++++++++++++++----- lib/src/Proxy/TemplateGenerator.dart | 35 +++++++++++++++++++++------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/bin/esiur.dart b/bin/esiur.dart index 77a821c..7c8d856 100644 --- a/bin/esiur.dart +++ b/bin/esiur.dart @@ -24,7 +24,17 @@ void main(List arguments) async { ..addOption('username', abbr: 'u') ..addOption('password', abbr: 'p') ..addOption('dir', abbr: 'd') - ..addFlag("getx", abbr: 'x'); + ..addFlag( + "getx", + abbr: 'x', + defaultsTo: false, + help: "Generate apropriate getx bindings for resources", + ) + ..addFlag( + "namedargs", + help: + "Use named arguments instead of positional arguments for resource methods", + ); var results = parser.parse(arguments.skip(2)); @@ -35,11 +45,14 @@ void main(List arguments) async { //print("Username ${username} password ${password} dir ${dir}"); // make template - var destDir = await TemplateGenerator.getTemplate(link, - dir: dir, - username: username, - password: password, - getx: results['getx']); + var destDir = await TemplateGenerator.getTemplate( + link, + dir: dir, + username: username, + password: password, + getx: results['getx'], + namedArgs: results["namedargs"], + ); print("Generated directory `${destDir}`"); diff --git a/lib/src/Proxy/TemplateGenerator.dart b/lib/src/Proxy/TemplateGenerator.dart index 6cf5cb2..937fd9c 100644 --- a/lib/src/Proxy/TemplateGenerator.dart +++ b/lib/src/Proxy/TemplateGenerator.dart @@ -208,11 +208,14 @@ class TemplateGenerator { return v == null || v == ""; } - static Future getTemplate(String url, - {String? dir, - String? username, - String? password, - bool getx = false}) async { + static Future getTemplate( + String url, { + String? dir, + String? username, + String? password, + bool getx = false, + bool namedArgs = false, + }) async { try { if (!_urlRegex.hasMatch(url)) throw Exception("Invalid IIP URL"); @@ -266,7 +269,8 @@ class TemplateGenerator { var source = ""; if (tmp.type == TemplateType.Resource) { - source = makeImports(tmp) + generateClass(tmp, templates, getx: getx); + source = makeImports(tmp) + + generateClass(tmp, templates, getx: getx, namedArgs: namedArgs); } else if (tmp.type == TemplateType.Record) { source = makeImports(tmp) + generateRecord(tmp, templates); } @@ -308,8 +312,11 @@ class TemplateGenerator { } static String generateClass( - TypeTemplate template, List templates, - {bool getx = false}) { + TypeTemplate template, + List templates, { + bool getx = false, + bool namedArgs = false, + }) { var className = template.className.split('.').last; var rt = StringBuffer(); @@ -345,10 +352,20 @@ class TemplateGenerator { template.functions.forEach((f) { var rtTypeName = getTypeName(template, f.returnType, templates, true); rt.write("AsyncReply<$rtTypeName> ${f.name}("); + if (f.arguments.isNotEmpty && namedArgs) { + rt.write("{"); + } + final typeSuffix = namedArgs ? "?" : ""; rt.write(f.arguments .map((x) => - getTypeName(template, x.type, templates, true) + " " + x.name) + getTypeName(template, x.type, templates, true) + + typeSuffix + + " " + + x.name) .join(",")); + if (f.arguments.isNotEmpty && namedArgs) { + rt.write("}"); + } rt.writeln(") {"); rt.writeln("var rt = AsyncReply<$rtTypeName>();"); From 32df06dc440d8a710f666f7a0b409b27b28b20f3 Mon Sep 17 00:00:00 2001 From: Mohammed Salman Date: Fri, 21 Jan 2022 23:12:18 +0300 Subject: [PATCH 2/3] Dont make a param optional if it already is --- lib/src/Proxy/TemplateGenerator.dart | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/src/Proxy/TemplateGenerator.dart b/lib/src/Proxy/TemplateGenerator.dart index 937fd9c..3a641b9 100644 --- a/lib/src/Proxy/TemplateGenerator.dart +++ b/lib/src/Proxy/TemplateGenerator.dart @@ -355,14 +355,13 @@ class TemplateGenerator { if (f.arguments.isNotEmpty && namedArgs) { rt.write("{"); } - final typeSuffix = namedArgs ? "?" : ""; - rt.write(f.arguments - .map((x) => - getTypeName(template, x.type, templates, true) + - typeSuffix + - " " + - x.name) - .join(",")); + rt.write(f.arguments.map((x) { + final typeName = getTypeName(template, x.type, templates, true); + return typeName + + (namedArgs && !typeName.endsWith("?") ? "?" : "") + + " " + + x.name; + }).join(",")); if (f.arguments.isNotEmpty && namedArgs) { rt.write("}"); } From 599a9078b28d1063dd475743a152ac160a8e7b89 Mon Sep 17 00:00:00 2001 From: Mohammed Salman Date: Mon, 24 Jan 2022 20:15:36 +0300 Subject: [PATCH 3/3] Make strings nullable --- bin/esiur.dart | 4 ++++ lib/src/Proxy/TemplateGenerator.dart | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/bin/esiur.dart b/bin/esiur.dart index 7c8d856..d2790ee 100644 --- a/bin/esiur.dart +++ b/bin/esiur.dart @@ -72,6 +72,10 @@ printUsage() { print(""); print("Available commands:"); print("\tget-template\tGet a template from an IIP link."); + print("\t\tFlags:"); + print("\t\t\t-x, --getx\tGenerate apropriate getx bindings for resources."); + print( + "\t\t\t--namedargs\tUse named arguments instead of positional arguments for resource methods"); print("\tversion: print esiur version."); print(""); print("Global options:"); diff --git a/lib/src/Proxy/TemplateGenerator.dart b/lib/src/Proxy/TemplateGenerator.dart index 3a641b9..b27ca65 100644 --- a/lib/src/Proxy/TemplateGenerator.dart +++ b/lib/src/Proxy/TemplateGenerator.dart @@ -377,10 +377,12 @@ class TemplateGenerator { }); template.properties.forEach((p) { - var ptTypeName = getTypeName(template, p.valueType, templates, true); - rt.writeln("${ptTypeName} get ${p.name} { return get(${p.index}); }"); + final ptTypeName = getTypeName(template, p.valueType, templates, true); + final suffix = p.valueType.type == DataType.String ? "?" : ""; rt.writeln( - "set ${p.name}(${ptTypeName} value) { set(${p.index}, value); }"); + "$ptTypeName$suffix get ${p.name} { return get(${p.index}); }"); + rt.writeln( + "set ${p.name}($ptTypeName$suffix value) { set(${p.index}, value); }"); }); template.events.forEach((e) {