Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,22 @@ func NewObjectIdWithTime(t time.Time) ObjectId {
return ObjectId(string(b[:]))
}

// objectIdStringer contains the function used in String() method of ObjectId type.
// It can be set by user through the exported SetIdStringer function.
var objectIdStringer = func(id ObjectId) string {
return fmt.Sprintf(`ObjectIdHex("%x")`, string(id)) // id default stringer
}

// SetIdStringer defines how the string representation of ObjectId type should be in its String() method.
func SetIdStringer(f func(ObjectId) string) {
objectIdStringer = f
}

// String returns a hex string representation of the id.
// Example: ObjectIdHex("4d88e15b60f486e428412dc9").
// The default representation looks like: ObjectIdHex("4d88e15b60f486e428412dc9"),
// which can be changed/defined through the function argument provided for SetIdStringer.
func (id ObjectId) String() string {
return fmt.Sprintf(`ObjectIdHex("%x")`, string(id))
return objectIdStringer(id)
}

// Hex returns a hex representation of the ObjectId.
Expand All @@ -279,7 +291,7 @@ var nullBytes = []byte("null")
func (id *ObjectId) UnmarshalJSON(data []byte) error {
if len(data) > 0 && (data[0] == '{' || data[0] == 'O') {
var v struct {
Id json.RawMessage `json:"$oid"`
Id json.RawMessage `json:"$oid"`
Func struct {
Id json.RawMessage
} `json:"$oidFunc"`
Expand Down