自定义协议处理
本插件生成的入参和出参处理定义在 niuhe 库中, 具体代码为:
type IApiProtocol interface {
Read(*Context, reflect.Value) error
Write(*Context, reflect.Value, error) error
}
type DefaultApiProtocol struct{}
func (self DefaultApiProtocol) Read(c *Context, reqValue reflect.Value) error {
if err := zpform.ReadReflectedStructForm(c.Request, reqValue); err != nil {
return NewCommError(-1, err.Error())
}
return nil
}
func (self DefaultApiProtocol) Write(c *Context, rsp reflect.Value, err error) error {
if c._ignoreResult {
return nil
}
rspInst := rsp.Interface()
if _, ok := rspInst.(isCustomRoot); ok {
c.JSON(200, rspInst)
} else {
var response map[string]interface{}
if err != nil {
if commErr, ok := err.(ICommError); ok {
response = map[string]interface{}{
"result": commErr.GetCode(),
"message": commErr.GetMessage(),
}
if commErr.GetCode() == 0 {
response["data"] = rsp.Interface()
}
} else {
response = map[string]interface{}{
"result": -1,
"message": err.Error(),
}
}
} else {
response = map[string]interface{}{
"result": 0,
"data": rspInst,
}
}
c.JSON(200, response)
}
return nil
}
如果需要自行实现为其他协议, 可以参考上述代码进行自定义实现。