Documentation
¶
Index ¶
- Constants
- func CheckDBResult(result *gorm.DB)
- func ContextWithRequestBody(c *gin.Context, req *http.Request) error
- func ContextWithResource(c *gin.Context, resource IResource)
- func GetMethodName(method HttpMethod) string
- func GetMethodsName(methods map[HttpMethod]HandlerFunc) []string
- func GetQuery(c *gin.Context) map[string]string
- func New() *restful
- type Controller
- type HandlerDecorator
- type HandlerFunc
- type HttpMethod
- type IController
- type IDecorator
- type IDelete
- type IDeleteInit
- type IGet
- type IGetInit
- type IList
- type IListInit
- type IModel
- type IPatch
- type IPatchInit
- type IPost
- type IPostInit
- type IPut
- type IPutInit
- type IResource
- type IRoot
- type ISearch
- type ISearchInit
- type ISerializer
- type IValidator
- type MethodType
- type ModelValidator
- type ModelValidatorCtx
- type RequestBody
- type Resource
- func (resource *Resource) GetDB() *gorm.DB
- func (resource *Resource) GetModel() *model.Model
- func (resource *Resource) GetPartialSerializer(m *model.Model) ISerializer
- func (resource *Resource) GetPrimaryKey(c *gin.Context) interface{}
- func (resource *Resource) GetSerializer(m *model.Model) ISerializer
- func (resource *Resource) Init(instance interface{}, root IRoot)
- func (resource *Resource) Query() *gorm.DB
- func (resource *Resource) QueryPrimaryKey(c *gin.Context) *gorm.DB
- func (resource *Resource) QueryWithContext(ctx *gin.Context) *gorm.DB
- type Response
- type Serializer
- func (s *Serializer) Get(key string) (interface{}, error)
- func (s *Serializer) GetWithDefault(key string, value interface{}) interface{}
- func (s *Serializer) JsonData() map[string]interface{}
- func (s *Serializer) Parse(c *gin.Context, b []byte) error
- func (s *Serializer) ParseFromBody(c *gin.Context) error
- func (s *Serializer) ParseFromQuery(c *gin.Context, query map[string]string) error
- func (s *Serializer) StructData() interface{}
- func (s *Serializer) Validate(c *gin.Context) *response.Error
- func (s *Serializer) ValidateData() map[string]interface{}
- func (s *Serializer) WithDefaults(keys []string) ISerializer
- type Validator
Constants ¶
const (
TimeFormat string = "2006-01-02 15:04:05"
)
Variables ¶
This section is empty.
Functions ¶
func CheckDBResult ¶
func ContextWithRequestBody ¶
ContextWithRequestBody 将 RequestBody 设置到 ctx 里去,之后可以使用 RequestBodyFromContext 读取到
func ContextWithResource ¶
ContextWithResource 将 Resource 设置到 ctx 里去,之后可以使用 ResourceFromContext 读取到
func GetMethodName ¶
func GetMethodName(method HttpMethod) string
GetMethodName 根据 HTTP Method 返回对应的操作值
func GetMethodsName ¶
func GetMethodsName(methods map[HttpMethod]HandlerFunc) []string
GetMethodsName 获取 HTTP 请求方法列表
Types ¶
type Controller ¶
type Controller struct {
HaveDetail bool
// contains filtered or unexported fields
}
Controller 定义 Restful 路由转发相关 api 结构体
func NewController ¶
func NewController() *Controller
func (*Controller) Init ¶
func (ctrl *Controller) Init(instance interface{}, root IRoot)
Init Resource 初始化
func (*Controller) Mount ¶
func (ctrl *Controller) Mount(router *gin.RouterGroup, urlPath string)
Mount 将 Resource 方法注册到路由
func (*Controller) Print ¶
func (ctrl *Controller) Print(url string)
func (*Controller) RegisterMethod ¶
func (ctrl *Controller) RegisterMethod(methodType MethodType, httpMethod HttpMethod, postfix string, handler HandlerFunc)
RegisterMethod 操作方法和返回类型注册
type HandlerDecorator ¶
type HandlerDecorator func(HandlerFunc) HandlerFunc
HandlerDecorator 装饰器/中间件方法
type HandlerFunc ¶
func InstallDecorators ¶
func InstallDecorators(handler HandlerFunc, decorators []HandlerDecorator) HandlerFunc
type HttpMethod ¶
type HttpMethod int32
HttpMethod 原生方法
const ( HTTPMethodGet HttpMethod = iota + 1 HTTPMethodPost HTTPMethodPut HTTPMethodPatch HTTPMethodDelete )
HTTP METHOD 方法定义
type IController ¶
type IController interface {
Init(interface{}, IRoot)
Mount(*gin.RouterGroup, string)
RegisterMethod(MethodType, HttpMethod, string, HandlerFunc)
Print(string)
}
IController 接口定义,启动时可以调用
type IDecorator ¶
type IDecorator interface {
GetDecorators() []HandlerDecorator
}
type IDeleteInit ¶
type IDeleteInit interface {
InitDelete(IResource)
}
type IPatchInit ¶
type IPatchInit interface {
InitPatch(IResource)
}
type IResource ¶
type IResource interface {
// Query 获取查询句柄,推荐优先用QueryWithContext/QueryPrimaryKey
Query() *gorm.DB
// QueryWithContext 获取已绑定context的查询句柄
QueryWithContext(*gin.Context) *gorm.DB
// QueryPrimaryKey 获取添加PrimaryKey条件的查询句柄
QueryPrimaryKey(*gin.Context) *gorm.DB
// GetDB 获取gorm DB实例
GetDB() *gorm.DB
// GetModel 获取资源的Model封装
GetModel() *model.Model
// GetSerializer 获取具体Model的序列化实例
GetSerializer(*model.Model) ISerializer
// GetPartialSerializer 获取具体Model的Partial序列化实例
GetPartialSerializer(*model.Model) ISerializer
// GetPrimaryKey 获取PrimaryKey
GetPrimaryKey(*gin.Context) interface{}
}
IResource 接口定义,运行时可以调用
func ResourceFromContext ¶
ResourceFromContext 从 ctx 里读取 Resource
type IRoot ¶
type IRoot interface {
RegisterResource(string, IController)
Mount(*gin.RouterGroup)
GetValidator() IValidator
Print(string)
Validate() *validator.Validate
}
type ISearchInit ¶
type ISearchInit interface {
InitSearch(IResource)
}
type ISerializer ¶
type ISerializer interface {
WithDefaults([]string) ISerializer
Parse(*gin.Context, []byte) error
ParseFromQuery(*gin.Context, map[string]string) error
ParseFromBody(*gin.Context) error
Validate(*gin.Context) *response.Error
ValidateData() map[string]interface{}
GetWithDefault(string, interface{}) interface{}
Get(string) (interface{}, error)
JsonData() map[string]interface{}
StructData() interface{}
}
type IValidator ¶
type MethodType ¶
type MethodType int32
MethodType 操作方法类型定义
const ( ListMethod MethodType = 1 DetailMethod MethodType = 2 )
Restful请求方法类型,针对列表和详情页有单独的处理
type ModelValidator ¶
type ModelValidator interface {
Validate(validator.StructLevel)
}
type ModelValidatorCtx ¶
type ModelValidatorCtx interface {
ValidateCtx(context.Context, validator.StructLevel)
}
type RequestBody ¶
RequestBody 请求body,为了支持修改专门设置
func NewRequestBody ¶
func NewRequestBody(req *http.Request) (*RequestBody, error)
NewRequestBody 创建RequestBody
func RequestBodyFromContext ¶
func RequestBodyFromContext(c *gin.Context) *RequestBody
RequestBodyFromContext 从 ctx 里读取 RequestBody
func (*RequestBody) Get ¶
func (rb *RequestBody) Get() []byte
func (*RequestBody) Set ¶
func (rb *RequestBody) Set(b []byte)
type Resource ¶
type Resource struct {
*Controller
// 必选
DB *gorm.DB
Model *model.Model
// contains filtered or unexported fields
}
Resource 定义 Restful Resource 结构体
func NewResource ¶
func (*Resource) GetPartialSerializer ¶
func (resource *Resource) GetPartialSerializer(m *model.Model) ISerializer
func (*Resource) GetPrimaryKey ¶
func (*Resource) GetSerializer ¶
func (resource *Resource) GetSerializer(m *model.Model) ISerializer
func (*Resource) QueryPrimaryKey ¶
type Serializer ¶
type Serializer struct {
// contains filtered or unexported fields
}
func NewSerializer ¶
func NewSerializer(m *model.Model, v IValidator, partial bool) *Serializer
NewSerializer Serializer 实例化,设置partial=true后,不会解析默认值,不会校验未传入的字段
func (*Serializer) Get ¶
func (s *Serializer) Get(key string) (interface{}, error)
Get 务必在 Validate 后调用,这里为了使用方便,未做错误检查 该函数的key是json/form的key key不存在时,会返回error
func (*Serializer) GetWithDefault ¶
func (s *Serializer) GetWithDefault(key string, value interface{}) interface{}
GetWithDefault 务必在 Validate 后调用,这里为了使用方便,未做错误检查 否则只能返回两个参数,不便于调用 该函数的key是json/form的key
func (*Serializer) JsonData ¶
func (s *Serializer) JsonData() map[string]interface{}
JsonData 务必在 Validate 后调用,这里为了使用方便,未做错误检查 否则只能返回两个参数,不便于调用 该函数返回的map,key为数据库列,不一定是json/form的key
func (*Serializer) Parse ¶
func (s *Serializer) Parse(c *gin.Context, b []byte) error
Parse 注意返回的是model的指针
func (*Serializer) ParseFromBody ¶
func (s *Serializer) ParseFromBody(c *gin.Context) error
ParseFromBody 从request解析,对Parse的封装
func (*Serializer) ParseFromQuery ¶
ParseFromQuery 注意返回的是model的指针
func (*Serializer) StructData ¶
func (s *Serializer) StructData() interface{}
StructData 务必在 Validate 后调用,这里为了使用方便,未做错误检查 该函数返回的类型与model相同,可以断言转回
func (*Serializer) Validate ¶
func (s *Serializer) Validate(c *gin.Context) *response.Error
Validate 根据struct tag校验输入数据
func (*Serializer) ValidateData ¶
func (s *Serializer) ValidateData() map[string]interface{}
ValidateData 务必在 Validate 后调用,这里为了使用方便,未做错误检查 否则只能返回两个参数,不便于调用 该函数返回的map,key为数据库列,不一定是json/form的key 注意,该方法应仅在需要存储数据到db时使用,参数解析务必使用 JsonData 方法
func (*Serializer) WithDefaults ¶
func (s *Serializer) WithDefaults(keys []string) ISerializer
WithDefaults 固定解析部分字段的默认值