86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package web
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
type AccessMode int
|
|
|
|
const (
|
|
AccessMode_Read = 1
|
|
AccessMode_Write = 2
|
|
)
|
|
|
|
type RouteDescInfo struct {
|
|
Path string
|
|
Desc string
|
|
Method []string
|
|
AccessMode
|
|
rawRequest any
|
|
Fields []*FieldDescInfo
|
|
}
|
|
|
|
func newRouteDescInfo(path, desc, method string, access AccessMode, request any) *RouteDescInfo {
|
|
info := &RouteDescInfo{
|
|
Path: path,
|
|
Desc: desc,
|
|
Method: []string{method},
|
|
AccessMode: access,
|
|
rawRequest: request,
|
|
}
|
|
info.RequestFieldsDesc()
|
|
return info
|
|
}
|
|
|
|
func (info *RouteDescInfo) MethodDesc(sep string) string {
|
|
return strings.Join(info.Method, sep)
|
|
}
|
|
|
|
func (info *RouteDescInfo) RequestFieldsDesc() []*FieldDescInfo {
|
|
if !info.HasRequest() {
|
|
return []*FieldDescInfo{}
|
|
}
|
|
|
|
list := make([]*FieldDescInfo, 0)
|
|
to := reflect.TypeOf(info.rawRequest)
|
|
if to.Kind() == reflect.Ptr {
|
|
to = to.Elem()
|
|
}
|
|
for i := 0; i < to.NumField(); i++ {
|
|
field := to.Field(i)
|
|
list = append(list, newFieldDescInfo(field))
|
|
}
|
|
info.Fields = list
|
|
return list
|
|
}
|
|
|
|
func (info *RouteDescInfo) HasRequest() bool {
|
|
return info.rawRequest != nil
|
|
}
|
|
|
|
type FieldDescInfo struct {
|
|
Name string
|
|
FieldName string
|
|
Type string
|
|
Desc string
|
|
Required bool
|
|
Default string
|
|
rawTypeOfField reflect.StructField
|
|
}
|
|
|
|
func newFieldDescInfo(field reflect.StructField) *FieldDescInfo {
|
|
desc := &FieldDescInfo{
|
|
Name: field.Tag.Get("json"),
|
|
FieldName: field.Name,
|
|
Type: field.Type.String(),
|
|
Desc: field.Tag.Get("desc"),
|
|
rawTypeOfField: field,
|
|
}
|
|
if field.Tag.Get("required") == "true" {
|
|
desc.Required = true
|
|
}
|
|
desc.Default = desc.rawTypeOfField.Tag.Get("default")
|
|
return desc
|
|
}
|