Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support iterator type #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
'artifactId', 'version'
];
needParamsKeys.forEach(key => {
if (params[key] === undefined) {

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (10.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (10.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected use of undefined

Check warning on line 73 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected use of undefined
params[key] = '';
}
});
Expand Down Expand Up @@ -176,8 +176,15 @@

visitFor(ast, level) {
assert.equal(ast.type, 'for');
if (ast.list.inferred.type === 'iterator') {
this.emit(`ArrayList<`, level);
this.visitType(ast.list.inferred.valueType);
this.emit(`> _result = new ArrayList<`);
this.visitType(ast.list.inferred.valueType);
this.emit(`>();\n`);
}
this.emit(`for (`, level);
this.visitType(ast.list.inferred.itemType);
this.visitType(ast.list.inferred.itemType || ast.list.inferred.valueType);
this.emit(` ${_name(ast.id)} : `);
this.visitExpr(ast.list, level + 1);
this.emit(') {\n');
Expand Down Expand Up @@ -524,6 +531,8 @@
} else if (ast.type === 'module_instance') {
let className = this.imports[_name(ast)].className || 'Client';
this.emit(`${this.imports[_name(ast)].package}.${className}`);
} else if (ast.type === 'iterator') {
this.emit(`Iterable<${_type(ast.valueType.lexeme || ast.valueType.name)}>`);
} else {
if (isSubType) {
this.emit(collectionType(_type(ast.lexeme || ast.name)));
Expand Down Expand Up @@ -601,6 +610,10 @@
this.visitFor(ast, level);
} else if (ast.type === 'try') {
this.visitTry(ast, level);
} else if (ast.type === 'yield') {
this.emit(`_result.add(`, level);
this.visitExpr(ast.expr, level);
this.emit(`);\n`);
} else {
this.emit(``, level);
this.visitExpr(ast, level);
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/iterator/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.main;

import com.aliyun.tea.*;

public class Client {

public Client() throws Exception {
}


public Iterable<String> test1() throws Exception {
}

public Iterable<String> test2() throws Exception {
Iterable<String> it = this.test1();
ArrayList<String> _result = new ArrayList<String>();
for (String test : it) {
_result.add(test);
}
}

public String test3(Iterable<String> iter) throws Exception {
Iterable<String> it = iter;
String str = "";
ArrayList<String> _result = new ArrayList<String>();
for (String i : it) {
str = "" + str + ", " + i + "";
}
return str;
}

public void test4(Object test) throws Exception {
}

public void test5(Iterable<String> iter) throws Exception {
test3(iter);
this.test4(iter);
}
}
13 changes: 13 additions & 0 deletions test/fixtures/iterator/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scope": "darabonba",
"name": "main",
"version": "0.0.1",
"main": "./main.dara",
"libraries": {},
"java": {
"package": "com.aliyun.main",
"className": "Client"
},
"releases": {
}
}
26 changes: 26 additions & 0 deletions test/fixtures/iterator/main.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
init(){}

async function test1(): iterator[string];
async function test2(): iterator[string]{
var it:iterator[string] = test1();
for(var test : it) {
yield test;
}
}

async function test3(iter: iterator[string]): string{
var it = iter;
var str : string = '';
for (var i : it) {
str = `${str}, ${i}`;
}
return str;
}

async function test4(test: any): void{
}

async function test5(iter: iterator[string]): void{
// test3(iter);
test4(iter);
}
11 changes: 11 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,15 @@ describe('new Generator', function () {
await msleep(500);
assert.deepStrictEqual(fs.readFileSync(clientPath, 'utf8'), expected);
});

it('iterator should ok', function () {
const outputDir = path.join(__dirname, 'output/iterator');
const mainFilePath = path.join(__dirname, 'fixtures/iterator/main.dara');
const pkgContent = fs.readFileSync(path.join(__dirname, 'fixtures/iterator/Darafile'), 'utf8');
const pkg = JSON.parse(pkgContent);
check(mainFilePath, outputDir, path.join(__dirname, 'fixtures/iterator/Client.java'), 'src/main/java/com/aliyun/test/Client.java', {
pkgDir: path.join(__dirname, 'fixtures/iterator'),
...pkg
});
});
});
Loading