Skip to content

Latest commit

 

History

History
321 lines (185 loc) · 12 KB

gin_to_hertz.md

File metadata and controls

321 lines (185 loc) · 12 KB

Gin -> Hertz conversion table

  • All the pseudocode below assumes ctx have these types:
func handler(ctx *gin.Context){..}
->
func handler(
c context.Context
ctx *app.RequestContext
){..}

Need Change Function

// gin: 
for key := range c.Request.Header {
        value := c.Request.Header.Get(key)
        c.Set(key, value)
}
// hertz: 
c.Request.Header.VisitAll(
        func(k, v []byte) {
                key := string(k)
                value := string(v)
                c.Set(key, value)
})
  • request query args iteration
// gin:
for k, v := range c.Request.URL.Query() {
   if len(v) > 0 {
      req.data[k] = v[0]
   }
}
// hertz: 
c.Request.URI().QueryArgs().VisitAll(func(k, v []byte) {
   strK := string(k)
   if _, ok := req.data[strK]; !ok {
      req.data[strK] = string(v)
   }
})
  • request post args iteration
// gin: 
if c.Request.ParseForm() != nil {
   for k, v := range c.Request.PostForm {
      if len(v) > 0 {
         req.data[k] = v[0]
      }
   }
}

// hertz:
c.Request.PostArgs().VisitAll(func(k, v []byte) {
   strK := string(k)
   if _, ok := req.data[strK]; !ok {
      req.data[strK] = string(v)
   }
})

Unimplemented function

  • ctx.AddParam

  • ctx.AsciiJSON

  • ctx.BindHeader

  • ctx.BindJSON

  • ctx.BindQuery

  • ctx.BindTOML

  • ctx.BindUri

  • ctx.BindWith

  • ctx.BindXML

  • ctx.BindYAML

  • ctx.DataFromReader

  • ctx.GetPostFormArray

  • ctx.GetPostFormMap

  • ctx.GetQueryArray

  • ctx.GetQueryMap

  • ctx.IsWebsocket

  • ctx.JSONP

  • ctx.MustBindWith

  • ctx.Negotiate

  • ctx.NegotiateFormat

  • ctx.PostFormArray

  • ctx.PostFormMap

  • ctx.QueryArray

  • ctx.QueryMap

  • ctx.SSEvent

  • ctx.SecureJSON

  • ctx.SetAccepted

  • ctx.ShouldBind

  • ctx.ShouldBindBodyWith

  • ctx.ShouldBindHeader

  • ctx.ShouldBindJSON

  • ctx.ShouldBindQuery

  • ctx.ShouldBindTOML

  • ctx.ShouldBindUri

  • ctx.ShouldBindWith

  • ctx.ShouldBindXML

  • ctx.ShouldBindYAML

  • ctx.TOML

  • ctx.YAML

No Changed Function

context.Context Interface

  • ctx.Deadline -> c.Deadline

  • ctx.Done -> c.Done

  • ctx.Err -> c.Err

  • ctx.Value -> c.Value