Skip to content

Commit 5bf5b31

Browse files
committed
[draft] Dart typealias
Resolves: #907 Signed-off-by: Daniel Kamkha <daniel.kamkha@here.com>
1 parent 629ee41 commit 5bf5b31

File tree

47 files changed

+427
-255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+427
-255
lines changed

.github/workflows/functional-tests.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ jobs:
234234
- name: Install Dart SDK
235235
run: |
236236
DART_RELEASE_CHANNEL=stable
237-
DART_VERSION=2.12.0
237+
DART_VERSION=2.13.3
238238
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
239239
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
240240
- name: Build and run functional tests
@@ -273,14 +273,14 @@ jobs:
273273
with:
274274
path: |
275275
~/dart_sdk
276-
key: ${{ runner.os }}-dart-2.12.0-stable
277-
restore-keys: ${{ runner.os }}-dart-2.12.0-stable
276+
key: ${{ runner.os }}-dart-2.13.3-stable
277+
restore-keys: ${{ runner.os }}-dart-2.13.3-stable
278278
- name: Compile Dart SDK without tcmalloc
279279
run: |
280280
export DART_ROOT=${HOME}/dart_sdk
281281
export DART_BIN=${DART_ROOT}/bin
282282
export PATH=${PATH}:${PWD}/depot_tools:${DART_BIN}
283-
DART_VERSION=2.12.0
283+
DART_VERSION=2.13.3
284284
if [ ! -d "${DART_ROOT}/bin" ]; then
285285
sudo apt install -y python2
286286
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
@@ -355,7 +355,7 @@ jobs:
355355
- name: Install Dart SDK
356356
run: |
357357
DART_RELEASE_CHANNEL=stable
358-
DART_VERSION=2.12.0
358+
DART_VERSION=2.13.3
359359
wget -nv https://storage.googleapis.com/dart-archive/channels/${DART_RELEASE_CHANNEL}/release/${DART_VERSION}/linux_packages/dart_${DART_VERSION}-1_amd64.deb
360360
sudo apt -y install ./dart_${DART_VERSION}-1_amd64.deb
361361
- name: Build and run functional tests

functional-tests/functional/dart/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import "test/StaticIntMethods_test.dart" as StaticIntMethodsTests;
6464
import "test/StaticStringMethods_test.dart" as StaticStringMethodsTests;
6565
import "test/StructsWithConstants_test.dart" as StructsWithConstantsTests;
6666
import "test/StructsWithMethods_test.dart" as StructsWithMethodsTests;
67+
import "test/TypeAliases_test.dart" as TypeAliasesTests;
6768

6869
final _allTests = [
6970
BlobsTests.main,
@@ -107,7 +108,8 @@ final _allTests = [
107108
StaticIntMethodsTests.main,
108109
StaticStringMethodsTests.main,
109110
StructsWithConstantsTests.main,
110-
StructsWithMethodsTests.main
111+
StructsWithMethodsTests.main,
112+
TypeAliasesTests.main
111113
];
112114

113115
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
@@ -156,7 +156,7 @@ internal class DartGenerator : Generator {
156156

157157
val predicates = predicates(dartFilteredModel.referenceMap, activeTags)
158158
val generatedFiles = dartFilteredModel.topElements.flatMap {
159-
listOfNotNull(
159+
listOf(
160160
generateDart(
161161
it, dartResolvers, dartNameResolver, listOf(importsCollector, declarationImportsCollector),
162162
exportsCollector, typeRepositoriesCollector, predicates
@@ -185,15 +185,15 @@ internal class DartGenerator : Generator {
185185
exportsCollector: MutableMap<List<String>, MutableList<DartExport>>,
186186
typeRepositoriesCollector: MutableList<LimeContainerWithInheritance>,
187187
predicates: Map<String, (Any) -> Boolean>
188-
): GeneratedFile? {
189-
val contentTemplateName = selectTemplate(rootElement) ?: return null
188+
): GeneratedFile {
189+
val contentTemplateName = selectTemplate(rootElement)
190190

191191
val packagePath = rootElement.path.head.joinToString(separator = "/")
192192
val fileName = dartNameResolver.resolveFileName(rootElement)
193193
val filePath = "$packagePath/$fileName"
194194
val relativePath = "$SRC_DIR_SUFFIX/$filePath.dart"
195195

196-
val allTypes = LimeTypeHelper.getAllTypes(rootElement).filterNot { it is LimeTypeAlias }
196+
val allTypes = LimeTypeHelper.getAllTypes(rootElement)
197197
val nonExternalTypes = allTypes.filter { it.external?.dart == null }
198198
val freeConstants = (rootElement as? LimeTypesCollection)?.constants ?: emptyList()
199199
val allSymbols =
@@ -501,7 +501,7 @@ internal class DartGenerator : Generator {
501501
is LimeEnumeration -> "dart/DartEnumeration"
502502
is LimeException -> "dart/DartException"
503503
is LimeLambda -> "dart/DartLambda"
504-
is LimeTypeAlias -> null
504+
is LimeTypeAlias -> "dart/DartTypeAlias"
505505
else -> throw GluecodiumExecutionException(
506506
"Unsupported top-level element: " +
507507
limeElement::class.java.name

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ internal class DartImportResolver(
5454
}
5555

5656
private fun resolveTypeImports(limeType: LimeType): List<DartImport> =
57-
when (val actualType = limeType.actualType) {
58-
is LimeBasicType -> resolveBasicTypeImports(actualType)
59-
is LimeGenericType -> resolveGenericTypeImports(actualType)
57+
when (limeType) {
58+
is LimeBasicType -> resolveBasicTypeImports(limeType)
59+
is LimeGenericType -> resolveGenericTypeImports(limeType)
6060
else -> listOfNotNull(
61-
createImport(actualType),
62-
resolveExternalImport(actualType, IMPORT_PATH_NAME, useAlias = true)
61+
createImport(limeType),
62+
resolveExternalImport(limeType, IMPORT_PATH_NAME, useAlias = true)
6363
)
6464
}
6565

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
@@ -26,7 +26,12 @@ import com.here.gluecodium.model.lime.LimeInterface
2626
import com.here.gluecodium.model.lime.LimeTypeRef
2727

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

3136
override fun collectParentTypeRefs(limeContainer: LimeContainerWithInheritance): List<LimeTypeRef> {
3237
val parentTypeRef = limeContainer.parent

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

Lines changed: 1 addition & 3 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
@@ -70,7 +69,6 @@ internal class DartNameResolver(
7069
is LimeValue -> resolveValue(element)
7170
is LimeGenericType -> resolveGenericType(element)
7271
is LimeTypeRef -> resolveTypeRefName(element)
73-
is LimeTypeAlias -> resolveName(element.typeRef)
7472
is LimeType -> resolveType(element)
7573
is LimeNamedElement -> getPlatformName(element)
7674
else ->
@@ -207,7 +205,7 @@ internal class DartNameResolver(
207205

208206
private fun resolveTypeRefName(limeTypeRef: LimeTypeRef): String {
209207
val typeName = resolveName(limeTypeRef.type)
210-
val alias = limeTypeRef.type.actualType.external?.dart?.get(IMPORT_PATH_NAME)?.let { computeAlias(it) }
208+
val alias = limeTypeRef.type.external?.dart?.get(IMPORT_PATH_NAME)?.let { computeAlias(it) }
211209
val suffix = if (limeTypeRef.isNullable) "?" else ""
212210
return listOfNotNull(alias, typeName).joinToString(".") + suffix
213211
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ abstract class {{resolveName}} {{#if this.parent}}implements {{resolveName this.
4949
{{/ifPredicate}}
5050
}
5151

52+
{{#typeAliases}}
53+
{{>dart/DartTypeAlias}}
54+
{{/typeAliases}}
5255
{{#enumerations}}
5356
{{>dart/DartEnumeration}}
5457
{{/enumerations}}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ abstract class {{resolveName}} {{#if this.parent}}implements {{resolveName this.
6262
{{/ifPredicate}}
6363
}
6464

65+
{{#typeAliases}}
66+
{{>dart/DartTypeAlias}}
67+
{{/typeAliases}}
6568
{{#enumerations}}
6669
{{>dart/DartEnumeration}}
6770
{{/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)