js json obj 实体生成 json的默认值和字段注释生成
文档:js json obj 实体生成 json的默认值和?..
链接:http://note.youdao.com/noteshare?id=4e9a9ab56127af81bf548d2bde27338c&sub=3D661303F525437B89B5F267EDCE3C27
添加链接描述
let obj = {
"id": "0a92e7d9",
"conversationId": "81f8f396",
"personId": "ea38108e",
"text": "b6f5f494",
"audioFileToken": "69f3e515",
"translationResult": "2ba14003",
"createTime": {
"date": {
"year": -829233272,
"month": 433736398,
"day": -1093198240
},
"time": {
"hour": "0",
"minute": "0",
"second": "0",
"nano": -2067186232
}
},
"deleted": false,
"insertTime": 648848170,
"updateTime": 2049751701,
"deleteTime": -1846444143
}
obj = {
"creator": "4fb67f4d",
"topic": "b06b8336",
"id": "95e0d834",
"deleted": false,
"insertTime": 950660558,
"updateTime": 2075673529,
"deleteTime": 1124049122
}
function nowMs() {
return Date.now()
}
function strWarp(key) {
return `"${key}"`
}
function getShowVal(key) {
let mapToShowVal = {
insertTime: nowMs(),
deleteTime: nowMs(),
updateTime: nowMs(),
createTime: "" + new Date(),
topic: "一个主题",
creator: "school001",
"deleted": false,
text: "hello",
translationResult: "你好",
}
let showVal = mapToShowVal[key]
if (key == 'deleted') {
return false
}
if (showVal) {
if (typeof showVal === 'string' || showVal instanceof String) {
return strWarp(showVal)
}
return showVal
}
return `"${key}"`
}
function getShowCmt(key) {
let map = {
insertTime: "插入时间,插入数据的时候默认不传递,后端自动设置.修改数据的时候一般也不传递,因为插入时间是固定的",
deleteTime: "不用传递,后端处理",
updateTime: "不用传递,后端处理",
createTime: "不用传递,后端处理",
"id":"插入的时候前端传递null,因为数据库会自动生成,update的时候要根据id来指定",
topic: "一个主题",
creator: "createorId--后端根据token获取-save时候前端不需要传递",
"deleted": "如果是插入数据,默认不用传递,后端会设置为false,认为他新建的时候是没有被删除的",
text: "原文",
translationResult: "翻译结果字符串",
}
let showVal = map[key]
if (showVal) {
if (typeof showVal === 'string' || showVal instanceof String) {
return strWarp(showVal)
}
return showVal
}
return `"${key}"`
}
function desp(key, value) {
let showVal = getShowVal(key)
row = `${key}:${showVal}, // ${ getShowCmt(key)}`
console.log(row);
}
for (let key in obj) {
desp(key, obj[key])
}