Go
Go output depends on three packages of the runtime github.com/oldsheeep3/comatype/packages/go: the shared comatype, the client c, and the server s.
What is generated#
<out>/
├─ types/types.go Named types (package types)
├─ init/init.go Wrappers, ctx and wrapper statuses (package comatype_init)
└─ user/put__id/endpoint.go One per endpoint (package user_put_id)The contents of init and endpoint differ between client and server. The import path of the output is decided from importPath in comatype.config.json, or from module in go.mod if that is missing.
Client#
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
}- The argument is
Requestif there areparams/search,Bodyif there is only abody, and it can be left out if there is neither (c.Options(ctx, ep)). - The return value is
*Response:Statusplus a field per status (S200S200Headers...).nilif there is no body. - A status that is not in the
.comatis*comatype.UnexpectedStatusError. - A validation error is
*comatype.ValidationError(PathandReason).
Go
c.Init(c.Config{
Wrappers: comatype_init.Wrappers{Auth: wrappers.Auth},
CookieJar: c.NewCookieJar(),
})Server#
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 (when wrap auth is enabled)
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())- The constructor is
S200(body). For anonebody it isS204(). It takes headers as arguments only when there are required response headers (S200(nil, &S200Headers{...})). Optional headers go through.WithS200Headers(&S200Headers{...}). - Cookies use
.WithCookie(value, comatype.MaxAge(3600))/.ClearCookie().
Type mapping#
uuid,emailand the like are separate types that wrap a string. Converting fromstringis required (comatype.Uuidv4(id)).Validate()checks the format.int64iscomatype.Int64,datetimeistime.Time,bytesis[]byte, andjsonisjson.RawMessage.- Optional (
?) fields are pointers (*string).c.Ptr(true)makes one. - Constraints go into struct tags (
minmaxpatternoneof). - Field names follow Go conventions (
IDUIDURL), and the names of declared types stay as they are.
Notes#
- A missing wrapper cannot be caught by types (a struct literal can leave fields out). It becomes an error on the client, and
500on the server, when an endpoint that uses it is called. - Union types support
T | nulland unions of only string literals.
Real example#
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})
})
}