private/apigen: Validate param types
The Go generator only supports certain types as query and path parameters and it panics on any an unsupported type. The Document and TypeScript generator don't have any validation for them. TypeScript generator generates code that compiles, however, it won't work properly with all the types not supported by the Go generator. Because it doesn't make sense that some types may work on the TypeScript generator, while the Go generator doesn't, doing the validation in the Param constructor is better because it reports the issue without having to run the Go generator and it gives a more understanding panic message. TypeScript generator generates code that works properly with all the types supported by the Go generator, hence, there isn't any change int he TypeScript generator in this commit. Change-Id: I03085283942a98341726a1560f511a46540df9f5
This commit is contained in:
parent
0db898b0a8
commit
386a978310
@ -9,8 +9,11 @@ import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/common/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -282,6 +285,24 @@ type Param struct {
|
||||
|
||||
// NewParam constructor which creates new Param entity by given name and type.
|
||||
func NewParam(name string, instance interface{}) Param {
|
||||
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,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return Param{
|
||||
Name: name,
|
||||
Type: reflect.TypeOf(instance),
|
||||
|
Loading…
Reference in New Issue
Block a user