2021-12-10 17:15:33 +00:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package apigen
|
|
|
|
|
2022-02-17 13:49:07 +00:00
|
|
|
import (
|
2023-09-23 18:37:11 +01:00
|
|
|
"fmt"
|
2022-02-17 13:49:07 +00:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2023-09-27 17:48:01 +01:00
|
|
|
"regexp"
|
2023-09-23 18:37:11 +01:00
|
|
|
"strings"
|
2023-10-06 12:05:58 +01:00
|
|
|
"time"
|
2023-09-27 17:48:01 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2023-10-06 12:05:58 +01:00
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2023-09-27 17:48:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errsEndpoint = errs.Class("Endpoint")
|
|
|
|
|
|
|
|
goNameRegExp = regexp.MustCompile(`^[A-Z]\w*$`)
|
|
|
|
typeScriptNameRegExp = regexp.MustCompile(`^[a-z][a-zA-Z0-9_$]*$`)
|
2022-02-17 13:49:07 +00:00
|
|
|
)
|
2021-12-10 17:15:33 +00:00
|
|
|
|
|
|
|
// Endpoint represents endpoint's configuration.
|
|
|
|
type Endpoint struct {
|
2023-09-23 17:40:30 +01:00
|
|
|
// Name is a free text used to name the endpoint for documentation purpose.
|
|
|
|
// It cannot be empty.
|
|
|
|
Name string
|
|
|
|
// Description is a free text to describe the endpoint for documentation purpose.
|
|
|
|
Description string
|
2023-09-27 17:48:01 +01:00
|
|
|
// GoName is an identifier used by the Go generator to generate specific server side code for this
|
|
|
|
// endpoint.
|
|
|
|
//
|
|
|
|
// It must start with an uppercase letter and fulfill the Go language specification for method
|
|
|
|
// names (https://go.dev/ref/spec#MethodName).
|
|
|
|
// It cannot be empty.
|
|
|
|
GoName string
|
|
|
|
// TypeScriptName is an identifier used by the TypeScript generator to generate specific client
|
|
|
|
// code for this endpoint
|
|
|
|
//
|
|
|
|
// It must start with a lowercase letter and can only contains letters, digits, _, and $.
|
|
|
|
// It cannot be empty.
|
|
|
|
TypeScriptName string
|
|
|
|
NoCookieAuth bool
|
|
|
|
NoAPIAuth bool
|
2023-09-23 17:40:30 +01:00
|
|
|
// Request is the type that defines the format of the request body.
|
|
|
|
Request interface{}
|
|
|
|
// Response is the type that defines the format of the response body.
|
|
|
|
Response interface{}
|
|
|
|
// QueryParams is the list of query parameters that the endpoint accepts.
|
|
|
|
QueryParams []Param
|
|
|
|
// PathParams is the list of path parameters that appear in the path associated with this
|
|
|
|
// endpoint.
|
|
|
|
PathParams []Param
|
2023-10-18 18:54:03 +01:00
|
|
|
// ResponseMock is the data to use as a response for the generated mocks.
|
|
|
|
// It must be of the same type than Response.
|
|
|
|
// If a mock generator is called it must not be nil unless Response is nil.
|
|
|
|
ResponseMock interface{}
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CookieAuth returns endpoint's cookie auth status.
|
|
|
|
func (e *Endpoint) CookieAuth() bool {
|
|
|
|
return !e.NoCookieAuth
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIAuth returns endpoint's API auth status.
|
|
|
|
func (e *Endpoint) APIAuth() bool {
|
|
|
|
return !e.NoAPIAuth
|
|
|
|
}
|
|
|
|
|
2023-09-27 17:48:01 +01:00
|
|
|
// Validate validates the endpoint fields values are correct according to the documented constraints.
|
|
|
|
func (e *Endpoint) Validate() error {
|
|
|
|
if e.Name == "" {
|
|
|
|
return errsEndpoint.New("Name cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Description == "" {
|
|
|
|
return errsEndpoint.New("Description cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !goNameRegExp.MatchString(e.GoName) {
|
|
|
|
return errsEndpoint.New("GoName doesn't match the regular expression %q", goNameRegExp)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !typeScriptNameRegExp.MatchString(e.TypeScriptName) {
|
|
|
|
return errsEndpoint.New("TypeScriptName doesn't match the regular expression %q", typeScriptNameRegExp)
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:34:03 +01:00
|
|
|
if e.Request != nil {
|
|
|
|
switch k := reflect.TypeOf(e.Request).Kind(); k {
|
|
|
|
case reflect.Invalid,
|
|
|
|
reflect.Complex64,
|
|
|
|
reflect.Complex128,
|
|
|
|
reflect.Chan,
|
|
|
|
reflect.Func,
|
|
|
|
reflect.Interface,
|
|
|
|
reflect.Map,
|
|
|
|
reflect.Pointer,
|
|
|
|
reflect.UnsafePointer:
|
|
|
|
return errsEndpoint.New("Request cannot be of a type %q", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Response != nil {
|
|
|
|
switch k := reflect.TypeOf(e.Response).Kind(); k {
|
|
|
|
case reflect.Invalid,
|
|
|
|
reflect.Complex64,
|
|
|
|
reflect.Complex128,
|
|
|
|
reflect.Chan,
|
|
|
|
reflect.Func,
|
|
|
|
reflect.Interface,
|
|
|
|
reflect.Map,
|
|
|
|
reflect.Pointer,
|
|
|
|
reflect.UnsafePointer:
|
|
|
|
return errsEndpoint.New("Response cannot be of a type %q", k)
|
|
|
|
}
|
2023-10-18 18:54:03 +01:00
|
|
|
|
|
|
|
if e.ResponseMock != nil {
|
|
|
|
if m, r := reflect.TypeOf(e.ResponseMock), reflect.TypeOf(e.Response); m != r {
|
|
|
|
return errsEndpoint.New(
|
|
|
|
"ResponseMock isn't of the same type than Response. Have=%q Want=%q", m, r,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 13:34:03 +01:00
|
|
|
}
|
|
|
|
|
2023-09-27 17:48:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
// fullEndpoint represents endpoint with path and method.
|
|
|
|
type fullEndpoint struct {
|
|
|
|
Endpoint
|
2021-12-10 17:15:33 +00:00
|
|
|
Path string
|
|
|
|
Method string
|
|
|
|
}
|
|
|
|
|
2023-09-23 19:07:45 +01:00
|
|
|
// requestType guarantees to return a named Go type associated to the Endpoint.Request field.
|
2023-10-03 18:27:08 +01:00
|
|
|
// g is used to avoid clashes with types defined in different groups that are different, but with
|
|
|
|
// the same name. It cannot be nil.
|
|
|
|
func (fe fullEndpoint) requestType(g *EndpointGroup) reflect.Type {
|
2023-09-23 19:07:45 +01:00
|
|
|
t := reflect.TypeOf(fe.Request)
|
2023-09-25 17:43:30 +01:00
|
|
|
if t.Name() != "" {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
switch k := t.Kind(); k {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
if t.Elem().Name() == "" {
|
2023-10-03 18:27:08 +01:00
|
|
|
t = typeCustomName{Type: t, name: compoundTypeName(capitalize(g.Prefix), fe.TypeScriptName, "Request")}
|
2023-09-25 17:43:30 +01:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2023-10-03 18:27:08 +01:00
|
|
|
t = typeCustomName{Type: t, name: compoundTypeName(capitalize(g.Prefix), fe.TypeScriptName, "Request")}
|
2023-09-25 17:43:30 +01:00
|
|
|
default:
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"BUG: Unsupported Request type. Endpoint.Method=%q, Endpoint.Path=%q, found type=%q",
|
|
|
|
fe.Method, fe.Path, k,
|
|
|
|
),
|
|
|
|
)
|
2023-09-23 19:07:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// responseType guarantees to return a named Go type associated to the Endpoint.Response field.
|
2023-10-03 18:27:08 +01:00
|
|
|
// g is used to avoid clashes with types defined in different groups that are different, but with
|
|
|
|
// the same name. It cannot be nil.
|
|
|
|
func (fe fullEndpoint) responseType(g *EndpointGroup) reflect.Type {
|
2023-09-23 19:07:45 +01:00
|
|
|
t := reflect.TypeOf(fe.Response)
|
2023-09-25 17:43:30 +01:00
|
|
|
if t.Name() != "" {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
switch k := t.Kind(); k {
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
if t.Elem().Name() == "" {
|
2023-10-03 18:27:08 +01:00
|
|
|
t = typeCustomName{Type: t, name: compoundTypeName(capitalize(g.Prefix), fe.TypeScriptName, "Response")}
|
2023-09-25 17:43:30 +01:00
|
|
|
}
|
|
|
|
case reflect.Struct:
|
2023-10-03 18:27:08 +01:00
|
|
|
t = typeCustomName{Type: t, name: compoundTypeName(capitalize(g.Prefix), fe.TypeScriptName, "Response")}
|
2023-09-25 17:43:30 +01:00
|
|
|
default:
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"BUG: Unsupported Response type. Endpoint.Method=%q, Endpoint.Path=%q, found type=%q",
|
|
|
|
fe.Method, fe.Path, k,
|
|
|
|
),
|
|
|
|
)
|
2023-09-23 19:07:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:15:33 +00:00
|
|
|
// EndpointGroup represents endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// You should always create a group using API.Group because it validates the field values to
|
|
|
|
// guarantee correct code generation.
|
2021-12-10 17:15:33 +00:00
|
|
|
type EndpointGroup struct {
|
2023-10-03 18:41:59 +01:00
|
|
|
// Name is the group name.
|
|
|
|
//
|
|
|
|
// Go generator uses it as part of type, functions, interfaces names, and in code comments.
|
|
|
|
// The casing is adjusted according where it's used.
|
|
|
|
//
|
|
|
|
// TypeScript generator uses it as part of types names for the API functionality of this group.
|
|
|
|
// The casing is adjusted according where it's used.
|
|
|
|
//
|
|
|
|
// Document generator uses as it is.
|
|
|
|
Name string
|
|
|
|
// Prefix is a prefix used for
|
|
|
|
//
|
|
|
|
// Go generator uses it as part of variables names, error messages, and the URL base path for the group.
|
|
|
|
// The casing is adjusted according where it's used, but for the URL base path, lowercase is used.
|
|
|
|
//
|
|
|
|
// TypeScript generator uses it for composing the URL base path (lowercase).
|
|
|
|
//
|
|
|
|
// Document generator uses as it is.
|
2021-12-10 17:15:33 +00:00
|
|
|
Prefix string
|
2022-06-09 16:23:08 +01:00
|
|
|
endpoints []*fullEndpoint
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get adds new GET endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2021-12-10 17:15:33 +00:00
|
|
|
func (eg *EndpointGroup) Get(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodGet, endpoint)
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:05:28 +01:00
|
|
|
// Patch adds new PATCH endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2022-04-07 09:05:28 +01:00
|
|
|
func (eg *EndpointGroup) Patch(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodPatch, endpoint)
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:52:02 +01:00
|
|
|
// Post adds new POST endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2022-04-27 12:52:02 +01:00
|
|
|
func (eg *EndpointGroup) Post(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodPost, endpoint)
|
2022-03-21 12:15:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:59:55 +01:00
|
|
|
// Delete adds new DELETE endpoint to endpoints group.
|
2023-09-23 18:37:11 +01:00
|
|
|
// It panics if path doesn't begin with '/'.
|
2022-04-28 16:59:55 +01:00
|
|
|
func (eg *EndpointGroup) Delete(path string, endpoint *Endpoint) {
|
|
|
|
eg.addEndpoint(path, http.MethodDelete, endpoint)
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:15:33 +00:00
|
|
|
// addEndpoint adds new endpoint to endpoints list.
|
2023-09-27 17:48:01 +01:00
|
|
|
// It panics if:
|
|
|
|
// - path doesn't begin with '/'.
|
|
|
|
// - endpoint.Validate() returns an error.
|
|
|
|
// - An Endpoint with the same path and method already exists.
|
2021-12-10 17:15:33 +00:00
|
|
|
func (eg *EndpointGroup) addEndpoint(path, method string, endpoint *Endpoint) {
|
2023-09-23 18:37:11 +01:00
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"invalid path for method %q of EndpointGroup %q. path must start with slash, got %q",
|
|
|
|
method,
|
|
|
|
eg.Name,
|
|
|
|
path,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-27 17:48:01 +01:00
|
|
|
if err := endpoint.Validate(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:23:08 +01:00
|
|
|
ep := &fullEndpoint{*endpoint, path, method}
|
2023-09-27 17:48:01 +01:00
|
|
|
for _, e := range eg.endpoints {
|
2022-06-09 16:23:08 +01:00
|
|
|
if e.Path == path && e.Method == method {
|
2023-09-27 17:48:01 +01:00
|
|
|
panic(fmt.Sprintf("there is already an endpoint defined with path %q and method %q", path, method))
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.GoName == ep.GoName {
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf("GoName %q is already used by the endpoint with path %q and method %q", e.GoName, e.Path, e.Method),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.TypeScriptName == ep.TypeScriptName {
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"TypeScriptName %q is already used by the endpoint with path %q and method %q",
|
|
|
|
e.TypeScriptName,
|
|
|
|
e.Path,
|
|
|
|
e.Method,
|
|
|
|
),
|
|
|
|
)
|
2022-06-09 16:23:08 +01:00
|
|
|
}
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
2022-06-09 16:23:08 +01:00
|
|
|
eg.endpoints = append(eg.endpoints, ep)
|
2021-12-10 17:15:33 +00:00
|
|
|
}
|
2022-02-17 13:49:07 +00:00
|
|
|
|
|
|
|
// Param represents string interpretation of param's name and type.
|
|
|
|
type Param struct {
|
|
|
|
Name string
|
|
|
|
Type reflect.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewParam constructor which creates new Param entity by given name and type.
|
|
|
|
func NewParam(name string, instance interface{}) Param {
|
2023-10-06 12:05:58 +01:00
|
|
|
switch t := reflect.TypeOf(instance); t {
|
|
|
|
case reflect.TypeOf(uuid.UUID{}), reflect.TypeOf(time.Time{}):
|
|
|
|
default:
|
|
|
|
switch k := t.Kind(); k {
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.String:
|
|
|
|
default:
|
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
`Unsupported parameter, only types: %q, %q, string, and "unsigned numbers" are supported . Found type=%q, Kind=%q`,
|
|
|
|
reflect.TypeOf(uuid.UUID{}),
|
|
|
|
reflect.TypeOf(time.Time{}),
|
|
|
|
t,
|
|
|
|
k,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 13:49:07 +00:00
|
|
|
return Param{
|
|
|
|
Name: name,
|
|
|
|
Type: reflect.TypeOf(instance),
|
|
|
|
}
|
|
|
|
}
|
2023-09-23 19:07:45 +01:00
|
|
|
|
|
|
|
// namedType guarantees to return a named Go type. where defines where the param is defined (e.g.
|
|
|
|
// path, query, etc.).
|
|
|
|
func (p Param) namedType(ep Endpoint, where string) reflect.Type {
|
|
|
|
if p.Type.Name() == "" {
|
|
|
|
return typeCustomName{
|
|
|
|
Type: p.Type,
|
2023-10-03 15:30:01 +01:00
|
|
|
name: compoundTypeName(ep.TypeScriptName, where, "param", p.Name),
|
2023-09-23 19:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.Type
|
|
|
|
}
|