Skip to content

Commit ec1679f

Browse files
committed
✨ Add feature that generate Struct name.
1 parent 3117cb7 commit ec1679f

File tree

3 files changed

+68
-27
lines changed

3 files changed

+68
-27
lines changed

README.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,45 @@
22

33
# Install
44
```sh
5-
npm install -g api-to-go
5+
npm install -g @nkmr-jp/api-to-go
66
```
77

8+
# Usage
89
```sh
9-
api-to-go https://api.github.com/users/github
10+
api-to-go https://api.github.com/users
11+
```
12+
13+
```sh
14+
tree .
15+
# > .
16+
# > └── api.github.com
17+
# > └── users.go
18+
```
19+
20+
```go
21+
// ./api.github.com/users.go
22+
package apigithubcom
23+
24+
type Users []struct {
25+
Login string `json:"login"`
26+
ID int `json:"id"`
27+
NodeID string `json:"node_id"`
28+
AvatarURL string `json:"avatar_url"`
29+
GravatarID string `json:"gravatar_id"`
30+
URL string `json:"url"`
31+
HTMLURL string `json:"html_url"`
32+
FollowersURL string `json:"followers_url"`
33+
FollowingURL string `json:"following_url"`
34+
GistsURL string `json:"gists_url"`
35+
StarredURL string `json:"starred_url"`
36+
SubscriptionsURL string `json:"subscriptions_url"`
37+
OrganizationsURL string `json:"organizations_url"`
38+
ReposURL string `json:"repos_url"`
39+
EventsURL string `json:"events_url"`
40+
ReceivedEventsURL string `json:"received_events_url"`
41+
Type string `json:"type"`
42+
SiteAdmin bool `json:"site_admin"`
43+
}
1044
```
1145

1246
# Development
@@ -15,9 +49,6 @@ api-to-go https://api.github.com/users/github
1549
# Make the command available
1650
npm link
1751

18-
# Commands
19-
api-to-go
20-
21-
# Publish
22-
npm publish --access=public
52+
# Run Command
53+
api-to-go https://api.github.com/users/github
2354
```

bin/api-to-go.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,61 @@ const fetch = require('node-fetch');
33
const fs = require('fs');
44
const jsonToGo = require('../vendor/json-to-go/json-to-go.js');
55

6-
function run() {
7-
if (process.argv.length !== 3) {
8-
console.log("parameter is wrong.")
9-
return
10-
}
11-
12-
const apiUrl = process.argv[2].replace(/\/$/, '')
6+
function run(url) {
7+
const apiUrl = url.replace(/\/$/, '')
138
fetch(apiUrl)
149
.then(res => res.json())
1510
.then(json => {
16-
let res = jsonToGo(JSON.stringify(json), 'AutoGenerated');
17-
const url = new URL(apiUrl);
18-
const path = _parsePath(`${url.hostname}${url.pathname}`)
11+
const path = _parseUrl(apiUrl)
12+
const res = jsonToGo(JSON.stringify(json), path.struct);
1913
const content = _buildContent(res.go, path.pkg)
2014
fs.mkdirSync(path.dir, {recursive: true})
2115
fs.writeFile(path.filePath, content, (err) => {
2216
if (err) throw err;
23-
console.log(`generated: ${path.filePath}`);
17+
console.log(json)
18+
console.log()
19+
console.log(`generated: ${path.filePath}`)
2420
});
2521
}
2622
);
2723
}
2824

29-
function _parsePath(path) {
25+
function _parseUrl(apiUrl) {
26+
const url = new URL(apiUrl);
27+
const path = `${url.hostname}${url.pathname}`
3028
const pathArr = path.split("/")
31-
const file = pathArr[pathArr.length - 1]+".go"
32-
const pkg = pathArr[pathArr.length - 2]
29+
const pkg = pathArr[pathArr.length - 2].replace(/\./g, '')
30+
const last = pathArr[pathArr.length - 1] || "index"
31+
const file = last + ".go"
32+
const struct = _capitalize(last)
3333
pathArr.pop()
3434
const dir = pathArr.join("/")
3535
return {
36+
struct,
3637
file,
3738
pkg,
3839
dir,
39-
filePath:`${dir}/${file}`
40+
filePath: `${dir}/${file}`
4041
}
4142
}
4243

4344
function _buildContent(struct, packageName) {
4445
let content = `package ${packageName}\n\n`
45-
if (struct.indexOf('time.')) {
46+
if (struct.indexOf('time.') !== -1) {
4647
content = `${content}import "time"\n\n`
4748
}
4849
content = `${content}${struct}`
4950
return content
5051
}
5152

52-
run()
53+
function _capitalize(str) {
54+
const lower = str.toLowerCase();
55+
return str.charAt(0).toUpperCase() + lower.slice(1);
56+
}
57+
58+
if (process.argv.length !== 3) {
59+
console.log("parameter is wrong.")
60+
return
61+
}
62+
63+
run(process.argv[2])

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "api-to-go",
2+
"name": "@nkmr-jp/api-to-go",
33
"version": "0.0.1",
44
"repository": "[email protected]:nkmr-jp/api-to-go.git",
55
"author": "nkmr-jp <[email protected]>",
@@ -10,8 +10,7 @@
1010
"homepage": "https://github.com/nkmr-jp/api-to-go#readme",
1111
"scripts": {
1212
"patch-release": "npm version patch && npm publish --access=public && git push --follow-tags",
13-
"clone": "mkdir -p vendor && cd vendor && git clone https://github.com/mholt/json-to-go.git",
14-
"build": "..."
13+
"fetch": "mkdir -p vendor && cd vendor && git clone https://github.com/mholt/json-to-go.git"
1514
},
1615
"description": "Convert Rest API's JSON payload to Golang struct.",
1716
"bin": {

0 commit comments

Comments
 (0)