forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkotlin_generator.dart
More file actions
978 lines (901 loc) · 33.3 KB
/
kotlin_generator.dart
File metadata and controls
978 lines (901 loc) · 33.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'ast.dart';
import 'functional.dart';
import 'generator.dart';
import 'generator_tools.dart';
import 'pigeon_lib.dart' show TaskQueueType;
/// Documentation open symbol.
const String _docCommentPrefix = '/**';
/// Documentation continuation symbol.
const String _docCommentContinuation = ' *';
/// Documentation close symbol.
const String _docCommentSuffix = ' */';
/// Documentation comment spec.
const DocumentCommentSpecification _docCommentSpec =
DocumentCommentSpecification(
_docCommentPrefix,
closeCommentToken: _docCommentSuffix,
blockContinuationToken: _docCommentContinuation,
);
/// Options that control how Kotlin code will be generated.
class KotlinOptions {
/// Creates a [KotlinOptions] object
const KotlinOptions({
this.package,
this.copyrightHeader,
this.errorClassName,
});
/// The package where the generated class will live.
final String? package;
/// A copyright header that will get prepended to generated code.
final Iterable<String>? copyrightHeader;
/// The name of the error class used for passing custom error parameters.
final String? errorClassName;
/// Creates a [KotlinOptions] from a Map representation where:
/// `x = KotlinOptions.fromMap(x.toMap())`.
static KotlinOptions fromMap(Map<String, Object> map) {
return KotlinOptions(
package: map['package'] as String?,
copyrightHeader: map['copyrightHeader'] as Iterable<String>?,
errorClassName: map['errorClassName'] as String?,
);
}
/// Converts a [KotlinOptions] to a Map representation where:
/// `x = KotlinOptions.fromMap(x.toMap())`.
Map<String, Object> toMap() {
final Map<String, Object> result = <String, Object>{
if (package != null) 'package': package!,
if (copyrightHeader != null) 'copyrightHeader': copyrightHeader!,
if (errorClassName != null) 'errorClassName': errorClassName!,
};
return result;
}
/// Overrides any non-null parameters from [options] into this to make a new
/// [KotlinOptions].
KotlinOptions merge(KotlinOptions options) {
return KotlinOptions.fromMap(mergeMaps(toMap(), options.toMap()));
}
}
/// Class that manages all Kotlin code generation.
class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
/// Instantiates a Kotlin Generator.
const KotlinGenerator();
@override
void writeFilePrologue(
KotlinOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
if (generatorOptions.copyrightHeader != null) {
addLines(indent, generatorOptions.copyrightHeader!, linePrefix: '// ');
}
indent.writeln('// ${getGeneratedCodeWarning()}');
indent.writeln('// $seeAlsoWarning');
}
@override
void writeFileImports(
KotlinOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
indent.newln();
if (generatorOptions.package != null) {
indent.writeln('package ${generatorOptions.package}');
}
indent.newln();
indent.writeln('import android.util.Log');
indent.writeln('import io.flutter.plugin.common.BasicMessageChannel');
indent.writeln('import io.flutter.plugin.common.BinaryMessenger');
indent.writeln('import io.flutter.plugin.common.MessageCodec');
indent.writeln('import io.flutter.plugin.common.StandardMessageCodec');
indent.writeln('import java.io.ByteArrayOutputStream');
indent.writeln('import java.nio.ByteBuffer');
}
@override
void writeEnum(
KotlinOptions generatorOptions,
Root root,
Indent indent,
Enum anEnum, {
required String dartPackageName,
}) {
indent.newln();
addDocumentationComments(
indent, anEnum.documentationComments, _docCommentSpec);
indent.write('enum class ${anEnum.name}(val raw: Int) ');
indent.addScoped('{', '}', () {
enumerate(anEnum.members, (int index, final EnumMember member) {
addDocumentationComments(
indent, member.documentationComments, _docCommentSpec);
final RegExp exp = RegExp('[a-z][A-Z]');
final String snakedMemberName = member.name.replaceAllMapped(exp,
(Match match) => '${match.group(0)?[0]}_${match.group(0)?[1]}');
indent.write('${snakedMemberName.toUpperCase()}($index)');
if (index != anEnum.members.length - 1) {
indent.addln(',');
} else {
indent.addln(';');
}
});
indent.newln();
indent.write('companion object ');
indent.addScoped('{', '}', () {
indent.write('fun ofRaw(raw: Int): ${anEnum.name}? ');
indent.addScoped('{', '}', () {
indent.writeln('return values().firstOrNull { it.raw == raw }');
});
});
});
}
@override
void writeDataClass(
KotlinOptions generatorOptions,
Root root,
Indent indent,
Class classDefinition, {
required String dartPackageName,
}) {
const List<String> generatedMessages = <String>[
' Generated class from Pigeon that represents data sent in messages.'
];
indent.newln();
addDocumentationComments(
indent, classDefinition.documentationComments, _docCommentSpec,
generatorComments: generatedMessages);
indent.write('data class ${classDefinition.name} ');
indent.addScoped('(', '', () {
for (final NamedType element
in getFieldsInSerializationOrder(classDefinition)) {
_writeClassField(indent, element);
if (getFieldsInSerializationOrder(classDefinition).last != element) {
indent.addln(',');
} else {
indent.newln();
}
}
});
indent.addScoped(') {', '}', () {
writeClassDecode(
generatorOptions,
root,
indent,
classDefinition,
dartPackageName: dartPackageName,
);
writeClassEncode(
generatorOptions,
root,
indent,
classDefinition,
dartPackageName: dartPackageName,
);
});
}
@override
void writeClassEncode(
KotlinOptions generatorOptions,
Root root,
Indent indent,
Class classDefinition, {
required String dartPackageName,
}) {
indent.write('fun toList(): List<Any?> ');
indent.addScoped('{', '}', () {
indent.write('return listOf<Any?>');
indent.addScoped('(', ')', () {
for (final NamedType field
in getFieldsInSerializationOrder(classDefinition)) {
final HostDatatype hostDatatype = _getHostDatatype(root, field);
String toWriteValue = '';
final String fieldName = field.name;
final String safeCall = field.type.isNullable ? '?' : '';
if (field.type.isClass) {
toWriteValue = '$fieldName$safeCall.toList()';
} else if (!hostDatatype.isBuiltin && field.type.isEnum) {
toWriteValue = '$fieldName$safeCall.raw';
} else {
toWriteValue = fieldName;
}
indent.writeln('$toWriteValue,');
}
});
});
}
@override
void writeClassDecode(
KotlinOptions generatorOptions,
Root root,
Indent indent,
Class classDefinition, {
required String dartPackageName,
}) {
final String className = classDefinition.name;
indent.write('companion object ');
indent.addScoped('{', '}', () {
indent.writeln('@Suppress("UNCHECKED_CAST")');
indent.write('fun fromList(list: List<Any?>): $className ');
indent.addScoped('{', '}', () {
enumerate(getFieldsInSerializationOrder(classDefinition),
(int index, final NamedType field) {
final String listValue = 'list[$index]';
final String fieldType = _kotlinTypeForDartType(field.type);
if (field.type.isNullable) {
if (field.type.isClass) {
indent.write('val ${field.name}: $fieldType? = ');
indent.add('($listValue as List<Any?>?)?.let ');
indent.addScoped('{', '}', () {
indent.writeln('$fieldType.fromList(it)');
});
} else if (field.type.isEnum) {
indent.write('val ${field.name}: $fieldType? = ');
indent.add('($listValue as Int?)?.let ');
indent.addScoped('{', '}', () {
indent.writeln('$fieldType.ofRaw(it)');
});
} else {
indent.writeln(
'val ${field.name} = ${_cast(indent, listValue, type: field.type)}');
}
} else {
if (field.type.isClass) {
indent.writeln(
'val ${field.name} = $fieldType.fromList($listValue as List<Any?>)');
} else if (field.type.isEnum) {
indent.writeln(
'val ${field.name} = $fieldType.ofRaw($listValue as Int)!!');
} else {
indent.writeln(
'val ${field.name} = ${_cast(indent, listValue, type: field.type)}');
}
}
});
indent.write('return $className(');
for (final NamedType field
in getFieldsInSerializationOrder(classDefinition)) {
final String comma =
getFieldsInSerializationOrder(classDefinition).last == field
? ''
: ', ';
indent.add('${field.name}$comma');
}
indent.addln(')');
});
});
}
void _writeClassField(Indent indent, NamedType field) {
addDocumentationComments(
indent, field.documentationComments, _docCommentSpec);
indent.write(
'val ${field.name}: ${_nullsafeKotlinTypeForDartType(field.type)}');
final String defaultNil = field.type.isNullable ? ' = null' : '';
indent.add(defaultNil);
}
@override
void writeApis(
KotlinOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
if (root.apis.any((Api api) =>
api.location == ApiLocation.host &&
api.methods.any((Method it) => it.isAsynchronous))) {
indent.newln();
}
super.writeApis(generatorOptions, root, indent,
dartPackageName: dartPackageName);
}
/// Writes the code for a flutter [Api], [api].
/// Example:
/// class Foo(private val binaryMessenger: BinaryMessenger) {
/// fun add(x: Int, y: Int, callback: (Int?) -> Unit) {...}
/// }
@override
void writeFlutterApi(
KotlinOptions generatorOptions,
Root root,
Indent indent,
Api api, {
required String dartPackageName,
}) {
assert(api.location == ApiLocation.flutter);
final bool isCustomCodec = getCodecClasses(api, root).isNotEmpty;
if (isCustomCodec) {
_writeCodec(indent, api, root);
}
const List<String> generatedMessages = <String>[
' Generated class from Pigeon that represents Flutter messages that can be called from Kotlin.'
];
addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
generatorComments: generatedMessages);
final String apiName = api.name;
indent.writeln('@Suppress("UNCHECKED_CAST")');
indent
.write('class $apiName(private val binaryMessenger: BinaryMessenger) ');
indent.addScoped('{', '}', () {
indent.write('companion object ');
indent.addScoped('{', '}', () {
indent.writeln('/** The codec used by $apiName. */');
indent.write('val codec: MessageCodec<Any?> by lazy ');
indent.addScoped('{', '}', () {
if (isCustomCodec) {
indent.writeln(_getCodecName(api));
} else {
indent.writeln('StandardMessageCodec()');
}
});
});
final String errorClassName = _getErrorClassName(generatorOptions);
for (final Method method in api.methods) {
_writeFlutterMethod(
indent,
name: method.name,
parameters: method.parameters,
returnType: method.returnType,
channelName: makeChannelName(api, method, dartPackageName),
documentationComments: method.documentationComments,
errorClassName: errorClassName,
dartPackageName: dartPackageName,
);
}
});
}
/// Write the kotlin code that represents a host [Api], [api].
/// Example:
/// interface Foo {
/// Int add(x: Int, y: Int);
/// companion object {
/// fun setUp(binaryMessenger: BinaryMessenger, api: Api) {...}
/// }
/// }
///
@override
void writeHostApi(
KotlinOptions generatorOptions,
Root root,
Indent indent,
Api api, {
required String dartPackageName,
}) {
assert(api.location == ApiLocation.host);
final String apiName = api.name;
final bool isCustomCodec = getCodecClasses(api, root).isNotEmpty;
if (isCustomCodec) {
_writeCodec(indent, api, root);
}
const List<String> generatedMessages = <String>[
' Generated interface from Pigeon that represents a handler of messages from Flutter.'
];
addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
generatorComments: generatedMessages);
indent.write('interface $apiName ');
indent.addScoped('{', '}', () {
for (final Method method in api.methods) {
_writeMethodDeclaration(
indent,
name: method.name,
documentationComments: method.documentationComments,
returnType: method.returnType,
parameters: method.parameters,
isAsynchronous: method.isAsynchronous,
);
}
indent.newln();
indent.write('companion object ');
indent.addScoped('{', '}', () {
indent.writeln('/** The codec used by $apiName. */');
indent.write('val codec: MessageCodec<Any?> by lazy ');
indent.addScoped('{', '}', () {
if (isCustomCodec) {
indent.writeln(_getCodecName(api));
} else {
indent.writeln('StandardMessageCodec()');
}
});
indent.writeln(
'/** Sets up an instance of `$apiName` to handle messages through the `binaryMessenger`. */');
indent.writeln('@Suppress("UNCHECKED_CAST")');
indent.write(
'fun setUp(binaryMessenger: BinaryMessenger, api: $apiName?) ');
indent.addScoped('{', '}', () {
for (final Method method in api.methods) {
_writeHostMethodMessageHandler(
indent,
api: api,
name: method.name,
channelName: makeChannelName(api, method, dartPackageName),
taskQueueType: method.taskQueueType,
parameters: method.parameters,
returnType: method.returnType,
isAsynchronous: method.isAsynchronous,
);
}
});
});
});
}
/// Writes the codec class that will be used by [api].
/// Example:
/// private static class FooCodec extends StandardMessageCodec {...}
void _writeCodec(Indent indent, Api api, Root root) {
assert(getCodecClasses(api, root).isNotEmpty);
final Iterable<EnumeratedClass> codecClasses = getCodecClasses(api, root);
final String codecName = _getCodecName(api);
indent.writeln('@Suppress("UNCHECKED_CAST")');
indent.write('private object $codecName : StandardMessageCodec() ');
indent.addScoped('{', '}', () {
indent.write(
'override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? ');
indent.addScoped('{', '}', () {
indent.write('return when (type) ');
indent.addScoped('{', '}', () {
for (final EnumeratedClass customClass in codecClasses) {
indent.write('${customClass.enumeration}.toByte() -> ');
indent.addScoped('{', '}', () {
indent.write('return (readValue(buffer) as? List<Any?>)?.let ');
indent.addScoped('{', '}', () {
indent.writeln('${customClass.name}.fromList(it)');
});
});
}
indent.writeln('else -> super.readValueOfType(type, buffer)');
});
});
indent.write(
'override fun writeValue(stream: ByteArrayOutputStream, value: Any?) ');
indent.writeScoped('{', '}', () {
indent.write('when (value) ');
indent.addScoped('{', '}', () {
for (final EnumeratedClass customClass in codecClasses) {
indent.write('is ${customClass.name} -> ');
indent.addScoped('{', '}', () {
indent.writeln('stream.write(${customClass.enumeration})');
indent.writeln('writeValue(stream, value.toList())');
});
}
indent.writeln('else -> super.writeValue(stream, value)');
});
});
});
indent.newln();
}
void _writeWrapResult(Indent indent) {
indent.newln();
indent.write('private fun wrapResult(result: Any?): List<Any?> ');
indent.addScoped('{', '}', () {
indent.writeln('return listOf(result)');
});
}
void _writeWrapError(KotlinOptions generatorOptions, Indent indent) {
indent.newln();
indent.write('private fun wrapError(exception: Throwable): List<Any?> ');
indent.addScoped('{', '}', () {
indent
.write('if (exception is ${_getErrorClassName(generatorOptions)}) ');
indent.addScoped('{', '}', () {
indent.write('return ');
indent.addScoped('listOf(', ')', () {
indent.writeln('exception.code,');
indent.writeln('exception.message,');
indent.writeln('exception.details');
});
}, addTrailingNewline: false);
indent.addScoped(' else {', '}', () {
indent.write('return ');
indent.addScoped('listOf(', ')', () {
indent.writeln('exception.javaClass.simpleName,');
indent.writeln('exception.toString(),');
indent.writeln(
'"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)');
});
});
});
}
void _writeErrorClass(KotlinOptions generatorOptions, Indent indent) {
indent.newln();
indent.writeln('/**');
indent.writeln(
' * Error class for passing custom error details to Flutter via a thrown PlatformException.');
indent.writeln(' * @property code The error code.');
indent.writeln(' * @property message The error message.');
indent.writeln(
' * @property details The error details. Must be a datatype supported by the api codec.');
indent.writeln(' */');
indent.write('class ${_getErrorClassName(generatorOptions)} ');
indent.addScoped('(', ')', () {
indent.writeln('val code: String,');
indent.writeln('override val message: String? = null,');
indent.writeln('val details: Any? = null');
}, addTrailingNewline: false);
indent.addln(' : Throwable()');
}
void _writeCreateConnectionError(
KotlinOptions generatorOptions, Indent indent) {
final String errorClassName = _getErrorClassName(generatorOptions);
indent.newln();
indent.write(
'private fun createConnectionError(channelName: String): $errorClassName ');
indent.addScoped('{', '}', () {
indent.write(
'return $errorClassName("channel-error", "Unable to establish connection on channel: \'\$channelName\'.", "")');
});
}
@override
void writeGeneralUtilities(
KotlinOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
final bool hasHostApi = root.apis.any((Api api) =>
api.methods.isNotEmpty && api.location == ApiLocation.host);
final bool hasFlutterApi = root.apis.any((Api api) =>
api.methods.isNotEmpty && api.location == ApiLocation.flutter);
if (hasHostApi) {
_writeWrapResult(indent);
_writeWrapError(generatorOptions, indent);
}
if (hasFlutterApi) {
_writeCreateConnectionError(generatorOptions, indent);
}
_writeErrorClass(generatorOptions, indent);
}
static void _writeMethodDeclaration(
Indent indent, {
required String name,
required TypeDeclaration returnType,
required List<Parameter> parameters,
List<String> documentationComments = const <String>[],
int? minApiRequirement,
bool isAsynchronous = false,
bool isAbstract = false,
String Function(int index, NamedType type) getArgumentName =
_getArgumentName,
}) {
final List<String> argSignature = <String>[];
if (parameters.isNotEmpty) {
final Iterable<String> argTypes = parameters
.map((NamedType e) => _nullsafeKotlinTypeForDartType(e.type));
final Iterable<String> argNames = indexMap(parameters, getArgumentName);
argSignature.addAll(
map2(
argTypes,
argNames,
(String argType, String argName) {
return '$argName: $argType';
},
),
);
}
final String returnTypeString =
returnType.isVoid ? '' : _nullsafeKotlinTypeForDartType(returnType);
final String resultType = returnType.isVoid ? 'Unit' : returnTypeString;
addDocumentationComments(indent, documentationComments, _docCommentSpec);
if (minApiRequirement != null) {
indent.writeln(
'@androidx.annotation.RequiresApi(api = $minApiRequirement)',
);
}
final String abstractKeyword = isAbstract ? 'abstract ' : '';
if (isAsynchronous) {
argSignature.add('callback: (Result<$resultType>) -> Unit');
indent.writeln('${abstractKeyword}fun $name(${argSignature.join(', ')})');
} else if (returnType.isVoid) {
indent.writeln('${abstractKeyword}fun $name(${argSignature.join(', ')})');
} else {
indent.writeln(
'${abstractKeyword}fun $name(${argSignature.join(', ')}): $returnTypeString',
);
}
}
void _writeHostMethodMessageHandler(
Indent indent, {
required Api api,
required String name,
required String channelName,
required TaskQueueType taskQueueType,
required List<Parameter> parameters,
required TypeDeclaration returnType,
bool isAsynchronous = false,
String Function(List<String> safeArgNames, {required String apiVarName})?
onCreateCall,
}) {
indent.write('run ');
indent.addScoped('{', '}', () {
String? taskQueue;
if (taskQueueType != TaskQueueType.serial) {
taskQueue = 'taskQueue';
indent.writeln(
'val $taskQueue = binaryMessenger.makeBackgroundTaskQueue()');
}
indent.write(
'val channel = BasicMessageChannel<Any?>(binaryMessenger, "$channelName", codec');
if (taskQueue != null) {
indent.addln(', $taskQueue)');
} else {
indent.addln(')');
}
indent.write('if (api != null) ');
indent.addScoped('{', '}', () {
final String messageVarName = parameters.isNotEmpty ? 'message' : '_';
indent.write('channel.setMessageHandler ');
indent.addScoped('{ $messageVarName, reply ->', '}', () {
final List<String> methodArguments = <String>[];
if (parameters.isNotEmpty) {
indent.writeln('val args = message as List<Any?>');
enumerate(parameters, (int index, NamedType arg) {
final String argName = _getSafeArgumentName(index, arg);
final String argIndex = 'args[$index]';
indent.writeln(
'val $argName = ${_castForceUnwrap(argIndex, arg.type, indent)}');
methodArguments.add(argName);
});
}
final String call = onCreateCall != null
? onCreateCall(methodArguments, apiVarName: 'api')
: 'api.$name(${methodArguments.join(', ')})';
if (isAsynchronous) {
indent.write('$call ');
final String resultType = returnType.isVoid
? 'Unit'
: _nullsafeKotlinTypeForDartType(returnType);
indent.addScoped('{ result: Result<$resultType> ->', '}', () {
indent.writeln('val error = result.exceptionOrNull()');
indent.writeScoped('if (error != null) {', '}', () {
indent.writeln('reply.reply(wrapError(error))');
}, addTrailingNewline: false);
indent.addScoped(' else {', '}', () {
final String enumTagNullablePrefix =
returnType.isNullable ? '?' : '!!';
final String enumTag =
returnType.isEnum ? '$enumTagNullablePrefix.raw' : '';
if (returnType.isVoid) {
indent.writeln('reply.reply(wrapResult(null))');
} else {
indent.writeln('val data = result.getOrNull()');
indent.writeln('reply.reply(wrapResult(data$enumTag))');
}
});
});
} else {
indent.writeln('var wrapped: List<Any?>');
indent.write('try ');
indent.addScoped('{', '}', () {
if (returnType.isVoid) {
indent.writeln(call);
indent.writeln('wrapped = listOf<Any?>(null)');
} else {
String enumTag = '';
if (returnType.isEnum) {
final String safeUnwrap = returnType.isNullable ? '?' : '';
enumTag = '$safeUnwrap.raw';
}
indent.writeln('wrapped = listOf<Any?>($call$enumTag)');
}
}, addTrailingNewline: false);
indent.add(' catch (exception: Throwable) ');
indent.addScoped('{', '}', () {
indent.writeln('wrapped = wrapError(exception)');
});
indent.writeln('reply.reply(wrapped)');
}
});
}, addTrailingNewline: false);
indent.addScoped(' else {', '}', () {
indent.writeln('channel.setMessageHandler(null)');
});
});
}
void _writeFlutterMethod(
Indent indent, {
required String name,
required List<Parameter> parameters,
required TypeDeclaration returnType,
required String channelName,
required String errorClassName,
required String dartPackageName,
List<String> documentationComments = const <String>[],
int? minApiRequirement,
void Function(
Indent indent, {
required List<Parameter> parameters,
required TypeDeclaration returnType,
required String channelName,
required String errorClassName,
}) onWriteBody = _writeFlutterMethodMessageCall,
}) {
_writeMethodDeclaration(
indent,
name: name,
returnType: returnType,
parameters: parameters,
documentationComments: documentationComments,
isAsynchronous: true,
minApiRequirement: minApiRequirement,
getArgumentName: _getSafeArgumentName,
);
indent.addScoped('{', '}', () {
onWriteBody(
indent,
parameters: parameters,
returnType: returnType,
channelName: channelName,
errorClassName: errorClassName,
);
});
}
static void _writeFlutterMethodMessageCall(
Indent indent, {
required List<Parameter> parameters,
required TypeDeclaration returnType,
required String channelName,
required String errorClassName,
}) {
String sendArgument;
if (parameters.isEmpty) {
sendArgument = 'null';
} else {
final Iterable<String> enumSafeArgNames = indexMap(
parameters,
(int count, NamedType type) =>
_getEnumSafeArgumentExpression(count, type));
sendArgument = 'listOf(${enumSafeArgNames.join(', ')})';
}
const String channel = 'channel';
indent.writeln('val channelName = "$channelName"');
indent.writeln(
'val $channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)');
indent.writeScoped('$channel.send($sendArgument) {', '}', () {
indent.writeScoped('if (it is List<*>) {', '} ', () {
indent.writeScoped('if (it.size > 1) {', '} ', () {
indent.writeln(
'callback(Result.failure($errorClassName(it[0] as String, it[1] as String, it[2] as String?)))');
}, addTrailingNewline: false);
if (!returnType.isNullable && !returnType.isVoid) {
indent.addScoped('else if (it[0] == null) {', '} ', () {
indent.writeln(
'callback(Result.failure($errorClassName("null-error", "Flutter api returned null value for non-null return value.", "")))');
}, addTrailingNewline: false);
}
indent.addScoped('else {', '}', () {
if (returnType.isVoid) {
indent.writeln('callback(Result.success(Unit))');
} else {
const String output = 'output';
// Nullable enums require special handling.
if (returnType.isEnum && returnType.isNullable) {
indent.writeScoped('val $output = (it[0] as Int?)?.let {', '}',
() {
indent.writeln('${returnType.baseName}.ofRaw(it)');
});
} else {
indent.writeln(
'val $output = ${_cast(indent, 'it[0]', type: returnType)}');
}
indent.writeln('callback(Result.success($output))');
}
});
}, addTrailingNewline: false);
indent.addScoped('else {', '} ', () {
indent.writeln(
'callback(Result.failure(createConnectionError(channelName)))');
});
});
}
}
HostDatatype _getHostDatatype(Root root, NamedType field) {
return getFieldHostDatatype(
field, (TypeDeclaration x) => _kotlinTypeForBuiltinDartType(x));
}
/// Calculates the name of the codec that will be generated for [api].
String _getCodecName(Api api) => '${api.name}Codec';
String _getErrorClassName(KotlinOptions generatorOptions) =>
generatorOptions.errorClassName ?? 'FlutterError';
String _getArgumentName(int count, NamedType argument) =>
argument.name.isEmpty ? 'arg$count' : argument.name;
/// Returns an argument name that can be used in a context where it is possible to collide
/// and append `.index` to enums.
String _getEnumSafeArgumentExpression(int count, NamedType argument) {
if (argument.type.isEnum) {
return argument.type.isNullable
? '${_getArgumentName(count, argument)}Arg?.raw'
: '${_getArgumentName(count, argument)}Arg.raw';
}
return '${_getArgumentName(count, argument)}Arg';
}
/// Returns an argument name that can be used in a context where it is possible to collide.
String _getSafeArgumentName(int count, NamedType argument) =>
'${_getArgumentName(count, argument)}Arg';
String _castForceUnwrap(String value, TypeDeclaration type, Indent indent) {
if (type.isEnum) {
final String forceUnwrap = type.isNullable ? '' : '!!';
final String nullableConditionPrefix =
type.isNullable ? 'if ($value == null) null else ' : '';
return '$nullableConditionPrefix${_kotlinTypeForDartType(type)}.ofRaw($value as Int)$forceUnwrap';
} else {
return _cast(indent, value, type: type);
}
}
/// Converts a [List] of [TypeDeclaration]s to a comma separated [String] to be
/// used in Kotlin code.
String _flattenTypeArguments(List<TypeDeclaration> args) {
return args.map(_kotlinTypeForDartType).join(', ');
}
String _kotlinTypeForBuiltinGenericDartType(TypeDeclaration type) {
if (type.typeArguments.isEmpty) {
switch (type.baseName) {
case 'List':
return 'List<Any?>';
case 'Map':
return 'Map<Any, Any?>';
default:
return 'Any';
}
} else {
switch (type.baseName) {
case 'List':
return 'List<${_nullsafeKotlinTypeForDartType(type.typeArguments.first)}>';
case 'Map':
return 'Map<${_nullsafeKotlinTypeForDartType(type.typeArguments.first)}, ${_nullsafeKotlinTypeForDartType(type.typeArguments.last)}>';
default:
return '${type.baseName}<${_flattenTypeArguments(type.typeArguments)}>';
}
}
}
String? _kotlinTypeForBuiltinDartType(TypeDeclaration type) {
const Map<String, String> kotlinTypeForDartTypeMap = <String, String>{
'void': 'Void',
'bool': 'Boolean',
'String': 'String',
'int': 'Long',
'double': 'Double',
'Uint8List': 'ByteArray',
'Int32List': 'IntArray',
'Int64List': 'LongArray',
'Float32List': 'FloatArray',
'Float64List': 'DoubleArray',
'Object': 'Any',
};
if (kotlinTypeForDartTypeMap.containsKey(type.baseName)) {
return kotlinTypeForDartTypeMap[type.baseName];
} else if (type.baseName == 'List' || type.baseName == 'Map') {
return _kotlinTypeForBuiltinGenericDartType(type);
} else {
return null;
}
}
String _kotlinTypeForDartType(TypeDeclaration type) {
return _kotlinTypeForBuiltinDartType(type) ?? type.baseName;
}
String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
final String nullSafe = type.isNullable ? '?' : '';
return '${_kotlinTypeForDartType(type)}$nullSafe';
}
/// Returns an expression to cast [variable] to [kotlinType].
String _cast(Indent indent, String variable, {required TypeDeclaration type}) {
// Special-case Any, since no-op casts cause warnings.
final String typeString = _kotlinTypeForDartType(type);
if (type.isNullable && typeString == 'Any') {
return variable;
}
if (typeString == 'Int' || typeString == 'Long') {
return '$variable${_castInt(type.isNullable)}';
}
if (type.isEnum) {
if (type.isNullable) {
return '($variable as Int?)?.let {\n'
'${indent.str} $typeString.ofRaw(it)\n'
'${indent.str}}';
}
return '${type.baseName}.ofRaw($variable as Int)!!';
}
return '$variable as ${_nullsafeKotlinTypeForDartType(type)}';
}
String _castInt(bool isNullable) {
final String nullability = isNullable ? '?' : '';
return '.let { if (it is Int) it.toLong() else it as Long$nullability }';
}