Skip to content

Commit bf55e6d

Browse files
committed
add logic to remove test class and update generation to use it
1 parent 7df2085 commit bf55e6d

File tree

7 files changed

+84
-98
lines changed

7 files changed

+84
-98
lines changed

packages/pigeon/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 17.0.1
22

3+
* [kotlin] Adds `includeErrorClass` to `KotlinOptions`.
34
* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.
45

56
## 17.0.0

packages/pigeon/lib/generator_tools.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'ast.dart';
1313
/// The current version of pigeon.
1414
///
1515
/// This must match the version in pubspec.yaml.
16-
const String pigeonVersion = '17.0.0';
16+
const String pigeonVersion = '17.0.1';
1717

1818
/// Read all the content from [stdin] to a String.
1919
String readStdin() {

packages/pigeon/lib/kotlin_generator.dart

+23-12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class KotlinOptions {
3232
this.package,
3333
this.copyrightHeader,
3434
this.errorClassName,
35+
this.includeErrorClass = true,
3536
});
3637

3738
/// The package where the generated class will live.
@@ -43,13 +44,20 @@ class KotlinOptions {
4344
/// The name of the error class used for passing custom error parameters.
4445
final String? errorClassName;
4546

47+
/// Whether to include the error class in generation.
48+
///
49+
/// This should only ever be set to false if you have another generated
50+
/// Kotlin file in the same directory.
51+
final bool includeErrorClass;
52+
4653
/// Creates a [KotlinOptions] from a Map representation where:
4754
/// `x = KotlinOptions.fromMap(x.toMap())`.
4855
static KotlinOptions fromMap(Map<String, Object> map) {
4956
return KotlinOptions(
5057
package: map['package'] as String?,
5158
copyrightHeader: map['copyrightHeader'] as Iterable<String>?,
5259
errorClassName: map['errorClassName'] as String?,
60+
includeErrorClass: map['includeErrorClass'] as bool? ?? true,
5361
);
5462
}
5563

@@ -60,6 +68,7 @@ class KotlinOptions {
6068
if (package != null) 'package': package!,
6169
if (copyrightHeader != null) 'copyrightHeader': copyrightHeader!,
6270
if (errorClassName != null) 'errorClassName': errorClassName!,
71+
'includeErrorClass': includeErrorClass,
6372
};
6473
return result;
6574
}
@@ -298,7 +307,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
298307
addDocumentationComments(
299308
indent, field.documentationComments, _docCommentSpec);
300309
indent.write(
301-
'val ${field.name}: ${_nullsafeKotlinTypeForDartType(field.type)}');
310+
'val ${field.name}: ${_nullSafeKotlinTypeForDartType(field.type)}');
302311
final String defaultNil = field.type.isNullable ? ' = null' : '';
303312
indent.add(defaultNil);
304313
}
@@ -361,16 +370,15 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
361370
});
362371
});
363372

364-
final String errorClassName = _getErrorClassName(generatorOptions);
365373
for (final Method method in api.methods) {
366374
_writeFlutterMethod(
367375
indent,
376+
generatorOptions: generatorOptions,
368377
name: method.name,
369378
parameters: method.parameters,
370379
returnType: method.returnType,
371380
channelName: makeChannelName(api, method, dartPackageName),
372381
documentationComments: method.documentationComments,
373-
errorClassName: errorClassName,
374382
dartPackageName: dartPackageName,
375383
);
376384
}
@@ -588,7 +596,9 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
588596
if (hasFlutterApi) {
589597
_writeCreateConnectionError(generatorOptions, indent);
590598
}
591-
_writeErrorClass(generatorOptions, indent);
599+
if (generatorOptions.includeErrorClass) {
600+
_writeErrorClass(generatorOptions, indent);
601+
}
592602
}
593603

594604
static void _writeMethodDeclaration(
@@ -606,7 +616,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
606616
final List<String> argSignature = <String>[];
607617
if (parameters.isNotEmpty) {
608618
final Iterable<String> argTypes = parameters
609-
.map((NamedType e) => _nullsafeKotlinTypeForDartType(e.type));
619+
.map((NamedType e) => _nullSafeKotlinTypeForDartType(e.type));
610620
final Iterable<String> argNames = indexMap(parameters, getArgumentName);
611621
argSignature.addAll(
612622
map2(
@@ -620,7 +630,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
620630
}
621631

622632
final String returnTypeString =
623-
returnType.isVoid ? '' : _nullsafeKotlinTypeForDartType(returnType);
633+
returnType.isVoid ? '' : _nullSafeKotlinTypeForDartType(returnType);
624634

625635
final String resultType = returnType.isVoid ? 'Unit' : returnTypeString;
626636
addDocumentationComments(indent, documentationComments, _docCommentSpec);
@@ -700,7 +710,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
700710
indent.write('$call ');
701711
final String resultType = returnType.isVoid
702712
? 'Unit'
703-
: _nullsafeKotlinTypeForDartType(returnType);
713+
: _nullSafeKotlinTypeForDartType(returnType);
704714
indent.addScoped('{ result: Result<$resultType> ->', '}', () {
705715
indent.writeln('val error = result.exceptionOrNull()');
706716
indent.writeScoped('if (error != null) {', '}', () {
@@ -751,11 +761,11 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
751761

752762
void _writeFlutterMethod(
753763
Indent indent, {
764+
required KotlinOptions generatorOptions,
754765
required String name,
755766
required List<Parameter> parameters,
756767
required TypeDeclaration returnType,
757768
required String channelName,
758-
required String errorClassName,
759769
required String dartPackageName,
760770
List<String> documentationComments = const <String>[],
761771
int? minApiRequirement,
@@ -778,6 +788,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
778788
getArgumentName: _getSafeArgumentName,
779789
);
780790

791+
final String errorClassName = _getErrorClassName(generatorOptions);
781792
indent.addScoped('{', '}', () {
782793
onWriteBody(
783794
indent,
@@ -910,9 +921,9 @@ String _kotlinTypeForBuiltinGenericDartType(TypeDeclaration type) {
910921
} else {
911922
switch (type.baseName) {
912923
case 'List':
913-
return 'List<${_nullsafeKotlinTypeForDartType(type.typeArguments.first)}>';
924+
return 'List<${_nullSafeKotlinTypeForDartType(type.typeArguments.first)}>';
914925
case 'Map':
915-
return 'Map<${_nullsafeKotlinTypeForDartType(type.typeArguments.first)}, ${_nullsafeKotlinTypeForDartType(type.typeArguments.last)}>';
926+
return 'Map<${_nullSafeKotlinTypeForDartType(type.typeArguments.first)}, ${_nullSafeKotlinTypeForDartType(type.typeArguments.last)}>';
916927
default:
917928
return '${type.baseName}<${_flattenTypeArguments(type.typeArguments)}>';
918929
}
@@ -946,7 +957,7 @@ String _kotlinTypeForDartType(TypeDeclaration type) {
946957
return _kotlinTypeForBuiltinDartType(type) ?? type.baseName;
947958
}
948959

949-
String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
960+
String _nullSafeKotlinTypeForDartType(TypeDeclaration type) {
950961
final String nullSafe = type.isNullable ? '?' : '';
951962
return '${_kotlinTypeForDartType(type)}$nullSafe';
952963
}
@@ -969,7 +980,7 @@ String _cast(Indent indent, String variable, {required TypeDeclaration type}) {
969980
}
970981
return '${type.baseName}.ofRaw($variable as Int)!!';
971982
}
972-
return '$variable as ${_nullsafeKotlinTypeForDartType(type)}';
983+
return '$variable as ${_nullSafeKotlinTypeForDartType(type)}';
973984
}
974985

975986
String _castInt(bool isNullable) {

packages/pigeon/lib/pigeon_lib.dart

+1
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ class KotlinGeneratorAdapter implements GeneratorAdapter {
776776
options.kotlinOptions ?? const KotlinOptions();
777777
kotlinOptions = kotlinOptions.merge(KotlinOptions(
778778
errorClassName: kotlinOptions.errorClassName ?? 'FlutterError',
779+
includeErrorClass: kotlinOptions.includeErrorClass,
779780
copyrightHeader: options.copyrightHeader != null
780781
? _lineReader(
781782
path.posix.join(options.basePath ?? '', options.copyrightHeader))

0 commit comments

Comments
 (0)