Skip to content

Commit 2a46e48

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 d540c7e commit 2a46e48

File tree

16 files changed

+143
-42
lines changed

16 files changed

+143
-42
lines changed

.github/workflows/functional-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ jobs:
244244
- name: Install Dart SDK
245245
run: |
246246
DART_RELEASE_CHANNEL=stable
247-
DART_VERSION=2.12.0
247+
DART_VERSION=2.13.3
248248
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
249249
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
250250
- name: Build and run functional tests
@@ -364,7 +364,7 @@ jobs:
364364
- name: Install Dart SDK
365365
run: |
366366
DART_RELEASE_CHANNEL=stable
367-
DART_VERSION=2.12.0
367+
DART_VERSION=2.13.3
368368
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
369369
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
370370
- 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
## 10.7.1
410
Release date: 2022-02-08
511
### Bug fixes:

functional-tests/functional/dart/main.dart

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

7273
final _allTests = [
7374
BlobsTests.main,
@@ -115,7 +116,8 @@ final _allTests = [
115116
StaticIntMethodsTests.main,
116117
StaticStringMethodsTests.main,
117118
StructsWithConstantsTests.main,
118-
StructsWithMethodsTests.main
119+
StructsWithMethodsTests.main,
120+
TypeAliasesTests.main
119121
];
120122

121123
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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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(PointTypedef(1.0, 3.0));
48+
49+
expect(result is Point, isTrue);
50+
expect(result.x, 1.0);
51+
expect(result.y, 3.0);
52+
});
53+
}

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
@@ -168,7 +168,7 @@ internal class DartGenerator : Generator {
168168
.sortedBy { ffiNameResolver.resolveName(it) }
169169

170170
val generatedFiles = dartFilteredModel.topElements.flatMap {
171-
listOfNotNull(
171+
listOf(
172172
generateDart(
173173
it, dartResolvers, dartNameResolver, listOf(importsCollector, declarationImportsCollector),
174174
exportsCollector, typeRepositoriesCollector, predicatesMap, descendantInterfaces
@@ -198,15 +198,15 @@ internal class DartGenerator : Generator {
198198
typeRepositoriesCollector: MutableList<LimeContainerWithInheritance>,
199199
predicates: Map<String, (Any) -> Boolean>,
200200
descendantInterfaces: Map<String, List<LimeInterface>>
201-
): GeneratedFile? {
202-
val contentTemplateName = selectTemplate(rootElement) ?: return null
201+
): GeneratedFile {
202+
val contentTemplateName = selectTemplate(rootElement)
203203

204204
val packagePath = rootElement.path.head.joinToString(separator = "/")
205205
val fileName = dartNameResolver.resolveFileName(rootElement)
206206
val filePath = "$packagePath/$fileName"
207207
val relativePath = "$SRC_DIR_SUFFIX/$filePath.dart"
208208

209-
val allTypes = LimeTypeHelper.getAllTypes(rootElement).filterNot { it is LimeTypeAlias }
209+
val allTypes = LimeTypeHelper.getAllTypes(rootElement)
210210
val nonExternalTypes = allTypes.filter { it.external?.dart == null }
211211
val freeConstants = (rootElement as? LimeTypesCollection)?.constants ?: emptyList()
212212
val allSymbols =
@@ -511,7 +511,7 @@ internal class DartGenerator : Generator {
511511
is LimeEnumeration -> "dart/DartEnumeration"
512512
is LimeException -> "dart/DartException"
513513
is LimeLambda -> "dart/DartLambda"
514-
is LimeTypeAlias -> null
514+
is LimeTypeAlias -> "dart/DartTypeAlias"
515515
else -> throw GluecodiumExecutionException(
516516
"Unsupported top-level element: " +
517517
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
@@ -54,17 +54,16 @@ internal class DartImportResolver(
5454
}
5555

5656
private fun resolveTypeImports(limeType: LimeType, skipHelpers: Boolean = false): List<DartImport> {
57-
val actualType = limeType.actualType
58-
when (actualType) {
59-
is LimeBasicType -> return resolveBasicTypeImports(actualType)
60-
is LimeGenericType -> return resolveGenericTypeImports(actualType)
57+
when (limeType) {
58+
is LimeBasicType -> return resolveBasicTypeImports(limeType)
59+
is LimeGenericType -> return resolveGenericTypeImports(limeType)
6160
}
6261

63-
val externalImport = resolveExternalImport(actualType, IMPORT_PATH_NAME, useAlias = true)
62+
val externalImport = resolveExternalImport(limeType, IMPORT_PATH_NAME, useAlias = true)
6463
return when {
65-
externalImport == null -> listOf(createImport(actualType))
64+
externalImport == null -> listOf(createImport(limeType))
6665
skipHelpers -> listOf(externalImport)
67-
else -> listOf(createImport(actualType), externalImport)
66+
else -> listOf(createImport(limeType), externalImport)
6867
}
6968
}
7069

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import com.here.gluecodium.model.lime.LimeClass
2525
import com.here.gluecodium.model.lime.LimeContainerWithInheritance
2626

2727
internal class DartImportsCollector(importsResolver: ImportsResolver<DartImport>) :
28-
GenericImportsCollector<DartImport>(importsResolver, collectTypeRefImports = true, parentTypeFilter = { true }) {
28+
GenericImportsCollector<DartImport>(
29+
importsResolver,
30+
collectTypeRefImports = true,
31+
parentTypeFilter = { true },
32+
collectTypeAliasImports = true
33+
) {
2934

3035
override fun collectParentTypeRefs(limeContainer: LimeContainerWithInheritance) =
3136
when (limeContainer) {

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
@@ -44,7 +44,6 @@ import com.here.gluecodium.model.lime.LimeReturnType
4444
import com.here.gluecodium.model.lime.LimeSet
4545
import com.here.gluecodium.model.lime.LimeStruct
4646
import com.here.gluecodium.model.lime.LimeType
47-
import com.here.gluecodium.model.lime.LimeTypeAlias
4847
import com.here.gluecodium.model.lime.LimeTypeRef
4948
import com.here.gluecodium.model.lime.LimeTypesCollection
5049
import com.here.gluecodium.model.lime.LimeValue
@@ -81,7 +80,6 @@ internal class DartNameResolver(
8180
is LimeValue -> resolveValue(element)
8281
is LimeGenericType -> resolveGenericType(element)
8382
is LimeTypeRef -> resolveTypeRefName(element)
84-
is LimeTypeAlias -> resolveName(element.typeRef)
8583
is LimeType -> resolveTypeName(element)
8684
is LimeNamedElement -> getPlatformName(element)
8785
else ->
@@ -246,11 +244,11 @@ internal class DartNameResolver(
246244

247245
private fun resolveTypeRefName(limeTypeRef: LimeTypeRef, ignoreDuplicates: Boolean = false): String {
248246
val typeName = resolveName(limeTypeRef.type)
249-
val importPath = limeTypeRef.type.actualType.external?.dart?.get(IMPORT_PATH_NAME)
247+
val importPath = limeTypeRef.type.external?.dart?.get(IMPORT_PATH_NAME)
250248
val alias = when {
251249
importPath != null -> computeAlias(importPath)
252250
ignoreDuplicates -> null
253-
duplicateNames.contains(typeName) -> limeTypeRef.type.actualType.path.head.joinToString("_")
251+
duplicateNames.contains(typeName) -> limeTypeRef.type.path.head.joinToString("_")
254252
else -> null
255253
}
256254
val suffix = if (limeTypeRef.isNullable) "?" else ""
@@ -283,7 +281,7 @@ internal class DartNameResolver(
283281
private fun buildDuplicateNames() =
284282
limeReferenceMap.values
285283
.filterIsInstance<LimeType>()
286-
.filterNot { it is LimeTypesCollection || it is LimeTypeAlias || it is LimeGenericType || it is LimeBasicType }
284+
.filterNot { it is LimeTypesCollection || it is LimeGenericType || it is LimeBasicType }
287285
.filter { it.external?.dart == null }
288286
.groupBy { resolveTypeName(it) }
289287
.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}};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
! License-Filename: LICENSE
1919
!
2020
!}}
21+
{{#typeAliases}}
22+
{{>dart/DartTypeAlias}}
23+
{{/typeAliases}}
2124
{{#enumerations}}
2225
{{>dart/DartEnumeration}}
2326
{{/enumerations}}

0 commit comments

Comments
 (0)