Skip to content

Commit

Permalink
Add custom JSON unmarshaller, remove old function
Browse files Browse the repository at this point in the history
  • Loading branch information
jpahm committed Nov 18, 2023
1 parent f1b8c6b commit 3edd281
Showing 1 changed file with 12 additions and 27 deletions.
39 changes: 12 additions & 27 deletions api/schema/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package schema

import (
"encoding/json"
"reflect"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
)

// Wrapper type for primitive.ObjectID to allow for custom mashalling below
Expand All @@ -23,28 +18,18 @@ func (id IdWrapper) MarshalJSON() (data []byte, err error) {
return json.Marshal(tmp{string(id)})
}

func CreateCustomRegistry() *bsoncodec.RegistryBuilder {
var primitiveCodecs bson.PrimitiveCodecs
rb := bsoncodec.NewRegistryBuilder()
bsoncodec.DefaultValueEncoders{}.RegisterDefaultEncoders(rb)
bsoncodec.DefaultValueDecoders{}.RegisterDefaultDecoders(rb)
// register our new type
idWrapperType := reflect.TypeOf(IdWrapper(""))
// read the datetime type and convert to integer
rb.RegisterTypeDecoder(
idWrapperType,
bsoncodec.ValueDecoderFunc(func(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
// this is the function when we read the datetime format
read, err := vr.ReadObjectID()
if err != nil {
return err
}
val.SetString(read.Hex())
return nil
}),
)
primitiveCodecs.RegisterPrimitiveCodecs(rb)
return rb
// Custom JSON unmarshalling for ObjectID to unmarshal ObjectIDs correctly
func (id *IdWrapper) UnmarshalJSON(data []byte) (e error) {

type tmp struct {
Id string `json:"$oid"`
}

var t tmp

e = json.Unmarshal(data, &t)
*id = IdWrapper(t.Id)
return e
}

type Course struct {
Expand Down

0 comments on commit 3edd281

Please sign in to comment.