-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_model.js
48 lines (45 loc) · 1.21 KB
/
parse_model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var util = require("./util")
var types = {
int64 : "int64",
time : "time.Time",
string : "string"
}
// Converts schema to Tags
function GenerateTags(obj) {
let tags = ""
Object.keys(obj).forEach(k => {
let structKey = util.snakeToCamel(k)
structKey = structKey[0].toUpperCase() + structKey.slice(1, structKey.length)
tags += `${structKey} ${obj[k]} \`json:"${k}"\` \n`
})
return tags
}
// Add additional definitions to schema
function PreProcessSchema(obj) {
obj["CreateDate"] = types.time
obj["UpdateDate"] = types.time
return obj
}
// Converts the schema to struct without model name
function ProcessSchema(obj) {
let schema = PreProcessSchema(obj)
let tags = GenerateTags(schema)
return tags
}
// Converts the schema to struct with model name
function ParseModel(model, schema) {
let str = `type ${model} struct { \n`
str += ProcessSchema(schema)
str += `} \n`
return str
}
// Parse model from file
function ParseModelFromFile(model, file) {
let jsonObj = {}
jsonObj["id"] = types.int64
jsonObj = {...jsonObj, ...util.readJsonFromFile(file)}
return ParseModel(model, jsonObj)
}
module.exports = {
ParseModelFromFile
}