Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
6 changes: 4 additions & 2 deletions ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"devDependencies": {
"@types/mocha": "^7.0.1",
"@types/node": "^12.12.26",
"@types/jsonpath": "^0.2.4",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"source-map-support": "^0.5.16",
Expand All @@ -23,11 +24,12 @@
"dependencies": {
"@alicloud/tea-typescript": "^1.5.1",
"@darabonba/typescript": "^1.0.0",
"kitx": "^2.0.0"
"kitx": "^2.0.0",
"jsonpath": "^1.1.1"
},
"files": [
"dist",
"src"
],
"repository": "[email protected]:aliyun/tea-util.git"
}
}
48 changes: 48 additions & 0 deletions ts/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable } from 'stream';
import * as $dara from '@darabonba/typescript';
import * as $tea from '@alicloud/tea-typescript';
import { JSONPath } from 'jsonpath';
import * as kitx from 'kitx';
import querystring from 'querystring';
import { platform, arch } from 'os';
Expand Down Expand Up @@ -42,6 +43,47 @@ function read(readable: Readable): Promise<Buffer> {
});
}

function parseToMap(input: any): { [key: string]: any } {
return toMap(input);
}

function isObjectOrArray(t: any): boolean {
return Array.isArray(t) || (t instanceof Object && typeof t !== 'function');
}

function toMap(input: any) {
if (!isObjectOrArray(input)) {
return null;
} else if (input instanceof $tea.Model) {
return $tea.toMap(input);
} else if (input && input.toMap && typeof input.toMap === 'function') {
// 解决跨版本 Model 不互认的问题
return input.toMap();
} else if (Array.isArray(input)) {
const result = [];
input.forEach((value) => {
if (isObjectOrArray(value)) {
result.push(toMap(value));
} else {
result.push(value);
}
});

return result;
} else if (input instanceof Object) {
const result = {};
Object.entries(input).forEach(([key, value]) => {
if (isObjectOrArray(value)) {
result[key] = toMap(value);
} else {
result[key] = value;
}
});

return result;
}
}

export default class Client {

static toString(buff: Buffer): string {
Expand All @@ -52,6 +94,12 @@ export default class Client {
return JSON.parse(text);
}

static readPath(obj: Object, path: string) {
const jp = new JSONPath;
const objArr = jp.query(parseToMap(obj), path, 1);
return objArr[0];
}

static async readAsBytes(stream: Readable): Promise<Buffer> {
return await read(stream);
}
Expand Down
75 changes: 74 additions & 1 deletion ts/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ import assert from 'assert';
import 'mocha';
import { platform, arch } from 'os';

class Context extends $tea.Model {
str: string;
testBool: boolean;
contextInteger: number;
contextLong: number;
contextFloat: number;
contextDouble: number;
contextListLong: number[];
listList: number[][];
integerListMap: { [key: string]: number[] };

static names(): { [key: string]: string } {
return {
str: 'testStr',
testBool: 'testBool',
contextInteger: 'contextInteger',
contextLong: 'contextLong',
contextFloat: 'contextFloat',
contextDouble: 'contextDouble',
contextListLong: 'contextListLong',
listList: 'listList',
integerListMap: 'integerListMap',
};
}

static types(): { [key: string]: any } {
return {
str: 'string',
testBool: 'boolean',
contextInteger: 'number',
contextLong: 'number',
contextFloat: 'number',
contextDouble: 'number',
contextListLong: { 'type': 'array', 'itemType': 'number' },
listList: { 'type': 'array', 'itemType': { 'type': 'array', 'itemType': 'number' } },
integerListMap: { 'type': 'map', 'keyType': 'string', 'valueType': { 'type': 'array', 'itemType': 'number' } },
};
}

constructor(map?: { [key: string]: any }) {
super(map);
}
}

class MyReadable extends Readable {
value: Buffer

Expand Down Expand Up @@ -41,6 +85,35 @@ describe('Tea Util', function () {
assert.deepStrictEqual(Client.toJSONString({ 'str': 'test', 'number': 1, 'bool': false, 'null': null }), '{"str":"test","number":1,"bool":false,"null":null}');
});

it('parseJSON should ok', function () {
assert.deepStrictEqual(Client.parseJSON('{}'), {});
assert.deepStrictEqual(Client.parseJSON('{"str":"test","number":1,"bool":false,"null":null}'), { 'str': 'test', 'number': 1, 'bool': false, 'null': null });
assert.deepStrictEqual(Client.parseJSON('[]'), []);
assert.deepStrictEqual(Client.parseJSON('1'), 1);
assert.deepStrictEqual(Client.parseJSON('true'), true);
assert.deepStrictEqual(Client.parseJSON('null'), null);
});

it('readPath should ok', function () {
const context: Context = new Context({
str: 'test',
testBool: true,
contextInteger: 123,
contextLong: 123,
contextFloat: 3.456,
contextDouble: 1.123,
contextListLong: [123, 456],
listList: [[123, 456], [789, 123]],
integerListMap: {
'integerList': [123, 456],
},
});

assert.deepStrictEqual(Client.readPath(context, '$.testStr'), 'test');
assert.deepStrictEqual(Client.readPath(context, '$.listList[0]'), [123, 456]);
assert.deepStrictEqual(Client.readPath(context, '$.integerListMap'), { integerList: [123, 456] });
});

it('defaultString should ok', function () {
assert.deepStrictEqual(Client.defaultString('', 'default'), 'default');
assert.deepStrictEqual(Client.defaultString('input', 'default'), 'input');
Expand Down Expand Up @@ -393,4 +466,4 @@ describe('Tea Util', function () {
const result = Client.assertAsReadable(readable);
assert.deepStrictEqual(result, readable);
});
});
});
Loading