2022-05-05 15:03:51 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package apigen
|
|
|
|
|
|
|
|
import (
|
2023-02-22 10:08:34 +00:00
|
|
|
"fmt"
|
2023-08-24 17:55:59 +01:00
|
|
|
"path"
|
2023-02-22 10:08:34 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2022-05-05 15:03:51 +01:00
|
|
|
"storj.io/storj/private/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// API represents specific API's configuration.
|
|
|
|
type API struct {
|
2023-08-24 17:55:59 +01:00
|
|
|
// Version is the corresponding version of the API.
|
|
|
|
// It's concatenated to the BasePath, so assuming the base path is "/api" and the version is "v1"
|
|
|
|
// the API paths will begin with `/api/v1`.
|
|
|
|
// When empty, the version doesn't appear in the API paths. If it starts or ends with one or more
|
|
|
|
// "/", they are stripped from the API endpoint paths.
|
|
|
|
Version string
|
|
|
|
Description string
|
|
|
|
// The package name to use for the Go generated code.
|
|
|
|
PackageName string
|
|
|
|
// BasePath is the base path for the API endpoints. E.g. "/api".
|
|
|
|
// It doesn't require to begin with "/". When empty, "/" is used.
|
|
|
|
BasePath string
|
2022-05-05 15:03:51 +01:00
|
|
|
Auth api.Auth
|
|
|
|
EndpointGroups []*EndpointGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group adds new endpoints group to API.
|
|
|
|
func (a *API) Group(name, prefix string) *EndpointGroup {
|
|
|
|
group := &EndpointGroup{
|
2022-06-09 16:23:08 +01:00
|
|
|
Name: name,
|
|
|
|
Prefix: prefix,
|
2022-05-05 15:03:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
a.EndpointGroups = append(a.EndpointGroups, group)
|
|
|
|
|
|
|
|
return group
|
|
|
|
}
|
2023-02-22 10:08:34 +00:00
|
|
|
|
2023-08-24 17:55:59 +01:00
|
|
|
func (a *API) endpointBasePath() string {
|
|
|
|
if strings.HasPrefix(a.BasePath, "/") {
|
|
|
|
return path.Join(a.BasePath, a.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "/" + path.Join(a.BasePath, a.Version)
|
|
|
|
}
|
|
|
|
|
2023-02-22 10:08:34 +00:00
|
|
|
// StringBuilder is an extension of strings.Builder that allows for writing formatted lines.
|
|
|
|
type StringBuilder struct{ strings.Builder }
|
|
|
|
|
|
|
|
// Writelnf formats arguments according to a format specifier
|
|
|
|
// and appends the resulting string to the StringBuilder's buffer.
|
|
|
|
func (s *StringBuilder) Writelnf(format string, a ...interface{}) {
|
|
|
|
s.WriteString(fmt.Sprintf(format+"\n", a...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// getElementaryType simplifies a Go type.
|
|
|
|
func getElementaryType(t reflect.Type) reflect.Type {
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Array, reflect.Chan, reflect.Ptr, reflect.Slice:
|
|
|
|
return getElementaryType(t.Elem())
|
|
|
|
default:
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter returns a new slice of reflect.Type values that satisfy the given keep function.
|
|
|
|
func filter(types []reflect.Type, keep func(reflect.Type) bool) []reflect.Type {
|
|
|
|
filtered := make([]reflect.Type, 0, len(types))
|
|
|
|
for _, t := range types {
|
|
|
|
if keep(t) {
|
|
|
|
filtered = append(filtered, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
// isNillableType returns whether instances of the given type can be nil.
|
|
|
|
func isNillableType(t reflect.Type) bool {
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Chan, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|