Go
Go の生成物は、ランタイム github.com/oldsheeep3/comatype/packages/go の 3 つのパッケージ(共有の comatype、クライアントの c、サーバの s)に依存します。
生成されるもの#
<out>/
├─ types/types.go 名前付きの型(package types)
├─ init/init.go ラッパー・ctx・ラッパーのステータス(package comatype_init)
└─ user/put__id/endpoint.go エンドポイントごと(package user_put_id)client と server で、init と endpoint の中身が違います。生成先の import path は、comatype.config.json の importPath、無ければ go.mod の module から決めます。
クライアント#
Go
res, err := c.Put(ctx, user_put_id.Endpoint, user_put_id.Request{
Params: user_put_id.Params{ID: comatype.Uuidv4(id)},
Body: userInput,
})
if err != nil { return err }
switch res.Status {
case 200:
_ = res.S200 // *user_put_id.S200Res
case 404:
_ = res.S404
}- 引数は、
params/searchがあればRequest、bodyだけならBody、無ければ省略できます(c.Options(ctx, ep))。 - 戻り値は
*Response。Statusと、ステータスごとのフィールド(S200S200Headers...)です。ボディが無ければnil。 .comatに無いステータスは、*comatype.UnexpectedStatusError。- 検証エラーは
*comatype.ValidationError(PathとReason)。
Go
c.Init(c.Config{
Wrappers: comatype_init.Wrappers{Auth: wrappers.Auth},
CookieJar: c.NewCookieJar(),
})サーバ#
Go
s.Put(user_put_id.Endpoint, func(req user_put_id.Request) user_put_id.Response {
// req.Params.ID, req.Body, req.Ctx.User(wrap auth が有効なとき)
if !ok {
return user_put_id.S404(&user_put_id.S404Res{Code: "not_found", Message: "not found"})
}
return user_put_id.S200(&user)
})
s.Init(s.Config{Wrappers: comatype_init.Wrappers{Auth: wrappers.Auth}})
http.ListenAndServe(":3000", s.Handler())- コンストラクタは
S200(body)。ボディがnoneならS204()。必須のレスポンスヘッダがあるときだけ、引数に取ります(S200(nil, &S200Headers{...}))。任意のヘッダは.WithS200Headers(&S200Headers{...})。 - cookie は
.WithCookie(value, comatype.MaxAge(3600))/.ClearCookie()。
型の対応#
uuidemailなどは、文字列を包んだ別の型です。stringからは変換が要ります(comatype.Uuidv4(id))。Validate()で形式を確かめます。int64はcomatype.Int64、datetimeはtime.Time、bytesは[]byte、jsonはjson.RawMessage。- 省略可能(
?)のフィールドは、ポインタ(*string)です。c.Ptr(true)で作れます。 - 制約は構造体のタグ(
minmaxpatternoneof)に入ります。 - フィールド名は Go の慣習(
IDUIDURL)で、宣言した型の名前はそのままです。
注意#
- ラッパーが足りないことを、型では検出できません(構造体のリテラルは省略できるため)。使うエンドポイントを呼ぶときに、クライアントはエラー、サーバは
500になります。 - ユニオン型は、
T | nullと文字列リテラルだけのユニオンに対応しています。
実例#
Go
package main
import (
"context"
"github.com/oldsheeep3/comatype/packages/go/c"
"github.com/oldsheeep3/comatype/packages/go/comatype"
user_put_id "example.com/app/comatype/user/put__id"
)
// Body is the request body of user/put__id.comat (UserData)
func UserPutId(ctx context.Context, id string, userInput user_put_id.Body) error {
res, err := c.Put(ctx, user_put_id.Endpoint, user_put_id.Request{
Params: user_put_id.Params{ID: comatype.Uuidv4(id)}, // uuidv4 is a custom type wrapping a string (comatype.Uuidv4). Validate() checks the format
Body: userInput,
})
if err != nil {
return err
}
switch res.Status {
case 200:
_ = res.S200 // *user_put_id.S200Res (UserData)
case 404:
_ = res.S404 // *user_put_id.S404Res (ApiError)
}
return nil
}Go
package main
import (
"github.com/oldsheeep3/comatype/packages/go/s"
user_put_id "example.com/app/comatype/user/put__id"
)
func registerUserPutId() {
s.Put(user_put_id.Endpoint, func(req user_put_id.Request) user_put_id.Response {
// req.Params.ID: string, req.Body: UserData
user, ok := db.User.Update(req.Params.ID, req.Body) // the app's own storage
if !ok {
return user_put_id.S404(&user_put_id.S404Res{Code: "not_found", Message: "user not found"})
}
return user_put_id.S200(&user_put_id.S200Res{Name: user.Name, Email: user.Email, Phone: user.Phone, UID: user.UID})
})
}