plain.go
This file
Path
binding/plain.go
56 lines · 868 bytes · Go
Last changed by
refactor: replace interface{} with any in type declarations (#4249)Flc゛before adoption848e1cd
History of this fileTake it
1package binding23import (4 "fmt"5 "io"6 "net/http"7 "reflect"89 "github.com/gin-gonic/gin/internal/bytesconv"10)1112type plainBinding struct{}1314func (plainBinding) Name() string {15 return "plain"16}1718func (plainBinding) Bind(req *http.Request, obj any) error {19 all, err := io.ReadAll(req.Body)20 if err != nil {21 return err22 }2324 return decodePlain(all, obj)25}2627func (plainBinding) BindBody(body []byte, obj any) error {28 return decodePlain(body, obj)29}3031func decodePlain(data []byte, obj any) error {32 if obj == nil {33 return nil34 }3536 v := reflect.ValueOf(obj)3738 for v.Kind() == reflect.Ptr {39 if v.IsNil() {40 return nil41 }42 v = v.Elem()43 }4445 if v.Kind() == reflect.String {46 v.SetString(bytesconv.BytesToString(data))47 return nil48 }4950 if _, ok := v.Interface().([]byte); ok {51 v.SetBytes(data)52 return nil53 }5455 return fmt.Errorf("type (%T) unknown type", v)56}