-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8090cd6
commit 3124795
Showing
5 changed files
with
51 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,48 @@ | ||
import { readJsonFile } from '../util' | ||
import { Ischema, Resolver } from '../resolver' | ||
|
||
const ENTRY = 'Result' | ||
const DATA = 'data' | ||
|
||
export function generateData (schemaPath: string) { | ||
return Promise.resolve() | ||
.then(() => readJsonFile(schemaPath)) | ||
.then((schema: object) => { | ||
console.log(schema) | ||
.then((schema): any => { | ||
if (!schema[ENTRY]) { | ||
return Promise.reject('不存在 Result 字段,无法解析') | ||
} | ||
const result = parse(schema[ENTRY] as {[key: string]: Ischema}, schema) | ||
// @ts-ignore | ||
console.log(result.data.feedbackDos) | ||
}) | ||
} | ||
|
||
|
||
function parse<T, K extends keyof T>(root: T, source: any): Map<string, Ischema> { | ||
const keys = Object.keys(root) as K[] | ||
let result = {} as any | ||
return keys.reduce((accu, currentKey) => { | ||
const schema = root[currentKey] as Ischema | ||
const { type, generics, length, comment, data, optional } = schema | ||
if (data) { | ||
// 存在data字段了, 不需要搞别的值了 | ||
accu[currentKey] = data | ||
} else { | ||
if (type === 'array') { | ||
if (!generics) { | ||
throw new Error('数组类型缺少泛型 generics') | ||
} | ||
if (source[generics]) { | ||
// 其他类型的值 | ||
accu[currentKey] = new Array(length).fill(0).map(() => parse(source[generics], source)) | ||
} else { | ||
// 基本类型的值 | ||
accu[currentKey] = new Array(length).fill(0).map(() => Resolver.getType(generics).defaultValue) | ||
} | ||
} else { | ||
accu[currentKey] = parse(source[type || ''], source) | ||
} | ||
} | ||
return accu | ||
}, result) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters