Skip to content

Commit

Permalink
fix(grpc-mock): repeat judge only in repeated
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed Nov 16, 2024
1 parent 10b7ec4 commit d0e3c4b
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/grpc-mock/src/gen/gen-response-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ type IOpts = {
defaultMockData?: Partial<IDefaultMockData>;
};

let repeatList: string[] = [];

export default function genResponseData(opts: IOpts): string {
const { typeMessage, root, longsTypeToString, defaultMockData = {} } = opts;

const commentReg = /(@optional)|(@option)|[\r\n]/g;
repeatList = [];
let repeatList: string[] = [];

const genEnumObj = (type: Enum) => {
const { valuesById, comments } = type;
Expand Down Expand Up @@ -101,12 +99,14 @@ export default function genResponseData(opts: IOpts): string {
);
}
} else {
repeatList.push(fieldType);
if (repeated) {
repeatList.push(fieldType);
}
const repeatCount = repeatList.filter((value) => {
return value === fieldType;
}).length;
// 防止死循环
if (repeatCount < 2) {
if (!repeated || (repeatCount < 2 && repeated)) {
const typePath = `${fieldType}`;
const lookupTypeOrEnumMessage = root.lookupTypeOrEnum(
typePath,
Expand Down Expand Up @@ -136,11 +136,8 @@ export default function genResponseData(opts: IOpts): string {
);
}
} else {
if (repeated) {
jsonArr.push(`${field}: [],`);
} else {
jsonArr.push(`${field}: {},`);
}
// only repeated
jsonArr.push(`${field}: [],`);
}
}
});
Expand Down

0 comments on commit d0e3c4b

Please sign in to comment.