Skip to content

Commit

Permalink
Merge pull request #13 from nano-interactive/feature/unmrashall
Browse files Browse the repository at this point in the history
  • Loading branch information
Dusan Malusev authored Oct 20, 2023
2 parents c70b088 + a43847b commit 38d0a2e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"reflect"
"time"

Expand Down Expand Up @@ -78,6 +79,7 @@ func (o *NullBool) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error {
switch t {
case bson.TypeNull:
o.Valid = false
o.Bool = false
default:
var b bool
if err := bson.UnmarshalValue(t, bytes, &b); err != nil {
Expand All @@ -89,6 +91,26 @@ func (o *NullBool) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error {

return nil
}
func (o *NullBool) UnmarshalJSON(data []byte) error {
if len(data) == 0 || bytes.Compare(jsonNullBytes, data) == 0 {
o.Valid = false
o.Bool = false
return nil
}

switch utils.UnsafeString(data) {
case "true":
o.Valid = true
o.Bool = true
case "false":
o.Valid = true
o.Bool = false
default:
return errors.New("invalid bool data")
}

return nil
}
func (o NullBool) MarshalBSONValue() (bsontype.Type, []byte, error) {
if !o.Valid {
return bson.TypeNull, nil, nil
Expand Down

0 comments on commit 38d0a2e

Please sign in to comment.