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

Aliyun parser #138

Merged
merged 8 commits into from
Jul 23, 2022
Merged
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
21 changes: 21 additions & 0 deletions generator/generators/aliyun/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ClassData, extractSDKData, getAST } from '../../parsers/aliyun/parser';

export const generateAliyunClass = (
serviceClass: unknown,
serviceName: string
) => {
const sdkfile = serviceClass[Object.keys(serviceClass)[0]];
getAST(sdkfile)
.then(async result => {
const sdkClassAst = result;
try {
const classData: ClassData = extractSDKData(sdkClassAst, serviceClass);
classData.serviceName = serviceName;
} catch (err) {
console.error('Error : ', err);
}
})
.catch(error => {
console.error('Error : ', error);
});
};
3 changes: 3 additions & 0 deletions generator/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as yaml from 'js-yaml';

import { generateAliyunClass } from './generators/aliyun/generator';
import { generateAWSClass } from './generators/aws/generator';
import { generateAzureClass } from './generators/azure/generator';
import { generateDOClass } from './generators/do/generator';
Expand All @@ -18,6 +19,8 @@ try {
generateGCPClass(services[service][provider], service);
} else if (provider == 'DO') {
generateDOClass(services[service][provider], service);
} else if (provider === 'Ali') {
generateAliyunClass(services[service][provider], service);
}
});
});
Expand Down
8 changes: 8 additions & 0 deletions generator/node-cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ StorageBucket:
list: storage storage.d.ts getBuckets
upload: storage bucket.d.ts upload
makePublic: storage file.d.ts makePublic
Ali:
setRegion: oss index.d.ts setRegion
create: oss index.d.ts create
listBuckets: oss index.d.ts listBuckets
delete: oss index.d.ts delete
describeBucket: oss index.d.ts describeBucket
listBucketObjects: oss index.d.ts listBucketObjects
uploadLocalObject: oss index.d.ts uploadLocalObject

PaaS:
AWS:
Expand Down
1 change: 1 addition & 0 deletions generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@google-cloud/pubsub": "^2.1.0",
"@google-cloud/storage": "^5.1.1",
"@google-cloud/translate": "^6.0.0",
"aliyun-v2-typescript-sdk": "^0.1.1",
"aws-sdk": "^2.686.0",
"config": "^1.26.1",
"do-wrapper": "^4.5.1",
Expand Down
114 changes: 114 additions & 0 deletions generator/parsers/aliyun/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as fs from 'fs';
import * as path from 'path';
import { createSourceFile, ScriptTarget, SyntaxKind } from 'typescript';

export const getAST = (sdkFilePath: string) => {
return new Promise(async (resolve, reject) => {
const [module, rootFile, service] = sdkFilePath.split(' ');
try {
const file = path.join(
__dirname,
'../../../node_modules/aliyun-v2-typescript-sdk/dist/modules/',
module.toLowerCase(),
rootFile
);

const ast = createSourceFile(
file,
fs.readFileSync(file).toString(),
ScriptTarget.Latest,
true
);

let cloned = null;

await ast.forEachChild(child => {
if (SyntaxKind[child.kind] === 'ClassDeclaration') {
cloned = Object.assign({}, child);
}
});

if (!cloned) {
reject(new Error('Class not found!'));
} else {
resolve(cloned);
}
} catch (error) {
if (error.code === 'ENOENT') {
reject(new Error('File not found!'));
} else {
reject(error);
}
}
});
};

export const extractSDKData = (sdkClassAst, serviceClass): ClassData => {
const methods: FunctionData[] = [];
const functions = [];

Object.keys(serviceClass).forEach((key: string) => {
functions.push(serviceClass[key].split(' ')[2]);
});

sdkClassAst.members.forEach(method => {
if (method.name && functions.includes(method.name.text)) {
let name;
Object.keys(serviceClass).forEach((key: string) => {
if (serviceClass[key].split(' ')[2] === method.name.text) {
name = key;
}
});

const parameters = [];
method.parameters.forEach(param => {
if (param.name.text !== 'callback') {
const parameter = {
name: param.name.text,
optional: param.questionToken ? true : false,
type: SyntaxKind[param.type.kind],
typeName: null,
};

if (parameter.type === 'TypeReference' && param.type.typeName) {
parameter.typeName = param.type.typeName.text;
}

parameters.push(parameter);
}
});

methods.push({
functionName: name.toString(),
SDKFunctionName: method.name.text.toString(),
params: parameters,
});
}
});

const classData: ClassData = {
className: sdkClassAst.name.text,
functions: methods,
serviceName: null,
};

return classData;
};

export interface ClassData {
className: string;
functions: FunctionData[];
serviceName: string;
}

interface FunctionData {
functionName: string;
SDKFunctionName: string;
params: param[];
}

interface param {
name: string;
type: string;
typeName: string;
}
26 changes: 26 additions & 0 deletions generator/test/parsers/aliyun/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai';
import { SyntaxKind } from 'typescript';

import { getAST } from '../../../parsers/aliyun/parser';

describe('Aliyun getAST Implementation', () => {
const sdkFilePath = 'oss index.d.ts setRegion';
const invalidPath = 'oss unknown.d.ts setRegion';
context('With existing file', () => {
it('Should return Abstract syntax tree of the class', async () => {
const ast: any = await getAST(sdkFilePath);
expect(ast).to.be.an('object');
expect(SyntaxKind[ast.kind] === 'ClassDeclaration').to.be.true;
});
});

context('With non-existing file', () => {
it('should return File not found Error', async () => {
try {
await getAST(invalidPath);
} catch (error) {
expect(error.message).to.eql('File not found!');
}
});
});
});