goext/mongoext/registry.go

48 lines
2.0 KiB
Go
Raw Normal View History

2023-05-05 17:47:30 +02:00
package mongoext
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
2023-10-09 15:17:22 +02:00
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
2023-11-12 03:10:55 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2023-05-05 17:47:30 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"reflect"
)
func CreateGoExtBsonRegistry() *bsoncodec.Registry {
rb := bsoncodec.NewRegistryBuilder()
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.RFC3339Time{}), rfctime.RFC3339Time{})
2023-05-05 18:05:58 +02:00
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.RFC3339Time{}), rfctime.RFC3339Time{})
2023-05-05 18:00:25 +02:00
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.RFC3339NanoTime{}), rfctime.RFC3339NanoTime{})
2023-05-05 18:05:58 +02:00
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.RFC3339NanoTime{}), rfctime.RFC3339NanoTime{})
2023-05-05 17:47:30 +02:00
2023-11-10 13:37:55 +01:00
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.UnixTime{}), rfctime.UnixTime{})
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.UnixTime{}), rfctime.UnixTime{})
2023-11-10 13:26:30 +01:00
2023-11-10 13:37:55 +01:00
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.UnixMilliTime{}), rfctime.UnixMilliTime{})
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.UnixMilliTime{}), rfctime.UnixMilliTime{})
2023-11-10 13:26:30 +01:00
2023-11-10 13:37:55 +01:00
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.UnixNanoTime{}), rfctime.UnixNanoTime{})
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.UnixNanoTime{}), rfctime.UnixNanoTime{})
2023-11-10 13:26:30 +01:00
2023-06-10 19:13:15 +02:00
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.Date{}), rfctime.Date{})
rb.RegisterTypeDecoder(reflect.TypeOf(&rfctime.Date{}), rfctime.Date{})
2023-11-12 03:10:55 +01:00
rb.RegisterTypeDecoder(reflect.TypeOf(rfctime.SecondsF64(0)), rfctime.SecondsF64(0))
rb.RegisterTypeDecoder(reflect.TypeOf(langext.Ptr(rfctime.SecondsF64(0))), rfctime.SecondsF64(0))
2023-05-05 17:57:21 +02:00
bsoncodec.DefaultValueEncoders{}.RegisterDefaultEncoders(rb)
bsoncodec.DefaultValueDecoders{}.RegisterDefaultDecoders(rb)
2023-05-05 17:47:30 +02:00
2023-05-05 17:57:21 +02:00
bson.PrimitiveCodecs{}.RegisterPrimitiveCodecs(rb)
2023-05-05 17:47:30 +02:00
2023-10-09 15:25:30 +02:00
// otherwise we get []primitve.E when unmarshalling into any
// which will result in {'key': .., 'value': ...}[] json when json-marshalling
rb.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(primitive.M{}))
2023-05-05 17:57:21 +02:00
return rb.Build()
2023-05-05 17:47:30 +02:00
}