-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a865dc
commit e5e38ad
Showing
5 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package binding | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"reflect" | ||
|
||
"github.com/gin-gonic/gin/internal/bytesconv" | ||
) | ||
|
||
type plainBinding struct{} | ||
|
||
func (plainBinding) Name() string { | ||
return "plain" | ||
} | ||
|
||
func (plainBinding) Bind(req *http.Request, obj interface{}) error { | ||
all, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return decodePlain(all, obj) | ||
} | ||
|
||
func (plainBinding) BindBody(body []byte, obj any) error { | ||
return decodePlain(body, obj) | ||
} | ||
|
||
func decodePlain(data []byte, obj any) error { | ||
if obj == nil { | ||
return nil | ||
} | ||
|
||
v := reflect.ValueOf(obj) | ||
|
||
for v.Kind() == reflect.Ptr { | ||
if v.IsNil() { | ||
return nil | ||
} | ||
v = v.Elem() | ||
} | ||
|
||
if v.Kind() == reflect.String { | ||
v.SetString(bytesconv.BytesToString(data)) | ||
return nil | ||
} | ||
|
||
if _, ok := v.Interface().([]byte); ok { | ||
v.SetBytes(data) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("type (%T) unknown type", v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters