2021-12-10 17:15:33 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package apigen
|
|
|
|
|
2022-01-11 13:20:02 +00:00
|
|
|
import (
|
|
|
|
"go/format"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2022-06-09 05:12:50 +01:00
|
|
|
"sort"
|
2022-01-11 13:20:02 +00:00
|
|
|
"strings"
|
2022-02-17 13:49:07 +00:00
|
|
|
"time"
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-02-17 13:49:07 +00:00
|
|
|
"storj.io/common/uuid"
|
2022-01-11 13:20:02 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
// DateFormat is the layout of dates passed into and out of the API.
|
|
|
|
const DateFormat = "2006-01-02T15:04:05.999Z"
|
|
|
|
|
2022-05-05 15:03:51 +01:00
|
|
|
// MustWriteGo writes generated Go code into a file.
|
2023-08-23 19:10:05 +01:00
|
|
|
// If an error occurs, it panics.
|
2022-05-05 15:03:51 +01:00
|
|
|
func (a *API) MustWriteGo(path string) {
|
2022-01-11 13:20:02 +00:00
|
|
|
generated, err := a.generateGo()
|
|
|
|
if err != nil {
|
2023-09-23 18:37:11 +01:00
|
|
|
panic(err)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 15:06:52 +00:00
|
|
|
err = os.WriteFile(path, generated, 0644)
|
2022-01-11 13:20:02 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(errs.Wrap(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateGo generates api code and returns an output.
|
|
|
|
func (a *API) generateGo() ([]byte, error) {
|
2023-02-22 10:08:34 +00:00
|
|
|
result := &StringBuilder{}
|
|
|
|
pf := result.Writelnf
|
2022-01-11 13:20:02 +00:00
|
|
|
|
|
|
|
getPackageName := func(path string) string {
|
|
|
|
pathPackages := strings.Split(path, "/")
|
2023-09-25 13:08:33 +01:00
|
|
|
name := pathPackages[len(pathPackages)-1]
|
|
|
|
if name == "main" {
|
|
|
|
panic(errs.New(`invalid package name. Your types cannot be defined in a package named "main"`))
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 05:12:50 +01:00
|
|
|
imports := struct {
|
|
|
|
All map[string]bool
|
|
|
|
Standard []string
|
|
|
|
External []string
|
|
|
|
Internal []string
|
|
|
|
}{
|
|
|
|
All: make(map[string]bool),
|
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-06-09 05:12:50 +01:00
|
|
|
i := func(paths ...string) {
|
|
|
|
for _, path := range paths {
|
2022-06-16 03:07:38 +01:00
|
|
|
if path == "" || getPackageName(path) == a.PackageName {
|
2022-07-14 04:43:33 +01:00
|
|
|
continue
|
2022-06-09 05:12:50 +01:00
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-06-09 05:12:50 +01:00
|
|
|
if _, ok := imports.All[path]; ok {
|
2022-07-14 04:43:33 +01:00
|
|
|
continue
|
2022-06-09 05:12:50 +01:00
|
|
|
}
|
|
|
|
imports.All[path] = true
|
|
|
|
|
|
|
|
var slice *[]string
|
|
|
|
switch {
|
|
|
|
case !strings.Contains(path, "."):
|
|
|
|
slice = &imports.Standard
|
|
|
|
case strings.HasPrefix(path, "storj.io"):
|
|
|
|
slice = &imports.Internal
|
|
|
|
default:
|
|
|
|
slice = &imports.External
|
|
|
|
}
|
|
|
|
*slice = append(*slice, path)
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2023-02-22 10:08:34 +00:00
|
|
|
var getTypePackages func(t reflect.Type) []string
|
|
|
|
getTypePackages = func(t reflect.Type) []string {
|
|
|
|
t = getElementaryType(t)
|
|
|
|
if t.Kind() == reflect.Map {
|
|
|
|
pkgs := []string{getElementaryType(t.Key()).PkgPath()}
|
|
|
|
return append(pkgs, getTypePackages(t.Elem())...)
|
|
|
|
}
|
|
|
|
return []string{t.PkgPath()}
|
|
|
|
}
|
|
|
|
|
2022-01-11 13:20:02 +00:00
|
|
|
for _, group := range a.EndpointGroups {
|
2022-06-09 16:23:08 +01:00
|
|
|
for _, method := range group.endpoints {
|
2022-01-11 13:20:02 +00:00
|
|
|
if method.Request != nil {
|
2023-02-22 10:08:34 +00:00
|
|
|
i(getTypePackages(reflect.TypeOf(method.Request))...)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
if method.Response != nil {
|
2023-02-22 10:08:34 +00:00
|
|
|
i(getTypePackages(reflect.TypeOf(method.Response))...)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range a.EndpointGroups {
|
2022-06-09 05:12:50 +01:00
|
|
|
i("github.com/zeebo/errs")
|
2023-08-24 17:55:59 +01:00
|
|
|
pf(
|
|
|
|
"var Err%sAPI = errs.Class(\"%s %s api\")",
|
2023-10-03 18:41:59 +01:00
|
|
|
capitalize(group.Prefix),
|
2023-08-24 17:55:59 +01:00
|
|
|
a.PackageName,
|
2023-10-03 18:41:59 +01:00
|
|
|
strings.ToLower(group.Prefix),
|
2023-08-24 17:55:59 +01:00
|
|
|
)
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
|
|
|
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
params := make(map[*fullEndpoint][]Param)
|
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
for _, group := range a.EndpointGroups {
|
2023-10-03 18:41:59 +01:00
|
|
|
pf("type %sService interface {", capitalize(group.Name))
|
2022-06-09 16:23:08 +01:00
|
|
|
for _, e := range group.endpoints {
|
2022-08-31 14:55:28 +01:00
|
|
|
params[e] = append(e.PathParams, e.QueryParams...)
|
2022-07-14 04:43:33 +01:00
|
|
|
|
|
|
|
var paramStr string
|
2022-08-31 14:55:28 +01:00
|
|
|
for i, param := range params[e] {
|
|
|
|
paramStr += param.Name
|
|
|
|
if i == len(params[e])-1 || param.Type != params[e][i+1].Type {
|
|
|
|
paramStr += " " + param.Type.String()
|
|
|
|
}
|
|
|
|
paramStr += ", "
|
2022-07-14 04:43:33 +01:00
|
|
|
}
|
|
|
|
if e.Request != nil {
|
2022-08-31 14:55:28 +01:00
|
|
|
paramStr += "request " + reflect.TypeOf(e.Request).String() + ", "
|
2022-02-17 13:49:07 +00:00
|
|
|
}
|
2022-04-28 16:59:55 +01:00
|
|
|
|
2022-06-09 05:12:50 +01:00
|
|
|
i("context", "storj.io/storj/private/api")
|
2022-04-28 16:59:55 +01:00
|
|
|
if e.Response != nil {
|
|
|
|
responseType := reflect.TypeOf(e.Response)
|
2022-07-14 04:43:33 +01:00
|
|
|
returnParam := a.handleTypesPackage(responseType)
|
2023-02-22 10:08:34 +00:00
|
|
|
if !isNillableType(responseType) {
|
2022-07-14 04:43:33 +01:00
|
|
|
returnParam = "*" + returnParam
|
|
|
|
}
|
2023-09-27 17:48:01 +01:00
|
|
|
pf("%s(ctx context.Context, "+paramStr+") (%s, api.HTTPError)", e.GoName, returnParam)
|
2022-04-28 16:59:55 +01:00
|
|
|
} else {
|
2023-09-27 17:48:01 +01:00
|
|
|
pf("%s(ctx context.Context, "+paramStr+") (api.HTTPError)", e.GoName)
|
2022-04-28 16:59:55 +01:00
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
for _, group := range a.EndpointGroups {
|
2023-10-03 18:41:59 +01:00
|
|
|
cname := capitalize(group.Name)
|
2022-06-16 03:07:38 +01:00
|
|
|
i("go.uber.org/zap", "github.com/spacemonkeygo/monkit/v3")
|
2023-10-03 18:41:59 +01:00
|
|
|
pf(
|
|
|
|
"// %sHandler is an api handler that implements all %s API endpoints functionality.",
|
|
|
|
cname,
|
|
|
|
group.Name,
|
|
|
|
)
|
|
|
|
pf("type %sHandler struct {", cname)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("log *zap.Logger")
|
|
|
|
pf("mon *monkit.Scope")
|
2023-10-03 18:41:59 +01:00
|
|
|
pf("service %sService", cname)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("auth api.Auth")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
for _, group := range a.EndpointGroups {
|
2023-10-03 18:41:59 +01:00
|
|
|
cname := capitalize(group.Name)
|
2022-06-09 05:12:50 +01:00
|
|
|
i("github.com/gorilla/mux")
|
2022-08-09 22:38:01 +01:00
|
|
|
pf(
|
2022-06-16 03:07:38 +01:00
|
|
|
"func New%s(log *zap.Logger, mon *monkit.Scope, service %sService, router *mux.Router, auth api.Auth) *%sHandler {",
|
2023-10-03 18:41:59 +01:00
|
|
|
cname,
|
|
|
|
cname,
|
|
|
|
cname,
|
2022-01-11 13:20:02 +00:00
|
|
|
)
|
2023-10-03 18:41:59 +01:00
|
|
|
pf("handler := &%sHandler{", cname)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("log: log,")
|
|
|
|
pf("mon: mon,")
|
|
|
|
pf("service: service,")
|
|
|
|
pf("auth: auth,")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2023-10-03 18:41:59 +01:00
|
|
|
pf(
|
|
|
|
"%sRouter := router.PathPrefix(\"%s/%s\").Subrouter()",
|
|
|
|
uncapitalize(group.Prefix),
|
|
|
|
a.endpointBasePath(),
|
|
|
|
strings.ToLower(group.Prefix),
|
|
|
|
)
|
2022-06-09 16:23:08 +01:00
|
|
|
for _, endpoint := range group.endpoints {
|
2023-09-27 17:48:01 +01:00
|
|
|
handlerName := "handle" + endpoint.GoName
|
2023-08-24 17:55:59 +01:00
|
|
|
pf(
|
|
|
|
"%sRouter.HandleFunc(\"%s\", handler.%s).Methods(\"%s\")",
|
2023-10-03 18:41:59 +01:00
|
|
|
uncapitalize(group.Prefix),
|
2023-08-24 17:55:59 +01:00
|
|
|
endpoint.Path,
|
|
|
|
handlerName,
|
|
|
|
endpoint.Method,
|
|
|
|
)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("")
|
|
|
|
pf("return handler")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-05-06 12:29:59 +01:00
|
|
|
}
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-05-06 12:29:59 +01:00
|
|
|
for _, group := range a.EndpointGroups {
|
2022-06-09 16:23:08 +01:00
|
|
|
for _, endpoint := range group.endpoints {
|
2022-06-09 05:12:50 +01:00
|
|
|
i("net/http")
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("")
|
2023-09-27 17:48:01 +01:00
|
|
|
handlerName := "handle" + endpoint.GoName
|
2023-10-03 18:41:59 +01:00
|
|
|
pf("func (h *%sHandler) %s(w http.ResponseWriter, r *http.Request) {", capitalize(group.Name), handlerName)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("ctx := r.Context()")
|
|
|
|
pf("var err error")
|
|
|
|
pf("defer h.mon.Task()(&ctx)(&err)")
|
|
|
|
pf("")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("w.Header().Set(\"Content-Type\", \"application/json\")")
|
|
|
|
pf("")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2023-02-22 10:08:34 +00:00
|
|
|
if err := handleParams(result, i, endpoint.PathParams, endpoint.QueryParams); err != nil {
|
2023-01-10 18:03:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.Request != nil {
|
|
|
|
handleBody(pf, endpoint.Request)
|
|
|
|
}
|
|
|
|
|
2022-03-27 11:16:46 +01:00
|
|
|
if !endpoint.NoCookieAuth || !endpoint.NoAPIAuth {
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("ctx, err = h.auth.IsAuthenticated(ctx, r, %v, %v)", !endpoint.NoCookieAuth, !endpoint.NoAPIAuth)
|
|
|
|
pf("if err != nil {")
|
2022-06-05 23:41:38 +01:00
|
|
|
if !endpoint.NoCookieAuth {
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("h.auth.RemoveAuthCookie(w)")
|
2022-06-05 23:41:38 +01:00
|
|
|
}
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("api.ServeError(h.log, w, http.StatusUnauthorized, err)")
|
|
|
|
pf("return")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:59:55 +01:00
|
|
|
var methodFormat string
|
|
|
|
if endpoint.Response != nil {
|
|
|
|
methodFormat = "retVal, httpErr := h.service.%s(ctx, "
|
|
|
|
} else {
|
|
|
|
methodFormat = "httpErr := h.service.%s(ctx, "
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
for _, param := range params[endpoint] {
|
|
|
|
methodFormat += param.Name + ", "
|
|
|
|
}
|
|
|
|
if endpoint.Request != nil {
|
|
|
|
methodFormat += "payload"
|
2022-02-17 13:49:07 +00:00
|
|
|
}
|
2022-03-21 12:15:33 +00:00
|
|
|
|
2022-02-17 13:49:07 +00:00
|
|
|
methodFormat += ")"
|
2023-09-27 17:48:01 +01:00
|
|
|
pf(methodFormat, endpoint.GoName)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("if httpErr.Err != nil {")
|
|
|
|
pf("api.ServeError(h.log, w, httpErr.Status, httpErr.Err)")
|
2022-04-28 16:59:55 +01:00
|
|
|
if endpoint.Response == nil {
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("}")
|
|
|
|
pf("}")
|
2022-04-28 16:59:55 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("return")
|
|
|
|
pf("}")
|
2022-01-11 13:20:02 +00:00
|
|
|
|
2022-06-09 05:12:50 +01:00
|
|
|
i("encoding/json")
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("")
|
|
|
|
pf("err = json.NewEncoder(w).Encode(retVal)")
|
|
|
|
pf("if err != nil {")
|
2023-08-24 17:55:59 +01:00
|
|
|
pf(
|
|
|
|
"h.log.Debug(\"failed to write json %s response\", zap.Error(Err%sAPI.Wrap(err)))",
|
2023-09-27 17:48:01 +01:00
|
|
|
endpoint.GoName,
|
2023-10-03 18:41:59 +01:00
|
|
|
capitalize(group.Prefix),
|
2023-08-24 17:55:59 +01:00
|
|
|
)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("}")
|
|
|
|
pf("}")
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 10:08:34 +00:00
|
|
|
fileBody := result.String()
|
|
|
|
result = &StringBuilder{}
|
|
|
|
pf = result.Writelnf
|
2022-06-09 05:12:50 +01:00
|
|
|
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("// AUTOGENERATED BY private/apigen")
|
|
|
|
pf("// DO NOT EDIT.")
|
|
|
|
pf("")
|
2022-06-09 05:12:50 +01:00
|
|
|
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("package %s", a.PackageName)
|
|
|
|
pf("")
|
2022-06-09 05:12:50 +01:00
|
|
|
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("import (")
|
2022-06-09 05:12:50 +01:00
|
|
|
slices := [][]string{imports.Standard, imports.External, imports.Internal}
|
|
|
|
for sn, slice := range slices {
|
|
|
|
sort.Strings(slice)
|
|
|
|
for pn, path := range slice {
|
2022-08-09 22:38:01 +01:00
|
|
|
pf(`"%s"`, path)
|
2022-06-09 05:12:50 +01:00
|
|
|
if pn == len(slice)-1 && sn < len(slices)-1 {
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("")
|
2022-06-09 05:12:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 22:38:01 +01:00
|
|
|
pf(")")
|
|
|
|
pf("")
|
2022-06-09 05:12:50 +01:00
|
|
|
|
2022-06-16 03:07:38 +01:00
|
|
|
if _, ok := imports.All["time"]; ok {
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("const dateLayout = \"%s\"", DateFormat)
|
|
|
|
pf("")
|
2022-06-16 03:07:38 +01:00
|
|
|
}
|
|
|
|
|
2023-02-22 10:08:34 +00:00
|
|
|
result.WriteString(fileBody)
|
2022-06-09 05:12:50 +01:00
|
|
|
|
2023-02-22 10:08:34 +00:00
|
|
|
output, err := format.Source([]byte(result.String()))
|
2022-01-11 13:20:02 +00:00
|
|
|
if err != nil {
|
2023-09-23 18:37:11 +01:00
|
|
|
return nil, errs.Wrap(err)
|
2022-01-11 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
2022-02-11 15:06:52 +00:00
|
|
|
|
|
|
|
// handleTypesPackage handles the way some type is used in generated code.
|
|
|
|
// If type is from the same package then we use only type's name.
|
2022-04-28 16:59:55 +01:00
|
|
|
// If type is from external package then we use type along with its appropriate package name.
|
2022-07-14 04:43:33 +01:00
|
|
|
func (a *API) handleTypesPackage(t reflect.Type) string {
|
2022-02-11 15:06:52 +00:00
|
|
|
if strings.HasPrefix(t.String(), a.PackageName) {
|
|
|
|
return t.Elem().Name()
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
return t.String()
|
2022-02-11 15:06:52 +00:00
|
|
|
}
|
2022-04-28 16:59:55 +01:00
|
|
|
|
2022-08-31 14:55:28 +01:00
|
|
|
// handleParams handles parsing of URL path parameters or query parameters.
|
2023-02-22 10:08:34 +00:00
|
|
|
func handleParams(builder *StringBuilder, i func(paths ...string), pathParams, queryParams []Param) error {
|
|
|
|
pf := builder.Writelnf
|
2022-08-31 14:55:28 +01:00
|
|
|
pErrCheck := func() {
|
|
|
|
pf("if err != nil {")
|
|
|
|
pf("api.ServeError(h.log, w, http.StatusBadRequest, err)")
|
|
|
|
pf("return")
|
|
|
|
pf("}")
|
|
|
|
}
|
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
for _, params := range []*[]Param{&queryParams, &pathParams} {
|
|
|
|
for _, param := range *params {
|
|
|
|
varName := param.Name
|
2022-08-31 14:55:28 +01:00
|
|
|
if param.Type.Kind() != reflect.String {
|
2022-07-14 04:43:33 +01:00
|
|
|
varName += "Param"
|
|
|
|
}
|
2022-04-28 16:59:55 +01:00
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
switch params {
|
|
|
|
case &queryParams:
|
2022-09-02 14:26:48 +01:00
|
|
|
pf("%s := r.URL.Query().Get(\"%s\")", varName, param.Name)
|
|
|
|
pf("if %s == \"\" {", varName)
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("api.ServeError(h.log, w, http.StatusBadRequest, errs.New(\"parameter '%s' can't be empty\"))", param.Name)
|
|
|
|
pf("return")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-07-14 04:43:33 +01:00
|
|
|
case &pathParams:
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("%s, ok := mux.Vars(r)[\"%s\"]", varName, param.Name)
|
|
|
|
pf("if !ok {")
|
|
|
|
pf("api.ServeError(h.log, w, http.StatusBadRequest, errs.New(\"missing %s route param\"))", param.Name)
|
|
|
|
pf("return")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-07-14 04:43:33 +01:00
|
|
|
}
|
2022-04-28 16:59:55 +01:00
|
|
|
|
2022-07-14 04:43:33 +01:00
|
|
|
switch param.Type {
|
|
|
|
case reflect.TypeOf(uuid.UUID{}):
|
|
|
|
i("storj.io/common/uuid")
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("%s, err := uuid.FromString(%s)", param.Name, varName)
|
2022-08-31 14:55:28 +01:00
|
|
|
pErrCheck()
|
2022-07-14 04:43:33 +01:00
|
|
|
case reflect.TypeOf(time.Time{}):
|
|
|
|
i("time")
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("%s, err := time.Parse(dateLayout, %s)", param.Name, varName)
|
2022-08-31 14:55:28 +01:00
|
|
|
pErrCheck()
|
2022-07-14 04:43:33 +01:00
|
|
|
default:
|
2022-08-31 14:55:28 +01:00
|
|
|
switch param.Type.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
i("strconv")
|
|
|
|
convName := varName
|
|
|
|
if param.Type.Kind() != reflect.Uint64 {
|
|
|
|
convName += "U64"
|
|
|
|
}
|
|
|
|
bits := param.Type.Bits()
|
|
|
|
if param.Type.Kind() == reflect.Uint {
|
|
|
|
bits = 32
|
|
|
|
}
|
|
|
|
pf("%s, err := strconv.ParseUint(%s, 10, %d)", convName, varName, bits)
|
|
|
|
pErrCheck()
|
|
|
|
if param.Type.Kind() != reflect.Uint64 {
|
|
|
|
pf("%s := %s(%s)", param.Name, param.Type.String(), convName)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errs.New("Unsupported parameter type \"%s\"", param.Type)
|
|
|
|
}
|
2022-07-14 04:43:33 +01:00
|
|
|
}
|
2022-04-28 16:59:55 +01:00
|
|
|
|
2022-08-09 22:38:01 +01:00
|
|
|
pf("")
|
2022-07-14 04:43:33 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-31 14:55:28 +01:00
|
|
|
|
|
|
|
return nil
|
2022-04-28 16:59:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleBody handles request body.
|
2022-08-09 22:38:01 +01:00
|
|
|
func handleBody(pf func(format string, a ...interface{}), body interface{}) {
|
|
|
|
pf("payload := %s{}", reflect.TypeOf(body).String())
|
|
|
|
pf("if err = json.NewDecoder(r.Body).Decode(&payload); err != nil {")
|
|
|
|
pf("api.ServeError(h.log, w, http.StatusBadRequest, err)")
|
|
|
|
pf("return")
|
|
|
|
pf("}")
|
|
|
|
pf("")
|
2022-04-28 16:59:55 +01:00
|
|
|
}
|