Skip to content

Commit 866e567

Browse files
committed
✨ Add feature that to add comment to struct
1 parent 048b63a commit 866e567

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

bin/api-to-go.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,32 @@ function run(url) {
99
fetch(apiUrl)
1010
.then(res => res.json())
1111
.then(json => {
12-
const path = buildPath(apiUrl)
13-
const res = jsonToGo(JSON.stringify(json), path.struct);
14-
const content = _buildContent(res.go, path.pkg)
15-
fs.mkdirSync(path.dir, {recursive: true})
16-
fs.writeFile(path.jsonFilePath, JSON.stringify(json,null,"\t"), (err) => {
17-
if (err) throw err;
18-
console.log(`saved: ${path.jsonFilePath}`)
19-
});
20-
fs.writeFile(path.goFilePath, content, (err) => {
21-
if (err) throw err;
22-
console.log(`generated: ${path.goFilePath}`)
23-
});
12+
const path = buildPath(apiUrl)
13+
const res = jsonToGo(JSON.stringify(json), path.struct);
14+
const content = _buildContent(res.go, path, apiUrl)
15+
fs.mkdirSync(path.dir, {recursive: true})
16+
fs.writeFile(path.jsonFilePath, JSON.stringify(json, null, "\t"), (err) => {
17+
if (err) throw err;
18+
console.log(`saved: ${path.jsonFilePath}`)
19+
});
20+
fs.writeFile(path.goFilePath, content, (err) => {
21+
if (err) throw err;
22+
console.log(`generated: ${path.goFilePath}`)
23+
});
2424
}
2525
);
2626
}
2727

28-
function _buildContent(struct, packageName) {
29-
let content = `package ${packageName}\n\n`
28+
function _buildContent(struct, path, apiUrl) {
29+
let content = `package ${path.pkg}\n\n`
3030
if (struct.indexOf('time.') !== -1) {
3131
content = `${content}import "time"\n\n`
3232
}
33-
content = `${content}${struct}`
33+
let comment = `// ${path.struct} is struct of API's payload.\n`
34+
comment += `// url: ${apiUrl}\n`
35+
if (path.path.pathFormat) comment += `// format: ${path.path.pathFormat}\n`
36+
comment += `// example: ${path.jsonFilePath}\n`
37+
content = `${content}${comment}${struct}`
3438
return content
3539
}
3640

src/buildPath.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function _buildPath(url, configFile) {
2828
for (let i = 0; i < hostCfg.length; i++) {
2929
ret = _replacePath(url.pathname, hostCfg[i])
3030
if (ret) {
31+
ret["Url"] = `${url.hostname}${ret.replacedPath}`
3132
ret["replacedUrl"] = `${url.hostname}${ret.replacedPath}`
3233
return ret
3334
}

0 commit comments

Comments
 (0)