Skip to content

Commit b355311

Browse files
[native_doc_dartifier] Fix format issue + Adding more test cases (#2431)
Fix #2415.
1 parent 99ec517 commit b355311

File tree

10 files changed

+939
-47
lines changed

10 files changed

+939
-47
lines changed

pkgs/native_doc_dartifier/lib/src/prompts.dart

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ JNIgen handles various syntactic and semantic differences between Java/Kotlin an
7676
* Pass each interface method as a named closure argument within the `\$InterfaceName` factory constructor.
7777
* Example: `InterfaceName.implement(\$InterfaceName(method1: (param1) { /* ... */ }))`
7878
* **Reusable Implementations:**
79-
* If the Java/Kotlin code defines a class that `implements` an interface, translate it to a Dart `final class` that `with` (mixes in) the corresponding `\$`-prefixed interface (e.g., `final class MyPrinter with \$Runnable`).
80-
* The Dart class should override the interface methods as required.
81-
* Instantiate and pass this class to the `implement` factory: `InterfaceName.implement(MyClassImplementingInterface(...))`
79+
* If the Java/Kotlin code defines a class that `implements` an interface, To translate it to Dart make this class to be 'final class with' \$InterfaceName, as JNIgen make a mixin class \$InterfaceName that handles this, Don't make it to implements InterfaceName only use the mixin class.
8280
* **Asynchronous Listener Methods:**
8381
* For `void`-returning methods in implemented interfaces that should be non-blocking (listeners), explicitly set the `<methodName>\$async` parameter to `true` when using the inline implementation (e.g., `run\$async: true`).
8482
* If using a reusable class (mixin), override the `<methodName>\$async` getter to return `true` (e.g., `bool get run\$async => true;`).
@@ -108,14 +106,20 @@ $_schema
108106

109107
@override
110108
String getParsedResponse(String response) {
109+
response = response.replaceAll(r'\$', r'$').replaceAll('\\\'', '\'');
111110
print('Response: $response');
112-
final json = jsonDecode(response) as Map<String, dynamic>;
113-
if (!json.containsKey('dartCode')) {
111+
try {
112+
final json = jsonDecode(response) as Map<String, dynamic>;
113+
if (!json.containsKey('dartCode')) {
114+
return '';
115+
}
116+
final dartCode = json['dartCode'].toString();
117+
print('Dart Code: $dartCode');
118+
return dartCode;
119+
} catch (e) {
120+
print('Error decoding JSON: $e');
114121
return '';
115122
}
116-
final dartCode = json['dartCode'].toString();
117-
print('Dart Code: $dartCode');
118-
return dartCode;
119123
}
120124
}
121125

@@ -156,17 +160,23 @@ Output the response in JSON format:
156160

157161
@override
158162
FixResponse getParsedResponse(String response) {
163+
response = response.replaceAll(r'\$', r'$').replaceAll('\\\'', '\'');
159164
print('Response: $response');
160-
final json = jsonDecode(response) as Map<String, dynamic>;
161-
var mainDartCode = '';
162-
var tempDartCode = '';
163-
if (json.containsKey('mainDartCode')) {
164-
mainDartCode = json['mainDartCode'].toString();
165+
try {
166+
final json = jsonDecode(response) as Map<String, dynamic>;
167+
var mainDartCode = '';
168+
var tempDartCode = '';
169+
if (json.containsKey('mainDartCode')) {
170+
mainDartCode = json['mainDartCode'].toString();
171+
}
172+
if (json.containsKey('helperDartCode')) {
173+
tempDartCode = json['helperDartCode'].toString();
174+
}
175+
return FixResponse(mainCode: mainDartCode, helperCode: tempDartCode);
176+
} catch (e) {
177+
print('Error decoding JSON: $e');
178+
return FixResponse(mainCode: '', helperCode: '');
165179
}
166-
if (json.containsKey('helperDartCode')) {
167-
tempDartCode = json['helperDartCode'].toString();
168-
}
169-
return FixResponse(mainCode: mainDartCode, helperCode: tempDartCode);
170180
}
171181
}
172182

0 commit comments

Comments
 (0)