Skip to content

Commit 688a721

Browse files
committed
support json readpath in ts
1 parent 5f4bdeb commit 688a721

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

ts/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
"dependencies": {
2424
"@alicloud/tea-typescript": "^1.5.1",
2525
"@darabonba/typescript": "^1.0.0",
26-
"kitx": "^2.0.0"
26+
"kitx": "^2.0.0",
27+
"@types/jsonpath": "^0.2.4",
28+
"jsonpath": "^1.1.1"
2729
},
2830
"files": [
2931
"dist",
3032
"src"
3133
],
3234
"repository": "git@github.com:aliyun/tea-util.git"
33-
}
35+
}

ts/src/client.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ function read(readable: Readable): Promise<Buffer> {
4242
});
4343
}
4444

45+
function parseToMap(input: any): { [key: string]: any } {
46+
return toMap(input);
47+
}
48+
49+
function isObjectOrArray(t: any): boolean {
50+
return Array.isArray(t) || (t instanceof Object && typeof t !== 'function');
51+
}
52+
53+
function toMap(input: any) {
54+
if (!isObjectOrArray(input)) {
55+
return null;
56+
} else if (input instanceof $tea.Model) {
57+
return $tea.toMap(input);
58+
} else if (input && input.toMap && typeof input.toMap === 'function') {
59+
// 解决跨版本 Model 不互认的问题
60+
return input.toMap();
61+
} else if (Array.isArray(input)) {
62+
const result = [];
63+
input.forEach((value) => {
64+
if (isObjectOrArray(value)) {
65+
result.push(toMap(value));
66+
} else {
67+
result.push(value);
68+
}
69+
});
70+
71+
return result;
72+
} else if (input instanceof Object) {
73+
const result = {};
74+
Object.entries(input).forEach(([key, value]) => {
75+
if (isObjectOrArray(value)) {
76+
result[key] = toMap(value);
77+
} else {
78+
result[key] = value;
79+
}
80+
});
81+
82+
return result;
83+
}
84+
}
85+
4586
export default class Client {
4687

4788
static toString(buff: Buffer): string {
@@ -52,6 +93,10 @@ export default class Client {
5293
return JSON.parse(text);
5394
}
5495

96+
static readPath(obj: Object, path: string) {
97+
return jp.query(parseToMap(obj), path);
98+
}
99+
55100
static async readAsBytes(stream: Readable): Promise<Buffer> {
56101
return await read(stream);
57102
}

ts/test/client.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ describe('Tea Util', function () {
4141
assert.deepStrictEqual(Client.toJSONString({ 'str': 'test', 'number': 1, 'bool': false, 'null': null }), '{"str":"test","number":1,"bool":false,"null":null}');
4242
});
4343

44+
it('parseJSON should ok', function () {
45+
assert.deepStrictEqual(Client.parseJSON('{}'), {});
46+
assert.deepStrictEqual(Client.parseJSON('{"str":"test","number":1,"bool":false,"null":null}'), { 'str': 'test', 'number': 1, 'bool': false, 'null': null });
47+
assert.deepStrictEqual(Client.parseJSON('[]'), []);
48+
assert.deepStrictEqual(Client.parseJSON('1'), 1);
49+
assert.deepStrictEqual(Client.parseJSON('true'), true);
50+
assert.deepStrictEqual(Client.parseJSON('null'), null);
51+
});
52+
53+
it('readPath should ok', function () {
54+
const context: Context = new Context({
55+
str: 'test',
56+
testBool: true,
57+
contextInteger: 123,
58+
contextLong: 123,
59+
contextFloat: 3.456,
60+
contextDouble: 1.123,
61+
contextListLong: [123, 456],
62+
listList: [[123, 456], [789, 123]],
63+
integerListMap: {
64+
'integerList': [123, 456],
65+
},
66+
});
67+
68+
assert.deepStrictEqual(Client.readPath(context, 'testStr'), 'test');
69+
});
70+
4471
it('defaultString should ok', function () {
4572
assert.deepStrictEqual(Client.defaultString('', 'default'), 'default');
4673
assert.deepStrictEqual(Client.defaultString('input', 'default'), 'input');
@@ -393,4 +420,4 @@ describe('Tea Util', function () {
393420
const result = Client.assertAsReadable(readable);
394421
assert.deepStrictEqual(result, readable);
395422
});
396-
});
423+
});

0 commit comments

Comments
 (0)