Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than cf32d2d point at it.

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 1–17why, one step back
1package binding
23import (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}17

refactor: replace interface{} with any in type declarations (#4249)Flc゛before adoption848e1cd

line 18why, one step back
18func (plainBinding) Bind(req *http.Request, obj any) error {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 19–56why, one step back
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}