go-jarvis / rum-gonic

a gin based RESTful web framework, inspired by cobra
MIT License
0 stars 0 forks source link

根据 output interface{} 的类型选择不同的 c.XXX 返回结果 #1

Closed tangx closed 2 years ago

tangx commented 2 years ago
  1. 如果 v.Type()= string, 则使用 c.String() , 否则使用用 c.Json()

  2. 如果 err!=nil 优先返回 error

tangx commented 2 years ago

// output give response code and data
// content-type is text/plain if data is string type, or content-type
// is application/json by default. maybe it will support more
// content types in feture.
func (r *RouterGroup) output(c *gin.Context, data interface{}, err error) {
    code, data := extract(data, err)

    switch ret := data.(type) {
    case string:
        c.String(code, ret)
    default:
        c.JSON(code, ret)
    }
}