Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed May 21, 2024
1 parent 7a0b8bf commit 896be3d
Show file tree
Hide file tree
Showing 28 changed files with 199 additions and 112 deletions.
4 changes: 2 additions & 2 deletions bson/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ type Decoder struct {
vr ValueReader
}

// NewDecoder returns a new decoder that uses the DefaultRegistry to read from vr.
// NewDecoder returns a new decoder that uses the default registry to read from vr.
func NewDecoder(vr ValueReader) *Decoder {
return &Decoder{
reg: DefaultRegistry,
reg: NewRegistryBuilder().Build(),
vr: vr,
}
}
Expand Down
26 changes: 13 additions & 13 deletions bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,19 @@ func TestDecoderv2(t *testing.T) {
t.Errorf("Decoder should use the value reader provided. got %v; want %v", dec.vr, vr2)
}
})
t.Run("SetRegistry", func(t *testing.T) {
t.Parallel()

r1, r2 := DefaultRegistry, NewRegistryBuilder().Build()
dec := NewDecoder(NewValueReader([]byte{}))
if !reflect.DeepEqual(dec.reg, r1) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.reg, r1)
}
dec.SetRegistry(r2)
if !reflect.DeepEqual(dec.reg, r2) {
t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.reg, r2)
}
})
// t.Run("SetRegistry", func(t *testing.T) {
// t.Parallel()

// r1, r2 := DefaultRegistry, NewRegistryBuilder().Build()
// dec := NewDecoder(NewValueReader([]byte{}))
// if !reflect.DeepEqual(dec.reg, r1) {
// t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.reg, r1)
// }
// dec.SetRegistry(r2)
// if !reflect.DeepEqual(dec.reg, r2) {
// t.Errorf("Decoder should use the Registry provided. got %v; want %v", dec.reg, r2)
// }
// })
t.Run("DecodeToNil", func(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 7 additions & 0 deletions bson/default_value_decoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,9 @@ func decodeDefault(reg DecoderRegistry, vr ValueReader, val reflect.Value) ([]re
if err != nil {
return nil, newDecodeError(strconv.Itoa(idx), err)
}
if elem.Type() != eType {
elem = elem.Convert(eType)
}
elems = append(elems, elem)
idx++
}
Expand Down Expand Up @@ -1175,6 +1178,7 @@ func codeWithScopeDecodeValue(reg DecoderRegistry, vr ValueReader, val reflect.V
func decodeD(reg DecoderRegistry, vr ValueReader, val reflect.Value) ([]reflect.Value, error) {
switch vr.Type() {
case Type(0), TypeEmbeddedDocument:
break
default:
return nil, fmt.Errorf("cannot decode %v into a D", vr.Type())
}
Expand Down Expand Up @@ -1208,6 +1212,9 @@ func decodeElemsFromDocumentReader(reg DecoderRegistry, dr DocumentReader, t ref
if err != nil {
return nil, newDecodeError(key, err)
}
if val.Type() != t {
val = val.Convert(t)
}

elems = append(elems, reflect.ValueOf(E{Key: key, Value: val.Interface()}))
}
Expand Down
98 changes: 70 additions & 28 deletions bson/default_value_decoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,11 @@ func TestDefaultValueDecoders(t *testing.T) {
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)},
readInt32,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf(wrong),
},
},
Expand Down Expand Up @@ -214,44 +217,59 @@ func TestDefaultValueDecoders(t *testing.T) {
"int8/fast path - nil", (*int8)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*int8)(nil)),
},
},
{
"int16/fast path - nil", (*int16)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*int16)(nil)),
},
},
{
"int32/fast path - nil", (*int32)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*int32)(nil)),
},
},
{
"int64/fast path - nil", (*int64)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*int64)(nil)),
},
},
{
"int/fast path - nil", (*int)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*int)(nil)),
},
},
Expand Down Expand Up @@ -347,8 +365,11 @@ func TestDefaultValueDecoders(t *testing.T) {
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)},
nothing,
ValueDecoderError{
Name: "IntDecodeValue",
Kinds: []reflect.Kind{reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
},
},
{
Expand Down Expand Up @@ -380,8 +401,11 @@ func TestDefaultValueDecoders(t *testing.T) {
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)},
readInt32,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf(wrong),
},
},
Expand Down Expand Up @@ -446,44 +470,59 @@ func TestDefaultValueDecoders(t *testing.T) {
"uint8/fast path - nil", (*uint8)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*uint8)(nil)),
},
},
{
"uint16/fast path - nil", (*uint16)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*uint16)(nil)),
},
},
{
"uint32/fast path - nil", (*uint32)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*uint32)(nil)),
},
},
{
"uint64/fast path - nil", (*uint64)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*uint64)(nil)),
},
},
{
"uint/fast path - nil", (*uint)(nil), nil,
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)}, readInt32,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
Received: reflect.ValueOf((*uint)(nil)),
},
},
Expand Down Expand Up @@ -599,8 +638,11 @@ func TestDefaultValueDecoders(t *testing.T) {
&valueReaderWriter{BSONType: TypeInt32, Return: int32(0)},
nothing,
ValueDecoderError{
Name: "UintDecodeValue",
Kinds: []reflect.Kind{reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint},
Name: "IntDecodeValue",
Kinds: []reflect.Kind{
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
},
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions bson/empty_interface_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type emptyInterfaceCodec struct {
}

// EncodeValue is the ValueEncoderFunc for interface{}.
func (eic emptyInterfaceCodec) EncodeValue(reg EncoderRegistry, vw ValueWriter, val reflect.Value) error {
func (eic *emptyInterfaceCodec) EncodeValue(reg EncoderRegistry, vw ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != tEmpty {
return ValueEncoderError{Name: "EmptyInterfaceEncodeValue", Types: []reflect.Type{tEmpty}, Received: val}
}
Expand All @@ -40,7 +40,7 @@ func (eic emptyInterfaceCodec) EncodeValue(reg EncoderRegistry, vw ValueWriter,
return encoder.EncodeValue(reg, vw, val.Elem())
}

func (eic emptyInterfaceCodec) getEmptyInterfaceDecodeType(reg DecoderRegistry, valueType Type, ancestorType reflect.Type) (reflect.Type, error) {
func (eic *emptyInterfaceCodec) getEmptyInterfaceDecodeType(reg DecoderRegistry, valueType Type, ancestorType reflect.Type) (reflect.Type, error) {
isDocument := valueType == Type(0) || valueType == TypeEmbeddedDocument
if isDocument {
if eic.defaultDocumentType != nil {
Expand Down Expand Up @@ -81,12 +81,12 @@ func (eic emptyInterfaceCodec) getEmptyInterfaceDecodeType(reg DecoderRegistry,
return nil, err
}

func (eic emptyInterfaceCodec) decodeType(reg DecoderRegistry, vr ValueReader, t reflect.Type) (reflect.Value, error) {
func (eic *emptyInterfaceCodec) decodeType(reg DecoderRegistry, vr ValueReader, t reflect.Type) (reflect.Value, error) {
rtype, err := eic.getEmptyInterfaceDecodeType(reg, vr.Type(), t)
if err != nil {
switch vr.Type() {
case TypeNull:
return reflect.Zero(t), vr.ReadNull()
return reflect.Zero(tEmpty), vr.ReadNull()
default:
return emptyValue, err
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (eic emptyInterfaceCodec) decodeType(reg DecoderRegistry, vr ValueReader, t
}

// DecodeValue is the ValueDecoderFunc for interface{}.
func (eic emptyInterfaceCodec) DecodeValue(reg DecoderRegistry, vr ValueReader, val reflect.Value) error {
func (eic *emptyInterfaceCodec) DecodeValue(reg DecoderRegistry, vr ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tEmpty {
return ValueDecoderError{Name: "EmptyInterfaceDecodeValue", Types: []reflect.Type{tEmpty}, Received: val}
}
Expand Down
6 changes: 3 additions & 3 deletions bson/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ type Encoder struct {
vw ValueWriter
}

// NewEncoder returns a new encoder that uses the DefaultRegistry to write to vw.
// NewEncoder returns a new encoder that uses the default registry to write to vw.
func NewEncoder(vw ValueWriter) *Encoder {
return &Encoder{
reg: DefaultRegistry,
reg: NewRegistryBuilder().Build(),
vw: vw,
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (e *Encoder) IntMinSize() {
t := reflect.TypeOf((*intCodec)(nil))
if v, ok := e.reg.codecTypeMap[t]; ok && v != nil {
for i := range v {
v[i].(*intCodec).encodeToMinSize = true
v[i].(*intCodec).minSize = true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bson/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestBasicEncode(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
got := make(SliceWriter, 0, 1024)
vw := NewValueWriter(&got)
reg := DefaultRegistry
reg := NewRegistryBuilder().Build()
encoder, err := reg.LookupEncoder(reflect.TypeOf(tc.val))
noerr(t, err)
err = encoder.EncodeValue(reg, vw, reflect.ValueOf(tc.val))
Expand Down
Loading

0 comments on commit 896be3d

Please sign in to comment.