Skip to content

Commit

Permalink
support json readpath in ts
Browse files Browse the repository at this point in the history
  • Loading branch information
PanPanZou committed Jan 21, 2025
1 parent 5f4bdeb commit 688a721
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
6 changes: 4 additions & 2 deletions ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"dependencies": {
"@alicloud/tea-typescript": "^1.5.1",
"@darabonba/typescript": "^1.0.0",
"kitx": "^2.0.0"
"kitx": "^2.0.0",
"@types/jsonpath": "^0.2.4",
"jsonpath": "^1.1.1"
},
"files": [
"dist",
"src"
],
"repository": "[email protected]:aliyun/tea-util.git"
}
}
45 changes: 45 additions & 0 deletions ts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 +93,10 @@ export default class Client {
return JSON.parse(text);
}

static readPath(obj: Object, path: string) {
return jp.query(parseToMap(obj), path);
}

static async readAsBytes(stream: Readable): Promise<Buffer> {
return await read(stream);
}
Expand Down
29 changes: 28 additions & 1 deletion ts/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ 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');
});

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

0 comments on commit 688a721

Please sign in to comment.