2016/11/01

いい記事に感化されて僕も何か書きたくなった。

GolangでAPI Clientを実装する | SOTA

GolangでAPI Clientを実装する 特定のAPIを利用するコマンドラインツールやサービスを書く場合はClientパッケージ(SDKと呼ばれることも多いが本記事ではClientと呼ぶ)を使うこ...

http://deeeet.com/writing/2016/11/01/go-api-client/

この先、JSON REST API のエンドポイントに対して Golang の struct を用意していく訳だけど、ここが一番かったるい作業で一番手を抜きたい所だと思います。そこで便利なのが JSON-to-Go です。

JSON-to-Go: Convert JSON to Go instantly

JSON-to-Go Convert JSON to Go struct This tool instantly converts JSON into a Go type definition. Pa...

https://mholt.github.io/json-to-go/ JSON-to-Go

このサイトの左側ペインに JSON を与えると、Golang の struct が一気に生成できます。例えば GitHub API でユーザ情報を取得する API の JSON は以下の様になります。

{
  "login": "octocat",
  "id": 1,
  "avatar_url": "https://github.com/images/error/octocat_happy.gif",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  "followers_url": "https://api.github.com/users/octocat/followers",
  "following_url": "https://api.github.com/users/octocat/following{/other_user}",
  "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
  "organizations_url": "https://api.github.com/users/octocat/orgs",
  "repos_url": "https://api.github.com/users/octocat/repos",
  "events_url": "https://api.github.com/users/octocat/events{/privacy}",
  "received_events_url": "https://api.github.com/users/octocat/received_events",
  "type": "User",
  "site_admin": false,
  "name": "monalisa octocat",
  "company": "GitHub",
  "blog": "https://github.com/blog",
  "location": "San Francisco",
  "email": "octocat@github.com",
  "hireable": false,
  "bio": "There once was...",
  "public_repos": 2,
  "public_gists": 1,
  "followers": 20,
  "following": 0,
  "created_at": "2008-01-14T04:33:35Z",
  "updated_at": "2008-01-14T04:33:35Z"
}

これを JSON-to-Go に食わせると

type AutoGenerated struct {
    Login string `json:"login"`
    ID int `json:"id"`
    AvatarURL string `json:"avatar_url"`
    GravatarID string `json:"gravatar_id"`
    URL string `json:"url"`
    HTMLURL string `json:"html_url"`
    FollowersURL string `json:"followers_url"`
    FollowingURL string `json:"following_url"`
    GistsURL string `json:"gists_url"`
    StarredURL string `json:"starred_url"`
    SubscriptionsURL string `json:"subscriptions_url"`
    OrganizationsURL string `json:"organizations_url"`
    ReposURL string `json:"repos_url"`
    EventsURL string `json:"events_url"`
    ReceivedEventsURL string `json:"received_events_url"`
    Type string `json:"type"`
    SiteAdmin bool `json:"site_admin"`
    Name string `json:"name"`
    Company string `json:"company"`
    Blog string `json:"blog"`
    Location string `json:"location"`
    Email string `json:"email"`
    Hireable bool `json:"hireable"`
    Bio string `json:"bio"`
    PublicRepos int `json:"public_repos"`
    PublicGists int `json:"public_gists"`
    Followers int `json:"followers"`
    Following int `json:"following"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
}

Golang の struct が一瞬で出来上がります。struct 名は AutoGenerated になっているので目的の名前で置き換えて下さい。このサイト、javascript だけで実装されているので JSON はどこにも送信されません。もしどうしても外部のサイトに JSON を食わしたくないというのであれば以下のリポジトリを clone して自前で実行すれば良いです。

GitHub - mholt/json-to-go: Translates JSON into a Go type in your browser instantly

README.md Translates JSON into a Go type definition. Check it out! This is a sister tool to curl-to-...

https://github.com/mholt/json-to-go

尚、いちいちサーバ立ち上げるのめんどくさいという事であれば、コマンドラインで実行するためのパッチもあります。

Publish CLI to npm? · Issue #17 · mholt/json-to-go · GitHub
https://github.com/mholt/json-to-go/issues/17#issuecomment-224544937

便利ですね。

Posted at 15:13 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.