-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
3f818c3
commit 5462e87
Showing
7 changed files
with
113 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,33 @@ | ||
// Copyright 2014 Manu Martinez-Almeida. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package binding | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
type bsonBinding struct{} | ||
|
||
func (bsonBinding) Name() string { | ||
return "bson" | ||
} | ||
|
||
func (b bsonBinding) Bind(req *http.Request, obj any) error { | ||
buf, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return b.BindBody(buf, obj) | ||
} | ||
|
||
func (bsonBinding) BindBody(body []byte, obj any) error { | ||
if err := bson.Unmarshal(body, obj); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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,37 @@ | ||
// Copyright 2018 Gin Core Team. All rights reserved. | ||
// Use of this source code is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
|
||
package render | ||
|
||
import ( | ||
"net/http" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// BSONBuf contains the given interface object. | ||
type BSONBuf struct { | ||
Data any | ||
} | ||
|
||
var bsonContentType = []string{"application/bson"} | ||
|
||
// Render (BSON) marshals the given interface object and writes data with custom ContentType. | ||
func (r BSONBuf) Render(w http.ResponseWriter) error { | ||
r.WriteContentType(w) | ||
|
||
bytes, err := bson.Marshal(r.Data.(proto.Message)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = w.Write(bytes) | ||
return err | ||
} | ||
|
||
// WriteContentType (BSONBuf) writes BSONBuf ContentType. | ||
func (r BSONBuf) WriteContentType(w http.ResponseWriter) { | ||
writeContentType(w, bsonContentType) | ||
} |
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