feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function filterKeys(obj: Record<string, any>, keys: string[]) {
const newObj: Record<string, any> = {};
for (const key of keys) {
newObj[key] = obj[key];
}
return newObj;
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable */
import * as t from '../src/proto';
const content = `
syntax = 'proto3';
// c1
message Foo { // c2
// c3
int32 code = 1; // c4
// c5
string content = 2;
// c6
string message = 3; // c7
}
`;
const document = t.parse(content);
console.log(JSON.stringify(document, null, 2));

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable */
import * as t from '../src/thrift';
import * as path from 'path';
const idl = `
/*
*/
struct UserDeleteDataMap {
1: required UserDeleteData DeleteData
2: string k2 (go.tag = 'json:\\"-\\"')
}
/*
We
*/
enum AvatarMetaType {
UNKNOWN = 0, // 没有数据, 错误数据或者系统错误降级
RANDOM = 1, // 在修改 or 创建时,用户未指定 name 或者选中推荐的文字时,程序随机选择的头像
}
`;
const document = t.parse(idl);
var c = path.join('a/b.thrift', './c.thrift');
console.log(JSON.stringify(document, null, 2));

View File

@@ -0,0 +1,213 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable */
import * as t from '../src/unify/index';
import * as path from 'path';
// const root = 'test/idl';
// const idl = path.resolve(process.cwd(), root, 'dep/common.thrift');
// const document = t.parse(idl, {
// root,
// namespaceRefer: true,
// cache: false,
// });
// // document.body[4]
// console.log('#gg', document);
const indexThriftContent = `
namespace java com.unify_idx
include 'unify_dependent1.thrift'
typedef unify_dependent1.Foo TFoo
enum Gender {
// male
Male // male tail
// female
Female // female tail
// mix
Mix
}
// const map<Gender, string> genderMap = {
// Gender.Male: '男性',
// Gender.Female: '女性',
// }
union FuncRequest {
1: unify_dependent1.Foo r_key1
2: TFoo list (go.tag = "json:\\"-\\"")
}
`;
const dep1ThriftContent = `
namespace js unify_dep1
typedef Foo Foo1
struct Foo {
1: string f_key1
}
`;
// const fileContentMap = {
// 'unify_index.thrift': indexThriftContent,
// 'unify_dependent1.thrift': dep1ThriftContent,
// };
const indexProtoContent = `
syntax = "proto3";
import "unify.dependent1.proto";
package a.b.c;
message Request {
repeated string key1 = 1[(api.key) = 'f'];
a.b.Foo key3 = 3;
// message Sub {
// enum Num {
// ONE = 1;
// }
// // string k1 = 1;
// Num k2 = 2;
// }
// Sub key2 = 2;
}
`;
const dep1ProtoContent = `
syntax = "proto3";
package a.b;
message Foo {
string f_key1 = 1;
message SubF {}
SubF f_key2 = 2;
}
`;
const fileContentMap = {
'unify_index.proto': indexProtoContent,
'unify.dependent1.proto': dep1ProtoContent,
};
// const document = t.parse(
// 'unify_index.proto',
// {
// root: '.',
// // namespaceRefer: true,
// },
// fileContentMap
// );
// // document.body[4]
// console.log(document);
const baseContent = `
syntax = "proto3";
package a.b;
message Bar {
message BarSub {
enum NumBar {
ONE = 1;
}
}
}
`;
const extraContent = `
syntax = "proto3";
package a.b;
message Extra {}
`;
const indexContent = `
syntax = "proto3";
package a.b;
import 'base.proto';
import 'extra.proto';
message Foo {
// message FooSub {
// enum NumFoo {
// TWO = 2;
// }
// }
// Foo.FooSub.NumFoo k1 = 1;
// FooSub.NumFoo k2 = 2;
// FooSub k3 = 3;
// repeated FooSub k4 = 4;
// map<string, FooSub.NumFoo> k5 = 5;
// Bar.BarSub.NumBar k10 = 10;
Bar.BarSub k11 = 11;
// repeated Bar.BarSub.NumBar k12 = 12;
// map<string, Bar.BarSub> k13 = 13;
}
// message Bar {
// message BarSub {
// enum NumBar {
// ONE = 1;
// }
// }
// }
`;
const document = t.parse(
'index.proto',
{ cache: false },
{
'index.proto': indexContent,
'base.proto': baseContent,
'extra.proto': extraContent,
}
);
const statement = document.statements[0] as t.InterfaceWithFields;
console.log(statement);
// const baseContent = `
// syntax = "proto3";
// package a.b;
// message Common {
// }
// `;
// const indexContent = `
// syntax = "proto3";
// message Foo {
// google.protobuf.Any k1 = 1;
// }
// `;
// const document = t.parse(
// 'index.proto',
// { cache: false },
// {
// 'index.proto': indexContent,
// // 'base.proto': baseContent,
// }
// );
// const { functions } = document.statements[0] as t.ServiceDefinition;
// console.log(functions);

View File

@@ -0,0 +1,3 @@
syntax = "proto3";
message Base {}

View File

@@ -0,0 +1 @@
struct Base {}

View File

@@ -0,0 +1,3 @@
syntax = "proto3";
message Basee {}

View File

@@ -0,0 +1 @@
struct Basee {}

View File

@@ -0,0 +1,7 @@
syntax = "proto3";
package common;
import "base.proto";
import "basee.proto";
message Common {}

View File

@@ -0,0 +1,6 @@
namespace go common
include 'base.thrift'
include 'basee.thrift'
struct Common {}

View File

@@ -0,0 +1,4 @@
syntax = "proto3";
message Foo {
string k1 = 1;,
}

View File

@@ -0,0 +1,3 @@
struct Foo {
1: string k1,,
}

View File

@@ -0,0 +1,5 @@
syntax = "proto3";
service Foo {
option (api.uri_prefix) = "//example.com";
}

View File

@@ -0,0 +1,5 @@
service Foo {
} (
api.uri_prefix = 'https://example.com'
)

View File

@@ -0,0 +1,11 @@
syntax = "proto3";
import "unify_dependent2.proto";
import "./dep/common.proto";
package unify_dep1;
message Foo {
string f_key1 = 1;
common.Common f_key2 = 2;
}

View File

@@ -0,0 +1,12 @@
include 'unify_dependent2.thrift'
include './dep/common.thrift'
namespace js unify_dep1
typedef Foo Foo1
struct Foo {
1: string f_key1
2: common.Common f_key2
}

View File

@@ -0,0 +1,9 @@
syntax = "proto3";
import "./unify_dependent1.proto";
package unify_idx;
enum Number {
ONE = 1;
}

View File

@@ -0,0 +1,5 @@
include './unify_dependent1.thrift'
enum Number {
ONE = 1,
}

View File

@@ -0,0 +1,2 @@
include "./unify_base.thrift"
include "./unify_base1.thrift"

View File

@@ -0,0 +1,29 @@
syntax = "proto3";
import "./unify_dependent1.proto";
import "unify_dependent2.proto";
package unify_idx;
// c0
enum Gender {
// c1
MALE = 1; // c2
// c3
FEMAL = 2; // c4
}
/* cm1 */
message Request {
// cm2
// repeated string key1 = 1[(api.key) = 'f'];
// unify_dep1.Foo key2 = 2;
Number key3 = 3 [(api.position) = 'query'];
}
service Example {
option (api.uri_prefix) = "//example.com";
rpc Biz1(Request) returns (Number) {
option (api.uri) = '/api/biz1';
}
}

View File

@@ -0,0 +1,21 @@
namespace go unify_idx
include './unify_dependent1.thrift'
include 'unify_dependent2.thrift'
typedef unify_dependent1.Foo TFoo
union FuncRequest {
1: unify_dependent1.Foo r_key1
2: TFoo r_key2
}
struct FuncResponse {
1: unify_dependent2.Number key2
}
service Example {
FuncResponse Func(1: FuncRequest req)
} (
api.uri_prefix = 'https://example.com'
)

View File

@@ -0,0 +1,7 @@
syntax = "proto3";
import "base.proto";
message Foo {
Base key1 = 1;
}

View File

@@ -0,0 +1,5 @@
include 'base.thrift'
struct Foo {
1: base.Base key1
}

View File

@@ -0,0 +1,12 @@
syntax = "proto3";
message Common {}
service Example {
rpc Func1 (Common) returns (Common) {
option (google.api.http) = {
get: "/ezo/web/v1/user_camp_result"
body: "*"
};
}
}

View File

@@ -0,0 +1,140 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/proto';
describe('ferry-parser', () => {
describe('proto field', () => {
it('should convert message field extenstions', () => {
const idl = `
syntax = "proto3";
enum Numbers {
ONE = 1;
}
message Foo {
string k1 = 1 [(api.position) = "query"];
string k2 = 2 [(api.position) = 'body'];
string k3 = 3 [(api.position) = 'path'];
string k4 = 4 [(api.position) = 'header'];
string k5 = 5 [(api.position) = 'entire_body'];
string k6 = 6 [(api.position) = 'raw_body', (aapi.position) = 'raw_body'];
string k7 = 7 [(api.position) = 'status_code', (api.positionn) = 'raw_body'];
string k10 = 10 [(api.key) = 'key10'];
string k11 = 11 [(api.key) = 'k11'];
bytes k12 = 12 [(api.web_type) = 'File'];
int32 k21 = 21 [(api.query) = 'k21'];
int32 k22 = 22 [(api.body) = 'k22'];
int32 k23 = 23 [(api.path) = 'k23'];
int32 k24 = 24 [(api.header) = 'k24'];
int32 k25 = 25 [(api.entire_body) = 'key25'];
int32 k26 = 26 [(api.raw_body) = 'key_26'];
int32 k27 = 27 [(api.status_code) = 'key-27'];
int32 k31 = 31 [(api.query) = 'key31', (api.web_type) = 'number', (api.position) = ''];
int32 k32 = 32 [(api.position) = 'body', (api.key)='key32', (api.value_type) = 'any'];
int32 k33 = 33 [(api.method) = 'POST', (api.position) = 'QUERY'];
int32 k34 = 34 ;
Numbers k35 = 35 [(api.position) = 'path'];
}
`;
const expected = [
{ position: 'query' },
{ position: 'body' },
{ position: 'path' },
{ position: 'header' },
{ position: 'entire_body' },
{ position: 'raw_body' },
{ position: 'status_code' },
{ key: 'key10' },
{},
{ web_type: 'File' },
{ position: 'query' },
{ position: 'body' },
{ position: 'path' },
{ position: 'header' },
{ position: 'entire_body', key: 'key25' },
{ position: 'raw_body', key: 'key_26' },
{ position: 'status_code', key: 'key-27' },
{ position: 'query', key: 'key31', web_type: 'number' },
{ position: 'body', key: 'key32', value_type: 'any' },
{},
undefined,
{ position: 'path' },
];
const document = t.parse(idl);
const Foo = (document.root.nested || {}).Foo as t.MessageDefinition;
const extensionConfigs = Object.values(Foo.fields).map(
field => field.extensionConfig,
);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert message field extenstions using old rules', () => {
const idl = `
syntax = "proto3";
message Foo {
int32 k1 = 1 [(api_req).query = 'k1'];
int32 k2 = 2 [(api_req).body = 'k2'];
int32 k3 = 3 [(api_req).path = 'k3'];
int32 k4 = 4 [(api_req).header = 'k4'];
int32 k6 = 5 [(api_req).raw_body = 'key5'];
int32 k5 = 6 [(api_resp).header = 'key6'];
int32 k7 = 7 [(api_resp).http_code = 'key7'];
string k8 = 8 [(api_resp).body = 'k8'];
}
`;
const expected = [
{ position: 'query' },
{ position: 'body' },
{ position: 'path' },
{ position: 'header' },
{ position: 'raw_body', key: 'key5' },
{ position: 'header', key: 'key6' },
{},
{ position: 'body' },
];
const document = t.parse(idl);
const Foo = (document.root.nested || {}).Foo as t.MessageDefinition;
const extensionConfigs = Object.values(Foo.fields).map(
field => field.extensionConfig,
);
return expect(extensionConfigs).to.eql(expected);
});
it('should throw an error when using invalid type for a path parameter', () => {
const idl = `
syntax = "proto3";
message Foo {
bool k1 = 1 [(api.position) = "path"];
}
`;
try {
t.parse(idl);
} catch (err) {
const { message } = err;
const expected =
"the type of path parameter 'k1' in 'Foo' should be string or integer";
return expect(message).to.equal(expected);
}
return expect(true).to.equal(false);
});
});
});

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'path';
import * as t from '../src/proto';
describe('ferry-parser', () => {
describe('proto index', () => {
it('should convert the file content', () => {
const idl = path.resolve(__dirname, 'idl/index.proto');
const expected = { uri_prefix: '//example.com' };
const document = t.parse(idl);
const Foo = (document.root.nested || {}).Foo as t.ServiceDefinition;
return expect(Foo.extensionConfig).to.eql(expected);
});
it('should throw an error due to invalid file path', () => {
const idl = path.resolve(__dirname, 'idl/indexx.proto');
try {
t.parse(idl);
} catch (err) {
const { message } = err;
return expect(message).to.includes('no such file:');
}
return expect(true).to.equal(false);
});
it('should throw an syntax error', () => {
const idl = `
syntax = "proto3";
message Foo {
string k1 = 1;,
}
`;
const expected = "illegal token ','(source:4:0)";
try {
t.parse(idl);
} catch (err) {
const { message } = err;
return expect(message).to.equal(expected);
}
return expect(true).to.equal(false);
});
it('should throw an syntax error in the file content', () => {
const idl = path.resolve(__dirname, 'idl/error.proto');
const expected = '__tests__/idl/error.proto:3:0)';
try {
t.parse(idl);
} catch (err) {
const { message } = err;
return expect(message).to.includes(expected);
}
return expect(true).to.equal(false);
});
});
});

View File

@@ -0,0 +1,108 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/proto';
describe('ferry-parser', () => {
describe('proto method', () => {
it('should convert method extenstions', () => {
const idl = `
syntax = 'proto3';
message BizRequest {}
message BizResponse {}
service Foo {
rpc Biz1(BizRequest) returns (BizResponse) {
option (api.uri) = '/api/biz1';
}
rpc Biz2(BizRequest) returns (BizResponse) {
option (api.method) = "POST";
option (api.uri) = "/api/biz2";
option (api.serializer) = "json";
option (api.group) = 'user';
}
rpc Biz3(BizRequest) returns (BizResponse) {
option (api.get) ='/api/biz3';
option (api.serializer) ='form';
}
rpc Biz4(BizRequest) returns (BizResponse) {
option (api.post) ='/api/biz4';
option (api.serializer) ='urlencoded';
}
rpc Biz5(BizRequest) returns (BizResponse) {
option (api.put) ='/api/biz5';
}
rpc Biz6(BizRequest) returns (BizResponse) {
option (api.delete) ='/api/biz6';
}
rpc Biz7(BizRequest) returns (BizResponse);
}
`;
const expected = [
{ uri: '/api/biz1' },
{
method: 'POST',
uri: '/api/biz2',
serializer: 'json',
group: 'user',
},
{ method: 'GET', uri: '/api/biz3', serializer: 'form' },
{ method: 'POST', uri: '/api/biz4', serializer: 'urlencoded' },
{ method: 'PUT', uri: '/api/biz5' },
{ method: 'DELETE', uri: '/api/biz6' },
undefined,
];
const document = t.parse(idl);
const Foo = (document.root.nested || {}).Foo as t.ServiceDefinition;
const extensionConfigs = Object.values(Foo.methods).map(
func => func.extensionConfig,
);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert method extenstions using old rules', () => {
const idl = `
syntax = 'proto3';
message BizRequest {}
message BizResponse {}
service Foo {
rpc Biz1(BizRequest) returns (BizResponse) {
option (api_method).get = "/api/biz1";
option (api_method).serializer = "json";
}
rpc Biz2(BizRequest) returns (BizResponse) {
option (pb_idl.api_method).post = "/api/biz2";
option (pb_idl.api_method).serializer = "form";
}
}
`;
const expected = [
{ method: 'GET', uri: '/api/biz1', serializer: 'json' },
{ method: 'POST', uri: '/api/biz2', serializer: 'form' },
];
const document = t.parse(idl);
const Foo = (document.root.nested || {}).Foo as t.ServiceDefinition;
const extensionConfigs = Object.values(Foo.methods).map(
func => func.extensionConfig,
);
return expect(extensionConfigs).to.eql(expected);
});
});
});

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/proto';
describe('ferry-parser', () => {
describe('proto service', () => {
it('should convert service extenstions', () => {
const idl = `
syntax = "proto3";
service Foo {
option (api.uri_prefix) = "//example.com";
}
`;
const expected = { uri_prefix: '//example.com' };
const document = t.parse(idl);
const Foo = (document.root.nested || {}).Foo as t.ServiceDefinition;
return expect(Foo.extensionConfig).to.eql(expected);
});
it('should convert service extenstions with package', () => {
const idl = `
syntax = "proto3";
package example;
service Foo {
option (api.uri_prefix) = "//example.com";
}
`;
const expected = { uri_prefix: '//example.com' };
const document = t.parse(idl);
const Foo = ((document.root.nested || {}).example.nested || {})
.Foo as t.ServiceDefinition;
return expect(Foo.extensionConfig).to.eql(expected);
});
});
});

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/thrift';
describe('ferry-parser', () => {
describe('thrift enum', () => {
it('should convert enum member comments', () => {
const idl = `
enum Bar {
// c1
ONE = 1, // c2
/* c3 */
TWO = 2, /* c4 */
// c5
/* c6 */
THTEE = 3, // c7
/* c8
c9 */
FOUR = 4
// c10
FIVE = 5; /* c11 */
}
`;
const expected = [
['c1', 'c2'],
[['c3'], ['c4']],
['c5', ['c6'], 'c7'],
[['c8', ' c9']],
['c10', ['c11']],
];
const document = t.parse(idl);
const { members } = document.body[0] as t.EnumDefinition;
const comments = members.map(member =>
member.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
});
});

View File

@@ -0,0 +1,226 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/thrift';
describe('ferry-parser', () => {
describe('thrift field', () => {
it('should convert struct field extenstions', () => {
const idl = `
enum Numbers {
ONE = 1
}
struct Foo {
1: string k1 (api.position = "query")
2: string k2 (api.position = 'body', aapi.position = 'query')
3: string k3 (api.position = 'path', api.positionn = 'query')
4: string k4 (api.position = 'header')
5: string k5 (api.position = 'entire_body')
6: string k6 (api.position = 'raw_body')
7: string k7 (api.position = 'status_code')
10: string k10 (api.key = 'key10')
11: string k11 (api.key = 'k11')
12: binary k12 (api.web_type = 'File')
13: string k13 (api.value_type = 'any')
14: list<string> k14 (api.value_type = 'any')
21: i32 k21 (api.query = 'k21[]')
22: i32 k22 (api.body = 'k22')
23: i32 k23 (api.path = 'k23')
24: i32 k24 (api.header = 'k24')
25: i32 k25 (api.entire_body = 'key25')
26: i32 k26 (api.raw_body = 'key_26')
27: i32 k27 (api.status_code = 'key-27')
31: i32 k31 (api.query = 'key31', api.web_type = 'number', api.position = '')
32: i32 k32 (api.position = 'body', api.key='key32', api.value_type = 'any')
33: i64 k33 (api.body="kk33, omitempty")
34: Numbers k34 (api.position = 'path')
}
`;
const expected = [
{ position: 'query' },
{ position: 'body' },
{ position: 'path' },
{ position: 'header' },
{ position: 'entire_body' },
{ position: 'raw_body' },
{ position: 'status_code' },
{ key: 'key10' },
{},
{ web_type: 'File' },
{ value_type: 'any' },
{ value_type: 'any' },
{ position: 'query' },
{ position: 'body' },
{ position: 'path' },
{ position: 'header' },
{ position: 'entire_body', key: 'key25' },
{ position: 'raw_body', key: 'key_26' },
{ position: 'status_code', key: 'key-27' },
{ position: 'query', key: 'key31', web_type: 'number' },
{ position: 'body', key: 'key32', value_type: 'any' },
{ position: 'body', key: 'kk33', tag: 'omitempty' },
{ position: 'path' },
];
const document = t.parse(idl);
const { fields } = document.body[1] as t.InterfaceWithFields;
const extensionConfigs = fields.map(field => field.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert union field extenstions', () => {
const idl = `
union Foo {
1: string k1 (api.position = "query")
}
`;
const expected = [{ position: 'query' }];
const document = t.parse(idl, { reviseTailComment: false });
const { fields } = document.body[0] as t.InterfaceWithFields;
const extensionConfigs = fields.map(field => field.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert struct field extenstions using agw specification', () => {
const idl = `
struct Foo {
1: string k1 (agw.source = 'query')
2: string k2 (agw.source = 'body')
3: string k3 (agw.source = 'path')
4: string k4 (agw.source = 'header')
5: string k5 (agw.source = 'raw_body')
6: string k6 (agw.target = 'header')
7: string k7 (agw.target = 'body')
7: string k7 (agw.target = 'http_code')
10: string k10 (agw.key = 'key10')
}
`;
const expected = [
{ position: 'query' },
{ position: 'body' },
{ position: 'path' },
{ position: 'header' },
{ position: 'raw_body' },
{ position: 'header' },
{ position: 'body' },
{ position: 'status_code' },
{ key: 'key10' },
];
const document = t.parse(idl);
const { fields } = document.body[0] as t.InterfaceWithFields;
const extensionConfigs = fields.map(field => field.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert struct field extenstions using golang tag', () => {
const idl = `
struct Foo {
1: string k1 (go.tag = "json:\\"key1\\"")
2: string k2 (go.tag = 'json:"key2,omitempty"')
3: string k3 (go.tag = 'jsonn:"key2,omitempty"')
}
`;
const expected = [{ key: 'key1' }, { key: 'key2', tag: 'omitempty' }, {}];
const document = t.parse(idl);
const { fields } = document.body[0] as t.InterfaceWithFields;
const extensionConfigs = fields.map(field => field.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should throw an error when using invalid type for a path parameter', () => {
const idl = `
struct Foo {
1: bool k1 (api.position = 'path')
}
`;
try {
t.parse(idl);
} catch (err) {
const { message } = err;
const expected =
"the type of path parameter 'k1' in 'Foo' should be string or integer";
return expect(message).to.equal(expected);
}
return expect(true).to.equal(false);
});
it('should revise field comments', () => {
const idl = `
struct Foo {
// c1
1: string k1 // c2
/* c3 */
2: string k2 /* c4 */
// c5
/* c6 */
3: string k3 // c7
/* c8
c9 */
4: string k4
// c10
5: string k5; /* c11 */
}
`;
const expected = [
['c1', 'c2'],
[['c3'], ['c4']],
['c5', ['c6'], 'c7'],
[['c8', ' c9']],
['c10', ['c11']],
];
const document = t.parse(idl);
const { fields } = document.body[0] as t.InterfaceWithFields;
const comments = fields.map(field =>
field.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
it('should revise empty field comments', () => {
const idl = `
/*
*/
struct Foo {
/**/
1: string k1
/* */
2: string k2
/** */
3: string k3
}
`;
const expected = [[['']], [['']], [['']]];
const document = t.parse(idl);
const { fields } = document.body[0] as t.InterfaceWithFields;
const comments = fields.map(field =>
field.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
});
});

View File

@@ -0,0 +1,116 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/thrift';
describe('ferry-parser', () => {
describe('thrift function', () => {
it('should convert function extenstions', () => {
const idl = `
service Foo {
BizResponse Biz1(1: BizRequest req) (api.uri = '/api/biz1')
BizResponse Biz2(1: BizRequest req) (
api.uri = '/api/biz2',
api.serializer = 'json',
api.method = 'POST',
api.group="user"
)
BizResponse Biz3(1: BizRequest req) (api.get = '/api/biz3', api.serializer='form')
BizResponse Biz4(1: BizRequest req) (api.post = '/api/biz4', api.serializer='urlencoded')
BizResponse Biz5(1: BizRequest req) (api.put = '/api/biz5', api.method = 'post')
BizResponse Biz6(1: BizRequest req) (api.delete = '/api/biz6', api.serializer='wow')
BizResponse Biz7(1: BizRequest req)
}
`;
const expected = [
{ uri: '/api/biz1' },
{
uri: '/api/biz2',
serializer: 'json',
method: 'POST',
group: 'user',
},
{ method: 'GET', uri: '/api/biz3', serializer: 'form' },
{ method: 'POST', uri: '/api/biz4', serializer: 'urlencoded' },
{ method: 'PUT', uri: '/api/biz5' },
{ method: 'DELETE', uri: '/api/biz6' },
undefined,
];
const document = t.parse(idl);
const { functions } = document.body[0] as t.ServiceDefinition;
const extensionConfigs = functions.map(func => func.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert function extenstions using agw specification', () => {
const idl = `
service Foo {
BizResponse Biz1(1: BizRequest req) (agw.uri = '/api/biz1')
BizResponse Biz2(1: BizRequest req) (
agw.uri = '/api/biz2',
agw.method = 'POST',
)
}
`;
const expected = [
{ uri: '/api/biz1' },
{ uri: '/api/biz2', method: 'POST' },
];
const document = t.parse(idl, { reviseTailComment: false });
const { functions } = document.body[0] as t.ServiceDefinition;
const extensionConfigs = functions.map(func => func.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should revise function comments', () => {
const idl = `
service Foo {
// c1
BizResponse Biz1(1: BizRequest req) // c2
/* c3 */
BizResponse Biz2(1: BizRequest req) /* c4 */
// c5
/* c6 */
BizResponse Biz3(1: BizRequest req) // c7
/* c8
c9 */
BizResponse Biz4(1: BizRequest req)
// c10
BizResponse Biz5(1: BizRequest req); /* c11 */
}
`;
const expected = [
['c1', 'c2'],
[['c3'], ['c4']],
['c5', ['c6'], 'c7'],
[['c8', ' c9']],
['c10', ['c11']],
];
const document = t.parse(idl);
const { functions } = document.body[0] as t.ServiceDefinition;
const comments = functions.map(func =>
func.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
});
});

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'path';
import * as t from '../src/thrift';
describe('ferry-parser', () => {
describe('thrift index', () => {
it('should convert the file content', () => {
const idl = path.resolve(__dirname, 'idl/index.thrift');
const expected = { uri_prefix: 'https://example.com' };
const document = t.parse(idl);
const { extensionConfig } = document.body[0] as t.ServiceDefinition;
return expect(extensionConfig).to.eql(expected);
});
it('should throw an error due to invalid file path', () => {
const idl = path.resolve(__dirname, 'idl/indexx.thrift');
try {
t.parse(idl);
} catch (err) {
const { message } = err;
return expect(message).to.includes('no such file:');
}
return expect(true).to.equal(false);
});
it('should throw an syntax error', () => {
const idl = `
struct Foo {
1: string k1,,
}
`;
const expected = 'FieldType expected but found: CommaToken(source:3:';
try {
t.parse(idl);
} catch (err) {
const { message } = err;
return expect(message).to.include(expected);
}
return expect(true).to.equal(false);
});
it('should throw an syntax error in the file content', () => {
const idl = path.resolve(__dirname, 'idl/error.thrift');
const expected = '__tests__/idl/error.thrift:2:16)';
try {
t.parse(idl);
} catch (err) {
const { message } = err;
return expect(message).includes(expected);
}
return expect(true).equal(false);
});
});
});

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src/thrift';
describe('ferry-parser', () => {
describe('thrift service', () => {
it('should convert service extenstions', () => {
const idl = `
service Foo {
} (api.uri_prefix = 'https://example.com')
`;
const expected = { uri_prefix: 'https://example.com' };
const document = t.parse(idl);
const { extensionConfig } = document.body[0] as t.ServiceDefinition;
return expect(extensionConfig).to.eql(expected);
});
});
});

View File

@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"include": ["."]
}

View File

@@ -0,0 +1,174 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src';
import { filterKeys } from './common';
describe('unify-parser', () => {
describe('thrift enum', () => {
it('should convert enum', () => {
const content = `
enum Number {
ONE = 1,
TWO,
}
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { members } = document.statements[0] as t.EnumDefinition;
return expect(members.length).to.eql(2);
});
it('should resolve enum name', () => {
const content = `
enum Number {
ONE = 1,
TWO,
}
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { name } = document.statements[0] as t.EnumDefinition;
expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'Number',
namespaceValue: 'root.Number',
});
});
it('should not resolve enum name', () => {
const content = `
enum Number {
ONE = 1,
TWO,
}
`;
const document = t.parse(
'index.thrift',
{ namespaceRefer: false, cache: false },
{ 'index.thrift': content },
);
const { name } = document.statements[0] as t.EnumDefinition;
expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'Number',
namespaceValue: undefined,
});
});
it('should revise enum comments', () => {
const content = `
enum Number {
// c1
ONE = 1, // c2
/* c3 */
TWO,/* c4 */
// c5
/* c6 */
FOUR = 4; // c7
/* c8
c9 */
FIVE;
SIX,
}
`;
const expected = [
['c1', 'c2'],
[['c3'], ['c4']],
['c5', ['c6'], 'c7'],
[['c8', ' c9']],
[],
];
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { members } = document.statements[0] as t.EnumDefinition;
const comments = members.map(field =>
field.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
it('should revise enum comments without dot', () => {
const content = `
enum Number {
// c1
ONE = 1 // c2
/* c3 */
TWO/* c4 */
// c5
/* c6 */
FOUR = 4 // c7
/* c8
c9 */
FIVE
SIX
}
`;
const expected = [
['c1', 'c2'],
[['c3'], ['c4']],
['c5', ['c6'], 'c7'],
[['c8', ' c9']],
[],
];
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { members } = document.statements[0] as t.EnumDefinition;
const comments = members.map(field =>
field.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
});
describe('proto enum', () => {
it('should convert enum', () => {
const content = `
enum Number {
ONE = 1;
TWO = 2;
}
`;
const document = t.parse(
'index.proto',
{ cache: false },
{ 'index.proto': content },
);
const { members } = document.statements[0] as t.EnumDefinition;
return expect(members.length).to.eql(2);
});
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,343 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src';
import { filterKeys } from './common';
describe('unify-parser', () => {
describe('thrift function', () => {
it('should convert function extenstions', () => {
const content = `
service Foo {
BizResponse Biz1(1: BizRequest req) (api.uri = '/api/biz1')
BizResponse Biz2(1: BizRequest req) (
api.uri = '/api/biz2',
api.serializer = 'json',
api.method = 'POST',
api.group="user"
)
BizResponse Biz3(1: BizRequest req) (api.get = '/api/biz3', api.serializer='form')
BizResponse Biz4(1: BizRequest req) (api.post = '/api/biz4', api.serializer='urlencoded')
BizResponse Biz5(1: BizRequest req) (api.put = '/api/biz5', api.method = 'post', api.version='1')
BizResponse Biz6(1: BizRequest req) (api.delete = '/api/biz6', api.serializer='wow', api.custom = '{"priority":1}')
BizResponse Biz7(1: BizRequest req)
}
`;
const expected = [
{ uri: '/api/biz1' },
{
uri: '/api/biz2',
serializer: 'json',
method: 'POST',
group: 'user',
},
{ method: 'GET', uri: '/api/biz3', serializer: 'form' },
{ method: 'POST', uri: '/api/biz4', serializer: 'urlencoded' },
{ method: 'PUT', uri: '/api/biz5', version: '1' },
{ method: 'DELETE', uri: '/api/biz6', custom: '{"priority":1}' },
{},
];
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { functions } = document.statements[0] as t.ServiceDefinition;
const extensionConfigs = functions.map(func => func.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should convert function extenstions using agw specification', () => {
const content = `
service Foo {
BizResponse Biz1(1: BizRequest req) (agw.uri = '/api/biz1')
BizResponse Biz2(1: BizRequest req) (
agw.uri = '/api/biz2',
agw.method = 'POST',
)
}
`;
const expected = [
{ uri: '/api/biz1' },
{ uri: '/api/biz2', method: 'POST' },
];
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { functions } = document.statements[0] as t.ServiceDefinition;
const extensionConfigs = functions.map(func => func.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should revise function comments', () => {
const content = `
service Foo {
// c1
BizResponse Biz1(1: BizRequest req) // c2
/* c3 */
BizResponse Biz2(1: BizRequest req) /* c4 */
// c5
/* c6 */
BizResponse Biz3(1: BizRequest req) // c7
/* c8
c9 */
BizResponse Biz4(1: BizRequest req)
BizResponse Biz5(1: BizRequest req)
}
`;
const expected = [
['c1', 'c2'],
[['c3'], ['c4']],
['c5', ['c6'], 'c7'],
[['c8', ' c9']],
[],
];
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { functions } = document.statements[0] as t.ServiceDefinition;
const comments = functions.map(func =>
func.comments.map(comment => comment.value),
);
return expect(comments).to.eql(expected);
});
it('should resolve function name', () => {
const content = `
service Foo {
BizResponse Biz1(1: BizRequest req) // c2
}
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { functions } = document.statements[0] as t.ServiceDefinition;
const { name } = functions[0];
return expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'Biz1',
namespaceValue: 'root.Biz1',
});
});
it('should resolve func type', () => {
const baseContent = `
namespace go test_base
struct Response {}
`;
const funcContent = `
include "./base.thrift"
service Foo {
base.Response Biz1(1: BizRequest req) // c2
}
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{
'base.thrift': baseContent,
'index.thrift': funcContent,
},
);
const { functions } = document.statements[0] as t.ServiceDefinition;
const identifier = functions[0].returnType as t.Identifier;
return expect(filterKeys(identifier, ['value', 'namespaceValue'])).to.eql(
{
value: 'base.Response',
namespaceValue: 'test_base.Response',
},
);
});
});
describe('proto method', () => {
it('should convert method extenstions', () => {
const content = `
syntax = 'proto3';
message BizRequest {}
message BizResponse {}
service Foo {
rpc Biz1(BizRequest) returns (BizResponse) {
option (api.uri) = '/api/biz1';
}
rpc Biz2(BizRequest) returns (BizResponse) {
option (api.method) = "POST";
option (api.uri) = "/api/biz2";
option (api.serializer) = "json";
option (api.group) = 'user';
}
rpc Biz3(BizRequest) returns (BizResponse) {
option (api.get) ='/api/biz3';
option (api.serializer) ='form';
}
rpc Biz4(BizRequest) returns (BizResponse) {
option (api.post) ='/api/biz4';
option (api.serializer) ='urlencoded';
}
rpc Biz5(BizRequest) returns (BizResponse) {
option (api.put) ='/api/biz5';
}
rpc Biz6(BizRequest) returns (BizResponse) {
option (api.delete) ='/api/biz6';
}
rpc Biz7(BizRequest) returns (BizResponse);
}
`;
const expected = [
{ uri: '/api/biz1' },
{
method: 'POST',
uri: '/api/biz2',
serializer: 'json',
group: 'user',
},
{ method: 'GET', uri: '/api/biz3', serializer: 'form' },
{ method: 'POST', uri: '/api/biz4', serializer: 'urlencoded' },
{ method: 'PUT', uri: '/api/biz5' },
{ method: 'DELETE', uri: '/api/biz6' },
{},
];
const document = t.parse(
'index.proto',
{ cache: false },
{ 'index.proto': content },
);
const { functions } = document.statements[2] as t.ServiceDefinition;
const extensionConfigs = functions.map(func => func.extensionConfig);
return expect(extensionConfigs).to.eql(expected);
});
it('should resolve method name', () => {
const content = `
syntax = 'proto3';
message BizRequest {}
message BizResponse {}
service Foo {
rpc Biz1(BizRequest) returns (BizResponse) {
option (api.uri) = '/api/biz1';
}
}
`;
const document = t.parse(
'index.proto',
{ cache: false },
{ 'index.proto': content },
);
const { functions } = document.statements[2] as t.ServiceDefinition;
const { name } = functions[0];
return expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'Biz1',
namespaceValue: 'root.Foo.Biz1',
});
});
it('should resolve response type', () => {
const baseContent = `
syntax = 'proto3';
package test_base;
message Response {}
`;
const funcContent = `
import "base.proto";
syntax = 'proto3';
message BizRequest {}
service Foo {
rpc Biz1(BizRequest) returns (test_base.Response) {
option (api.uri) = '/api/biz1';
}
}
`;
const document = t.parse(
'index.proto',
{ cache: false },
{
'base.proto': baseContent,
'index.proto': funcContent,
},
);
const { functions } = document.statements[1] as t.ServiceDefinition;
const identifier = functions[0].returnType as t.Identifier;
return expect(filterKeys(identifier, ['value', 'namespaceValue'])).to.eql(
{
value: 'base.Response',
namespaceValue: 'test_base.Response',
},
);
});
it('should resolve response type within the same namespace', () => {
const baseContent = `
syntax = 'proto3';
package same;
message Response {}
message Request {}
`;
const funcContent = `
import "base.proto";
syntax = 'proto3';
package same;
service Foo {
rpc Biz1(Request) returns (same.Response) {
option (api.uri) = '/api/biz1';
}
}
`;
const document = t.parse(
'index.proto',
{ cache: false },
{
'base.proto': baseContent,
'index.proto': funcContent,
},
);
const { functions } = document.statements[0] as t.ServiceDefinition;
const returnType = functions[0].returnType as t.Identifier;
const requestType = functions[0].fields[0].fieldType as t.Identifier;
expect(filterKeys(requestType, ['value', 'namespaceValue'])).to.eql({
value: 'base.Request',
namespaceValue: 'same.Request',
});
expect(filterKeys(returnType, ['value', 'namespaceValue'])).to.eql({
value: 'base.Response',
namespaceValue: 'same.Response',
});
});
});
});

View File

@@ -0,0 +1,750 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'path';
import * as t from '../src/unify';
describe('unify-parser', () => {
describe('thrift index', () => {
it('should parse a simple file', () => {
const idl = path.resolve(__dirname, 'idl/index.thrift');
const expected = { uri_prefix: 'https://example.com' };
const document = t.parse(idl, { cache: false });
const { extensionConfig } = document.statements[0] as t.ServiceDefinition;
return expect(extensionConfig).to.eql(expected);
});
it('should parse a complicate file', () => {
const expected = {
namespace: 'unify_idx',
unifyNamespace: 'unify_idx',
include: ['./unify_dependent1.thrift', 'unify_dependent2.thrift'],
};
const document = t.parse('unify_index.thrift', {
root: path.resolve(__dirname, './idl'),
namespaceRefer: false,
cache: false,
});
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse a complicate file with relative path', () => {
const expected = {
include: ['base.thrift', 'basee.thrift'],
};
const document = t.parse('dep/common.thrift', {
root: path.resolve(__dirname, './idl'),
namespaceRefer: false,
cache: false,
});
const target = {
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files from fileContentMap', () => {
const indexContent = `
include './unify_dependent.thrift'
typedef unify_dependent.Foo TFoo
union FuncRequest {
1: unify_dependent.Foo r_key1
2: TFoo r_key2
}
service Example {
unify_dependent.FuncResponse Func(1: FuncRequest req)
} (
)
`;
const dependentContent = `
typedef Foo Foo1
struct Foo {
1: string f_key1
}
struct FuncResponse {
}
`;
const fileContentMap = {
'unify_index.thrift': indexContent,
'unify_dependent.thrift': dependentContent,
};
const expected = {
namespace: '',
unifyNamespace: 'root',
include: ['./unify_dependent.thrift'],
};
const document = t.parse(
'unify_index.thrift',
{ cache: false },
fileContentMap,
);
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files from fileContentMap with relative path', () => {
const indexContent = `
include 'unify_dependent.thrift'
typedef unify_dependent.Foo TFoo
union FuncRequest {
1: unify_dependent.Foo r_key1
2: TFoo r_key2
}
service Example {
unify_dependent.FuncResponse Func(1: FuncRequest req)
} (
)
`;
const dependentContent = `
typedef Foo Foo1
struct Foo {
1: string f_key1
}
struct FuncResponse {
}
`;
const fileContentMap = {
'relative/unify_index.thrift': indexContent,
'relative/unify_dependent.thrift': dependentContent,
};
const expected = {
namespace: '',
unifyNamespace: 'root',
include: ['unify_dependent.thrift'],
};
const document = t.parse(
'relative/unify_index.thrift',
{ cache: false },
fileContentMap,
);
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files from fileContentMap and Java namespace', () => {
const indexContent = `
namespace java com.ferry.index
union FuncRequest {
}
struct FuncResponse {}
service Example {
FuncResponse Func(1: FuncRequest req)
} (
)
`;
const fileContentMap = {
'unify_index.thrift': indexContent,
};
const expected = {
namespace: 'index',
unifyNamespace: 'index',
include: [],
};
const document = t.parse(
'unify_index.thrift',
{ cache: false },
fileContentMap,
);
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files with cache', () => {
const indexContent = `
struct Foo {}
`;
const indexxContent = `
`;
const rootContent = `
include "unify_index_cache.thrift"
`;
t.parse(
'unify_index_cache.thrift',
{
cache: true,
},
{
'unify_index_cache.thrift': indexContent,
},
);
t.parse(
'unify_root.thrift',
{ cache: false },
{
'unify_root.thrift': rootContent,
},
);
const document = t.parse(
'unify_index_cache.thrift',
{ cache: true },
{
'unify_index_cache.thrift': indexxContent,
},
);
return expect(document.statements[0].name.value).to.eql('Foo');
});
it('should parse files with ignoreTag', () => {
const indexContent = `
struct Foo {
1: string k1 (go.tag = "json:\\"key1\\"")
}
`;
const document = t.parse(
'unify_index.thrift',
{
ignoreGoTag: true,
},
{
'unify_index.thrift': indexContent,
},
);
const { fields } = document.statements[0] as t.InterfaceWithFields;
return expect(fields[0].extensionConfig).to.eql({});
});
it('should parse files with goTagDash', () => {
const indexContent = `
struct Foo {
1: string k1 (go.tag = "json:\\"-\\"")
}
`;
const document = t.parse(
'unify_index.thrift',
{},
{
'unify_index.thrift': indexContent,
},
);
const { fields } = document.statements[0] as t.InterfaceWithFields;
return expect(fields.length).to.eql(0);
});
it('should parse files with ignoreTagDash', () => {
const indexContent = `
struct Foo {
1: string k1 (go.tag = "json:\\"-\\"")
}
`;
const document = t.parse(
'unify_index.thrift',
{
ignoreGoTagDash: true,
},
{
'unify_index.thrift': indexContent,
},
);
const { fields } = document.statements[0] as t.InterfaceWithFields;
return expect(fields.length).to.eql(1);
});
it('should search files from searchPaths', () => {
const idl = path.resolve(__dirname, 'idl/unify_search.thrift');
const depDir = path.resolve(__dirname, 'idl/dep');
const document = t.parse(idl, {
cache: false,
searchPaths: [depDir],
});
const struct = document.statements[0] as t.InterfaceWithFields;
return expect(struct.type).to.eql(t.SyntaxType.StructDefinition);
});
it('should search files from relative searchPaths', () => {
const document = t.parse('unify_search.thrift', {
root: path.resolve(__dirname, 'idl'),
cache: false,
searchPaths: ['./dep'],
});
const struct = document.statements[0] as t.InterfaceWithFields;
return expect(struct.type).to.eql(t.SyntaxType.StructDefinition);
});
it('should parse files with a syntax error', () => {
const indexContent = `
struct
`;
try {
t.parse(
'error.thrift',
{ cache: false },
{
'error.thrift': indexContent,
},
);
} catch (err) {
return expect(err.message).to.eql(
'Struct-like must have an identifier',
);
}
return expect(0).to.eql(1);
});
it('should parse files with a dependent file error within fileContentMap', () => {
const indexContent = `
include "./dependent.thrift"
`;
try {
t.parse(
'error.thrift',
{ cache: false },
{
'error.thrift': indexContent,
},
);
} catch (err) {
return expect(err.message).to.eql(
'file dependent.thrift does not exist in fileContentMap',
);
}
return expect(0).to.eql(1);
});
it('should parse files with a entry file error within fileContentMap', () => {
try {
t.parse('specify_error.thrift', { cache: false }, {});
} catch (err) {
return expect(err.message).to.equal(
'file "specify_error.thrift" does not exist in fileContentMap',
);
}
return expect(0).to.eql(1);
});
it('should parse files with a entry file error', () => {
const idl = path.resolve(__dirname, 'idl/special_error.thrift');
try {
t.parse(idl, { cache: false });
} catch (err) {
return expect(err.message).to.contain('no such file:');
}
return expect(0).to.eql(1);
});
it('should parse files with a dependent file error', () => {
const idl = path.resolve(__dirname, 'idl/unify_error.thrift');
try {
t.parse(idl, { cache: false });
} catch (err) {
return expect(err.message).to.contain('does not exist');
}
return expect(0).to.eql(1);
});
it('should parse files with a namespace error', () => {
const indexContent = `
namespace java1 com.index
`;
try {
t.parse(
'error.thrift',
{ cache: false },
{
'error.thrift': indexContent,
},
);
} catch (err) {
return expect(err.message).to.eql('a js namespace should be specifed');
}
return expect(0).to.eql(1);
});
});
describe('proto index', () => {
it('should a simple file', () => {
const idl = path.resolve(__dirname, 'idl/index.proto');
const expected = { uri_prefix: '//example.com' };
const document = t.parse(idl, { cache: false });
const { extensionConfig } = document.statements[0] as t.ServiceDefinition;
return expect(extensionConfig).to.eql(expected);
});
it('should parse a complicate file', () => {
const expected = {
namespace: 'unify_idx',
unifyNamespace: 'unify_idx',
include: ['./unify_dependent1.proto', 'unify_dependent2.proto'],
};
const document = t.parse('unify_index.proto', {
root: path.resolve(__dirname, './idl'),
namespaceRefer: false,
cache: false,
});
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse a complicate file with relative path', () => {
const expected = {
include: ['base.proto', 'basee.proto'],
};
const document = t.parse('dep/common.proto', {
root: path.resolve(__dirname, './idl'),
namespaceRefer: false,
cache: false,
});
const target = {
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files from fileContentMap', () => {
const indexContent = `
syntax = "proto3";
import "./unify_dependent.proto";
message Request {
repeated string key1 = 1[(api.key) = 'f'];
unify_dep3.Foo key2 = 2;
}
service Example {
option (api.uri_prefix) = "//example.com";
rpc Biz1(Request) returns (unify_dep3.Response) {
option (api.uri) = '/api/biz1';
}
}
`;
const dependentContent = `
syntax = "proto3";
package unify_dep3;
message Foo {
string f_key1 = 1;
}
message Response {}
`;
const fileContentMap = {
'unify_index.proto': indexContent,
'unify_dependent.proto': dependentContent,
};
const expected = {
namespace: '',
unifyNamespace: 'root',
include: ['./unify_dependent.proto'],
};
const document = t.parse(
'unify_index.proto',
{ cache: false },
fileContentMap,
);
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files from fileContentMap with relative path', () => {
const indexContent = `
syntax = "proto3";
import "unify_dependent.proto";
message Request {
repeated string key1 = 1[(api.key) = 'f'];
unify_dep3.Foo key2 = 2;
}
service Example {
option (api.uri_prefix) = "//example.com";
rpc Biz1(Request) returns (unify_dep3.Response) {
option (api.uri) = '/api/biz1';
}
}
`;
const dependentContent = `
syntax = "proto3";
package unify_dep3;
message Foo {
string f_key1 = 1;
}
message Response {}
`;
const fileContentMap = {
'relative/unify_index.proto': indexContent,
'relative/unify_dependent.proto': dependentContent,
};
const expected = {
namespace: '',
unifyNamespace: 'root',
include: ['unify_dependent.proto'],
};
const document = t.parse(
'relative/unify_index.proto',
{ cache: false },
fileContentMap,
);
const target = {
namespace: document.namespace,
unifyNamespace: document.unifyNamespace,
include: document.includes,
};
return expect(target).to.eql(expected);
});
it('should parse files with cache', () => {
const indexContent = `
syntax = "proto3";
message Foo {}
`;
const indexxContent = `
syntax = "proto3";
`;
const rootContent = `
import "unify_index_cache.proto";
syntax = "proto3";
`;
t.parse(
'unify_index_cache.proto',
{
cache: true,
},
{
'unify_index_cache.proto': indexContent,
},
);
t.parse(
'unify_root.proto',
{ cache: false },
{
'unify_root.proto': rootContent,
},
);
const document = t.parse(
'unify_index_cache.proto',
{ cache: true },
{
'unify_index_cache.proto': indexxContent,
},
);
return expect(document.statements[0].name.value).to.eql('Foo');
});
it('should parse files ignoring import', () => {
const indexContent = `
syntax = "proto3";
import "google/protobuf/api.proto";
`;
t.parse(
'ignore.proto',
{ cache: false },
{
'ignore.proto': indexContent,
},
);
});
it('should search files from searchPaths', () => {
const idl = path.resolve(__dirname, 'idl/unify_search.proto');
const depDir = path.resolve(__dirname, 'idl/dep');
const document = t.parse(idl, {
cache: false,
searchPaths: [depDir],
});
const struct = document.statements[0] as t.InterfaceWithFields;
return expect(struct.type).to.eql(t.SyntaxType.StructDefinition);
});
it('should search files from relative searchPaths', () => {
const document = t.parse('unify_search.proto', {
root: path.resolve(__dirname, 'idl'),
cache: false,
searchPaths: ['./dep'],
});
const struct = document.statements[0] as t.InterfaceWithFields;
return expect(struct.type).to.eql(t.SyntaxType.StructDefinition);
});
it('should parse files with a syntax error', () => {
const indexContent = `
syntax = "proto3";
message Foo {
`;
try {
t.parse(
'error.proto',
{ cache: false },
{
'error.proto': indexContent,
},
);
} catch (err) {
return expect(err.message).to.eql("illegal token 'null', '=' expected");
}
return expect(0).to.eql(1);
});
it('should parse files with a entry file error within fileContentMap', () => {
try {
t.parse('specify_error.proto', { cache: false }, {});
} catch (err) {
return expect(err.message).to.equal(
'file "specify_error.proto" does not exist in fileContentMap',
);
}
return expect(0).to.eql(1);
});
it('should parse files with a file error', () => {
const indexContent = `
syntax = "proto3";
import "./dependent.proto";
`;
try {
t.parse(
'error.proto',
{ cache: false },
{
'error.proto': indexContent,
},
);
} catch (err) {
return expect(err.message).to.eql(
'file dependent.proto does not exist in fileContentMap',
);
}
return expect(0).to.eql(1);
});
it('should parse files with a real file error', () => {
try {
t.parse('real_error.proto', { cache: false });
} catch (err) {
return expect(err.message).to.contain('no such file');
}
return expect(0).to.eql(1);
});
});
describe('all index', () => {
it('should parse files with a file error', () => {
try {
t.parse('error', { cache: false });
} catch (err) {
return expect(err.message).to.eql('invalid filePath: "error"');
}
return expect(0).to.eql(1);
});
});
});

View File

@@ -0,0 +1,140 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src';
import { filterKeys } from './common';
describe('unify-parser', () => {
describe('thrift const', () => {
it('should parse string const', () => {
const content = `
const string a = '1';
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { fieldType, initializer } = document
.statements[0] as t.ConstDefinition;
expect((fieldType as t.BaseType).type).to.eql(t.SyntaxType.StringKeyword);
expect((initializer as t.StringLiteral).value).to.equal('1');
});
it('should parse list const', () => {
const content = `
const list<i32> b = [1]
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { fieldType, initializer } = document
.statements[0] as t.ConstDefinition;
expect(((fieldType as t.ListType).valueType as t.BaseType).type).to.eql(
t.SyntaxType.I32Keyword,
);
expect(
((initializer as t.ConstList).elements[0] as t.IntConstant).value.value,
).to.equal('1');
});
it('should parse map const', () => {
const content = `
const map<string, i32> c = {'m': 1}
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': content },
);
const { fieldType, initializer } = document
.statements[0] as t.ConstDefinition;
expect(((fieldType as t.MapType).valueType as t.BaseType).type).to.eql(
t.SyntaxType.I32Keyword,
);
expect(
((initializer as t.ConstMap).properties[0].initializer as t.IntConstant)
.value.value,
).to.equal('1');
});
it('should not resolve const name', () => {
const content = `
const string a = '1';
`;
const document = t.parse(
'index.thrift',
{ cache: false, namespaceRefer: false },
{
'index.thrift': content,
},
);
const { name } = document.statements[0] as t.ConstDefinition;
return expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'a',
namespaceValue: undefined,
});
});
});
describe('thrift typedef', () => {
it('should resolve typedef', () => {
const baseContent = `
namespace go unify_base
`;
const indexContent = `
include 'base.thrift'
typedef base.Foo MyFoo
typedef Bar MyBar
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{
'index.thrift': indexContent,
'base.thrift': baseContent,
},
);
const { name: name0, definitionType: definitionType0 } = document
.statements[0] as t.TypedefDefinition;
const { definitionType: definitionType1 } = document
.statements[1] as t.TypedefDefinition;
expect(filterKeys(name0, ['value', 'namespaceValue'])).to.eql({
value: 'MyFoo',
namespaceValue: 'root.MyFoo',
});
expect(filterKeys(definitionType0, ['value', 'namespaceValue'])).to.eql({
value: 'base.Foo',
namespaceValue: 'unify_base.Foo',
});
expect(filterKeys(definitionType1, ['value', 'namespaceValue'])).to.eql({
value: 'Bar',
namespaceValue: 'root.Bar',
});
});
});
});

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as t from '../src';
import { filterKeys } from './common';
describe('unify-parser', () => {
describe('thrift service', () => {
it('should convert service extenstions', () => {
const fileContent = `
service Foo {
} (api.uri_prefix = 'https://example.com')
`;
const document = t.parse(
'index.thrift',
{ cache: false },
{ 'index.thrift': fileContent },
);
const { extensionConfig, name } = document
.statements[0] as t.ServiceDefinition;
expect(extensionConfig).to.eql({ uri_prefix: 'https://example.com' });
expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'Foo',
namespaceValue: 'root.Foo',
});
});
});
describe('proto service', () => {
it('should convert service extenstions', () => {
const fileContent = `
syntax = "proto3";
service Foo {
option (api.uri_prefix) = "//example.com";
}
`;
const document = t.parse(
'index.proto',
{ cache: false },
{ 'index.proto': fileContent },
);
const { extensionConfig, name } = document
.statements[0] as t.ServiceDefinition;
expect(extensionConfig).to.eql({ uri_prefix: '//example.com' });
expect(filterKeys(name, ['value', 'namespaceValue'])).to.eql({
value: 'Foo',
namespaceValue: 'root.Foo',
});
});
});
});