Skip to content

Commit

Permalink
feat: 解析schema.json 生成直接使用的data对象
Browse files Browse the repository at this point in the history
  • Loading branch information
limengke123 committed Jun 28, 2019
1 parent 8090cd6 commit 3124795
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion example/schema.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const mock = function (option: option): void {
.then(generateData)
.catch((e: Error) => {
console.log(e.message)
console.log(e.stack)
process.exit(-1)
})
}
14 changes: 9 additions & 5 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Resolver {
keys.forEach(key => {
this.schemaJson[key] = this.source[key].reduce((accu: {[propName: string]: Ischema}, current: Iline) => {
const {name, comment, optional, type} = current
const {transformType, builtin, defaultValue, length, generics} = this.getType(type || '')
const {transformType, builtin, defaultValue, length, generics} = Resolver.getType(type || '')
accu[name] = {
type: transformType,
comment,
Expand All @@ -63,14 +63,14 @@ export class Resolver {
})
}

getType (type: string): {transformType: string, builtin: boolean, defaultValue?: any, length?: number, generics?: string} {
static getType (type: string): {transformType: string, builtin: boolean, defaultValue?: any, length?: number, generics?: string} {
let result: {transformType:string, builtin: boolean, defaultValue?: any, length?: number, generics?: string} = {transformType: type, builtin: false}
if (arrReg.test(type)) {
return {
builtin: false,
transformType: 'array',
length: 1,
generics: this.resolveArray(type)
length: 2,
generics: Resolver.resolveArray(type)
}
}
switch (type) {
Expand All @@ -83,6 +83,10 @@ export class Resolver {
result.builtin = true
result.defaultValue = 123
break
case 'number':
result.builtin = true
result.defaultValue = 123
break
case 'boolean':
result.builtin = true
result.defaultValue = true
Expand All @@ -95,7 +99,7 @@ export class Resolver {
return result
}

resolveArray (str: string): string {
static resolveArray (str: string): string {
let result: string = ''
str.replace(arrReg, (_, generics) => {
result = generics
Expand Down
42 changes: 39 additions & 3 deletions src/tasks/generateData.ts
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)
}

2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const writeFile = (path: string, data: string) => {
}

export const readJsonFile = (path: string) => {
return new Promise<object>((resolve, reject) => {
return new Promise<any>((resolve, reject) => {
fs.readFile(path, (e, buf) => {
if (e) {
return reject(e)
Expand Down

0 comments on commit 3124795

Please sign in to comment.