Skip to content

Commit 8c5ac64

Browse files
committed
Generate type aliases in Dart
Added dedicated DartTypeAlias template, as type aliases (typedefs) are now supported in Dart language (since Dart version 2.13). Updated other Dart tempates and resolvers to treat type aliases as a normal type, instead of skipping it through to the target type. Added/updated related smoke and functional tests. Unrelated smoke tests are updated in a separate commit. Resolves: #907 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
1 parent 65c4c39 commit 8c5ac64

File tree

16 files changed

+141
-40
lines changed

16 files changed

+141
-40
lines changed

.github/workflows/functional-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ jobs:
248248
- name: Install Dart SDK
249249
run: |
250250
DART_RELEASE_CHANNEL=stable
251-
DART_VERSION=2.12.0
251+
DART_VERSION=2.13.3
252252
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
253253
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
254254
- name: Build and run functional tests
@@ -365,7 +365,7 @@ jobs:
365365
- name: Install Dart SDK
366366
run: |
367367
DART_RELEASE_CHANNEL=stable
368-
DART_VERSION=2.12.0
368+
DART_VERSION=2.13.3
369369
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
370370
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
371371
- name: Build and run functional tests

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Gluecodium project Release Notes
22

3+
## Unreleased
4+
### Features:
5+
* Added support for type aliases (typedefs) in Dart
6+
### Breaking changes:
7+
* Generated Dart code now requires minimum Dart version 2.13.0.
8+
39
## Unreleased
410
### Features:
511
* Added support for per-platform visibility attributes (e.g. `@Java(Internal)`, etc.).

functional-tests/functional/dart/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import "test/StaticIntMethods_test.dart" as StaticIntMethodsTests;
6969
import "test/StaticStringMethods_test.dart" as StaticStringMethodsTests;
7070
import "test/StructsWithConstants_test.dart" as StructsWithConstantsTests;
7171
import "test/StructsWithMethods_test.dart" as StructsWithMethodsTests;
72+
import "test/TypeAliases_test.dart" as TypeAliasesTests;
7273

7374
final _allTests = [
7475
AsyncTests.main,
@@ -117,7 +118,8 @@ final _allTests = [
117118
StaticIntMethodsTests.main,
118119
StaticStringMethodsTests.main,
119120
StructsWithConstantsTests.main,
120-
StructsWithMethodsTests.main
121+
StructsWithMethodsTests.main,
122+
TypeAliasesTests.main
121123
];
122124

123125
String _getLibraryPath(String nativeLibraryName) {

functional-tests/functional/dart/pubspec.yaml.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: FunctionalDartTests
22
environment:
3-
sdk: '>=2.12.0 <3.0.0'
3+
sdk: '>=2.13.0 <3.0.0'
44
dependencies:
55
test:
66
functional:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// Copyright (C) 2016-2021 HERE Europe B.V.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
// SPDX-License-Identifier: Apache-2.0
17+
// License-Filename: LICENSE
18+
//
19+
// -------------------------------------------------------------------------------------------------
20+
21+
import "package:test/test.dart";
22+
import "package:functional/test.dart";
23+
import "../test_suite.dart";
24+
25+
final _testSuite = TestSuite("Type Aliases");
26+
27+
void main() {
28+
_testSuite.test("Type alias to struct", () {
29+
final result = StaticTypedefExampleStructTypedef("nonsense");
30+
31+
expect(result is StaticTypedefExampleStruct, isTrue);
32+
expect(result.exampleString, "nonsense");
33+
});
34+
_testSuite.test("Type alias used by a function", () {
35+
final result = StaticTypedef.returnIntTypedef(2);
36+
37+
expect(result is int, isTrue);
38+
expect(result, 3);
39+
});
40+
_testSuite.test("Type alias points to a type alias", () {
41+
final result = StaticTypedef.returnNestedIntTypedef(4);
42+
43+
expect(result is int, isTrue);
44+
expect(result, 5);
45+
});
46+
_testSuite.test("Type alias from type collection", () {
47+
final result = StaticTypedef.returnTypedefPointFromTypeCollection(
48+
TypeCollectionPointTypedef(1.0, 3.0)
49+
);
50+
51+
expect(result is TypeCollectionPoint, isTrue);
52+
expect(result.x, 1.0);
53+
expect(result.y, 3.0);
54+
});
55+
}

functional-tests/functional/input/lime/StaticTypedef.lime

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class StaticTypedef {
2323
// Example struct
2424
struct ExampleStruct {
2525
exampleString: String = ""
26+
@Skip(Dart)
27+
field constructor()
28+
field constructor(exampleString)
2629
}
2730
typealias IntTypedef = Int
2831
typealias NestedIntTypedef = IntTypedef

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartGenerator.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ internal class DartGenerator : Generator {
181181
injectAsyncHelpers(ffiReferenceMap, asyncHelpers)
182182

183183
val generatedFiles = dartFilteredModel.topElements.flatMap {
184-
listOfNotNull(
184+
listOf(
185185
generateDart(
186186
it, dartResolvers, dartNameResolver, listOf(importsCollector, declarationImportsCollector),
187187
exportsCollector, typeRepositoriesCollector, predicatesMap, descendantInterfaces,
@@ -216,15 +216,15 @@ internal class DartGenerator : Generator {
216216
predicates: Map<String, (Any) -> Boolean>,
217217
descendantInterfaces: Map<String, List<LimeInterface>>,
218218
asyncHelpers: DartAsyncHelpers.AsyncHelpersGroup?
219-
): GeneratedFile? {
220-
val contentTemplateName = selectTemplate(rootElement) ?: return null
219+
): GeneratedFile {
220+
val contentTemplateName = selectTemplate(rootElement)
221221

222222
val packagePath = rootElement.path.head.joinToString(separator = "/")
223223
val fileName = dartNameResolver.resolveFileName(rootElement)
224224
val filePath = "$packagePath/$fileName"
225225
val relativePath = "$SRC_DIR_SUFFIX/$filePath.dart"
226226

227-
val allTypes = LimeTypeHelper.getAllTypes(rootElement).filterNot { it is LimeTypeAlias }
227+
val allTypes = LimeTypeHelper.getAllTypes(rootElement)
228228
val nonExternalTypes = allTypes.filter { it.external?.dart == null }
229229
val allSymbols = nonExternalTypes.filterNot { CommonGeneratorPredicates.isInternal(it, DART) }
230230
if (allSymbols.isNotEmpty()) {
@@ -529,7 +529,7 @@ internal class DartGenerator : Generator {
529529
is LimeEnumeration -> "dart/DartEnumeration"
530530
is LimeException -> "dart/DartException"
531531
is LimeLambda -> "dart/DartLambda"
532-
is LimeTypeAlias -> null
532+
is LimeTypeAlias -> "dart/DartTypeAlias"
533533
else -> throw GluecodiumExecutionException(
534534
"Unsupported top-level element: " +
535535
limeElement::class.java.name

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartImportResolver.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,16 @@ internal class DartImportResolver(
5656
}
5757

5858
private fun resolveTypeImports(limeType: LimeType, skipHelpers: Boolean = false): List<DartImport> {
59-
val actualType = limeType.actualType
60-
when (actualType) {
61-
is LimeBasicType -> return resolveBasicTypeImports(actualType)
62-
is LimeGenericType -> return resolveGenericTypeImports(actualType)
59+
when (limeType) {
60+
is LimeBasicType -> return resolveBasicTypeImports(limeType)
61+
is LimeGenericType -> return resolveGenericTypeImports(limeType)
6362
}
6463

65-
val externalImport = resolveExternalImport(actualType, IMPORT_PATH_NAME, useAlias = true)
64+
val externalImport = resolveExternalImport(limeType, IMPORT_PATH_NAME, useAlias = true)
6665
return when {
67-
externalImport == null -> listOf(createImport(actualType))
66+
externalImport == null -> listOf(createImport(limeType))
6867
skipHelpers -> listOf(externalImport)
69-
else -> listOf(createImport(actualType), externalImport)
68+
else -> listOf(createImport(limeType), externalImport)
7069
}
7170
}
7271

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartImportsCollector.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ internal class DartImportsCollector(importsResolver: ImportsResolver<DartImport>
2929
importsResolver,
3030
collectTypeRefImports = true,
3131
collectValueImports = true,
32-
parentTypeFilter = { true }
32+
parentTypeFilter = { true },
33+
collectTypeAliasImports = true
3334
) {
3435

3536
override fun collectParentTypeRefs(limeContainer: LimeContainerWithInheritance) =

gluecodium/src/main/java/com/here/gluecodium/generator/dart/DartNameResolver.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import com.here.gluecodium.model.lime.LimeReturnType
4848
import com.here.gluecodium.model.lime.LimeSet
4949
import com.here.gluecodium.model.lime.LimeStruct
5050
import com.here.gluecodium.model.lime.LimeType
51-
import com.here.gluecodium.model.lime.LimeTypeAlias
5251
import com.here.gluecodium.model.lime.LimeTypeHelper
5352
import com.here.gluecodium.model.lime.LimeTypeRef
5453
import com.here.gluecodium.model.lime.LimeValue
@@ -83,7 +82,6 @@ internal class DartNameResolver(
8382
is LimeValue -> resolveValue(element)
8483
is LimeGenericType -> resolveGenericType(element)
8584
is LimeTypeRef -> resolveTypeRefName(element)
86-
is LimeTypeAlias -> resolveName(element.typeRef)
8785
is LimeType -> resolveTypeName(element)
8886
is LimeNamedElement -> getPlatformName(element)
8987
else ->
@@ -261,11 +259,11 @@ internal class DartNameResolver(
261259

262260
private fun resolveTypeRefName(limeTypeRef: LimeTypeRef, ignoreDuplicates: Boolean = false): String {
263261
val typeName = resolveName(limeTypeRef.type)
264-
val importPath = limeTypeRef.type.actualType.external?.dart?.get(IMPORT_PATH_NAME)
262+
val importPath = limeTypeRef.type.external?.dart?.get(IMPORT_PATH_NAME)
265263
val alias = when {
266264
importPath != null -> computeAlias(importPath)
267265
ignoreDuplicates -> null
268-
duplicateNames.contains(typeName) -> limeTypeRef.type.actualType.path.head.joinToString("_")
266+
duplicateNames.contains(typeName) -> limeTypeRef.type.path.head.joinToString("_")
269267
else -> null
270268
}
271269
val suffix = if (limeTypeRef.isNullable) "?" else ""
@@ -303,7 +301,7 @@ internal class DartNameResolver(
303301
private fun buildDuplicateNames() =
304302
limeReferenceMap.values
305303
.filterIsInstance<LimeType>()
306-
.filterNot { it is LimeTypeAlias || it is LimeGenericType || it is LimeBasicType }
304+
.filterNot { it is LimeGenericType || it is LimeBasicType }
307305
.filter { it.external?.dart == null }
308306
.groupBy { resolveTypeName(it) }
309307
.filterValues { it.size > 1 }

gluecodium/src/main/resources/templates/dart/DartClass.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ abstract class {{resolveName}}{{!!
4545
{{/ifPredicate}}
4646
}
4747

48+
{{#typeAliases}}
49+
{{>dart/DartTypeAlias}}
50+
{{/typeAliases}}
4851
{{#enumerations}}
4952
{{>dart/DartEnumeration}}
5053
{{/enumerations}}

gluecodium/src/main/resources/templates/dart/DartInterface.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ abstract class {{resolveName}}{{!!
6161
{{/ifPredicate}}
6262
}
6363

64+
{{#typeAliases}}
65+
{{>dart/DartTypeAlias}}
66+
{{/typeAliases}}
6467
{{#enumerations}}
6568
{{>dart/DartEnumeration}}
6669
{{/enumerations}}

gluecodium/src/main/resources/templates/dart/DartPubspec.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
!}}
2121
name: {{libraryName}}
2222
environment:
23-
sdk: '>=2.12.0 <3.0.0'
23+
sdk: '>=2.13.0 <3.0.0'
2424
dependencies:
2525
ffi:
2626
intl:

gluecodium/src/main/resources/templates/dart/DartStruct.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class {{resolveName}}{{#if external.dart.converter}}Internal{{/if}} {
7070
}
7171
{{/unlessPredicate}}
7272

73+
{{#typeAliases}}
74+
{{>dart/DartTypeAlias}}
75+
{{/typeAliases}}
7376
{{#enumerations}}
7477
{{>dart/DartEnumeration}}
7578
{{/enumerations}}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{!!
2+
!
3+
! Copyright (C) 2016-2021 HERE Europe B.V.
4+
!
5+
! Licensed under the Apache License, Version 2.0 (the "License");
6+
! you may not use this file except in compliance with the License.
7+
! You may obtain a copy of the License at
8+
!
9+
! http://www.apache.org/licenses/LICENSE-2.0
10+
!
11+
! Unless required by applicable law or agreed to in writing, software
12+
! distributed under the License is distributed on an "AS IS" BASIS,
13+
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
! See the License for the specific language governing permissions and
15+
! limitations under the License.
16+
!
17+
! SPDX-License-Identifier: Apache-2.0
18+
! License-Filename: LICENSE
19+
!
20+
!}}
21+
{{>dart/DartDocumentation}}{{>dart/DartAttributes}}
22+
typedef {{resolveName "visibility"}}{{resolveName}} = {{resolveName typeRef}};

0 commit comments

Comments
 (0)