Skip to content

Commit 3c10ec9

Browse files
committed
fix model extend bug
1 parent a58297f commit 3c10ec9

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules/
33
.DS_Store
44
npm-*.log
5+
test/thrift

lib/generate-data.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var UNMATCHED = {};
1717
var mockData = {};
1818
var generateBoundary = false;
1919
function generate(type, ast, opts) {
20-
//console.log(type, JSON.stringify(ast, void 0 ,4), 'original-ast');
2120
var ret = type.split(".");
2221
if (ret.length > 1) {
2322
ast = ast.include[ret[0]];
@@ -57,6 +56,7 @@ function constructorData(structItems, ast) {
5756
} else {
5857
items = innerAst.struct[item.type];
5958
}
59+
extendModels(innerAst, ast);
6060
ret[item.name] = constructorData(items, innerAst);
6161
} else {
6262
ret[item.name] = result;
@@ -71,10 +71,10 @@ function constructorData(structItems, ast) {
7171
if (generateResult === UNMATCHED) {
7272
var data = getStruct(valueType, ast);
7373
var _items = data.struct;
74-
//console.log("---list", valueType, "||", ast, "list---");
75-
ast = data.ast;
74+
var _innerAst = data.ast;
75+
extendModels(_innerAst, ast);
7676
for (var i = 0, len = Math.round(Math.random() * 20); i < len; i++) {
77-
_result2.push(constructorData(_items, ast));
77+
_result2.push(constructorData(_items, _innerAst));
7878
}
7979
ret[item.name] = _result2;
8080
} else {
@@ -107,6 +107,17 @@ function getStruct(type, ast) {
107107
}
108108
}
109109

110+
function extendModels(innerAst, outerAst) {
111+
if (innerAst && outerAst && outerAst.include) {
112+
innerAst.include = innerAst.include || {};
113+
for (var model in outerAst.include) {
114+
if (!innerAst.include[model]) {
115+
innerAst.include[model] = outerAst.include[model];
116+
}
117+
}
118+
}
119+
}
120+
110121
function findModel(model, ast) {
111122
if (ast.include) {
112123
if (ast && ast.include && ast.include[model]) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "thrift-mocker",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"description": "Easy to mock data based on thrift, no hard code data anymore!",
55
"author": {
66
"name": "zangzhan"

src/generate-data.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const UNMATCHED = {};
55
var mockData = {};
66
var generateBoundary = false;
77
export default function generate(type, ast, opts){
8-
//console.log(type, JSON.stringify(ast, void 0 ,4), 'original-ast');
98
var ret = type.split(".");
109
if(ret.length > 1){
1110
ast = ast.include[ret[0]];
@@ -45,6 +44,7 @@ function constructorData(structItems, ast){
4544
}else{
4645
items = innerAst.struct[item.type];
4746
}
47+
extendModels(innerAst, ast);
4848
ret[item.name] = constructorData(items, innerAst);
4949
}else{
5050
ret[item.name] = result;
@@ -59,10 +59,10 @@ function constructorData(structItems, ast){
5959
if(generateResult === UNMATCHED){
6060
const data = getStruct(valueType, ast);
6161
const items = data.struct;
62-
//console.log("---list", valueType, "||", ast, "list---");
63-
ast = data.ast;
62+
let innerAst = data.ast;
63+
extendModels(innerAst, ast);
6464
for(let i=0, len=Math.round(Math.random()*20); i<len; i++){
65-
result.push(constructorData(items, ast));
65+
result.push(constructorData(items, innerAst));
6666
}
6767
ret[item.name] = result;
6868
}else{
@@ -95,6 +95,17 @@ function getStruct(type, ast){
9595
}
9696
}
9797

98+
function extendModels(innerAst, outerAst){
99+
if(innerAst && outerAst && outerAst.include) {
100+
innerAst.include = innerAst.include || {};
101+
for(var model in outerAst.include) {
102+
if(!innerAst.include[model]){
103+
innerAst.include[model] = outerAst.include[model];
104+
}
105+
}
106+
}
107+
}
108+
98109
function findModel(model, ast){
99110
if(ast.include){
100111
if(ast && ast.include &&ast.include[model]){

0 commit comments

Comments
 (0)