echoswagger/converter.go

116 lines
3.1 KiB
Go
Raw Permalink Normal View History

/*
* Copyright (c) 2019 Alex aka mailoman <alex@webz.asia>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Alex aka mailoman <alex@webz.asia>
* @copyright Copyright (c) 2019 Alex aka mailoman <alex@webz.asia>
* @since 18.12.2019
*
*/
2018-08-31 14:47:37 +02:00
package echoswagger
2018-09-01 17:18:38 +02:00
import (
"reflect"
"strconv"
"strings"
"time"
)
// toSwaggerType returns type、format for a reflect.Type in swagger format
func toSwaggerType(t reflect.Type) (string, string) {
if t == reflect.TypeOf(time.Time{}) {
return "string", "date-time"
}
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
return "integer", "int32"
case reflect.Int64, reflect.Uint64:
return "integer", "int64"
case reflect.Float32:
return "number", "float"
case reflect.Float64:
return "number", "double"
case reflect.String:
return "string", "string"
case reflect.Bool:
return "boolean", "boolean"
case reflect.Struct:
return "object", "object"
case reflect.Map:
return "object", "map"
case reflect.Array, reflect.Slice:
return "array", "array"
case reflect.Ptr:
return toSwaggerType(t.Elem())
default:
return "string", "string"
}
}
// toSwaggerPath returns path in swagger format
func toSwaggerPath(path string) string {
var params []string
for i := 0; i < len(path); i++ {
if path[i] == ':' {
j := i + 1
for ; i < len(path) && path[i] != '/'; i++ {
}
params = append(params, path[j:i])
}
}
for _, name := range params {
path = strings.Replace(path, ":"+name, "{"+name+"}", 1)
}
return connectPath(path)
2018-08-31 14:47:37 +02:00
}
2018-09-01 17:18:38 +02:00
2018-09-04 14:09:11 +02:00
func converter(t reflect.Type) func(s string) (interface{}, error) {
st, sf := toSwaggerType(t)
if st == "integer" && sf == "int32" {
2018-09-01 17:18:38 +02:00
return func(s string) (interface{}, error) {
v, err := strconv.Atoi(s)
return v, err
}
2018-09-04 14:09:11 +02:00
} else if st == "integer" && sf == "int64" {
2018-09-01 17:18:38 +02:00
return func(s string) (interface{}, error) {
v, err := strconv.ParseInt(s, 10, 64)
return v, err
}
2018-09-04 14:09:11 +02:00
} else if st == "number" && sf == "float" {
2018-09-01 17:18:38 +02:00
return func(s string) (interface{}, error) {
v, err := strconv.ParseFloat(s, 32)
return float32(v), err
}
2018-09-04 14:09:11 +02:00
} else if st == "number" && sf == "double" {
2018-09-01 17:18:38 +02:00
return func(s string) (interface{}, error) {
v, err := strconv.ParseFloat(s, 64)
return v, err
}
2018-09-04 14:09:11 +02:00
} else if st == "boolean" && sf == "boolean" {
2018-09-01 17:18:38 +02:00
return func(s string) (interface{}, error) {
2018-09-04 14:09:11 +02:00
v, err := strconv.ParseBool(s)
return v, err
2018-09-01 17:18:38 +02:00
}
2018-09-04 14:09:11 +02:00
} else if st == "array" && sf == "array" {
return converter(t.Elem())
} else {
return func(s string) (interface{}, error) {
return s, nil
2018-09-01 17:18:38 +02:00
}
}
}