forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(binding): add BindPlain (gin-gonic#3904)
* add BindPlain * fix ci/cd error
- Loading branch information
1 parent
40131af
commit 6ca8ddb
Showing
6 changed files
with
238 additions
and
1 deletion.
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
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