@@ -8,6 +8,7 @@ package bsoncodec
8
8
9
9
import (
10
10
"fmt"
11
+ "reflect"
11
12
"strings"
12
13
13
14
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
@@ -45,36 +46,60 @@ type ValueUnmarshaler interface {
45
46
UnmarshalBSONValue (bsontype.Type , []byte ) error
46
47
}
47
48
48
- // ValueEncoderError is an error returned from a ValueEncoder when the provided
49
- // value can't be encoded by the ValueEncoder.
49
+ // ValueEncoderError is an error returned from a ValueEncoder when the provided value can't be
50
+ // encoded by the ValueEncoder.
50
51
type ValueEncoderError struct {
51
52
Name string
52
- Types []interface {}
53
- Received interface {}
53
+ Types []reflect.Type
54
+ Kinds []reflect.Kind
55
+ Received reflect.Value
54
56
}
55
57
56
58
func (vee ValueEncoderError ) Error () string {
57
- types := make ([]string , 0 , len (vee .Types ))
59
+ typeKinds := make ([]string , 0 , len (vee .Types ) + len ( vee . Kinds ))
58
60
for _ , t := range vee .Types {
59
- types = append (types , fmt . Sprintf ( "%T" , t ))
61
+ typeKinds = append (typeKinds , t . String ( ))
60
62
}
61
- return fmt .Sprintf ("%s can only process %s, but got a %T" , vee .Name , strings .Join (types , ", " ), vee .Received )
63
+ for _ , k := range vee .Kinds {
64
+ if k == reflect .Map {
65
+ typeKinds = append (typeKinds , "map[string]*" )
66
+ continue
67
+ }
68
+ typeKinds = append (typeKinds , k .String ())
69
+ }
70
+ received := vee .Received .Kind ().String ()
71
+ if vee .Received .IsValid () {
72
+ received = vee .Received .Type ().String ()
73
+ }
74
+ return fmt .Sprintf ("%s can only encode valid %s, but got %s" , vee .Name , strings .Join (typeKinds , ", " ), received )
62
75
}
63
76
64
- // ValueDecoderError is an error returned from a ValueDecoder when the provided
65
- // value can't be decoded by the ValueDecoder.
77
+ // ValueDecoderError is an error returned from a ValueDecoder when the provided value can't be
78
+ // decoded by the ValueDecoder.
66
79
type ValueDecoderError struct {
67
80
Name string
68
- Types []interface {}
69
- Received interface {}
81
+ Types []reflect.Type
82
+ Kinds []reflect.Kind
83
+ Received reflect.Value
70
84
}
71
85
72
86
func (vde ValueDecoderError ) Error () string {
73
- types := make ([]string , 0 , len (vde .Types ))
87
+ typeKinds := make ([]string , 0 , len (vde .Types ) + len ( vde . Kinds ))
74
88
for _ , t := range vde .Types {
75
- types = append (types , fmt .Sprintf ("%T" , t ))
89
+ typeKinds = append (typeKinds , t .String ())
90
+ }
91
+ for _ , k := range vde .Kinds {
92
+ if k == reflect .Map {
93
+ typeKinds = append (typeKinds , "map[string]*" )
94
+ continue
95
+ }
96
+ typeKinds = append (typeKinds , k .String ())
97
+ }
98
+ received := vde .Received .Kind ().String ()
99
+ if vde .Received .IsValid () {
100
+ received = vde .Received .Type ().String ()
76
101
}
77
- return fmt .Sprintf ("%s can only process %s, but got a %T " , vde .Name , strings .Join (types , ", " ), vde . Received )
102
+ return fmt .Sprintf ("%s can only decode valid and settable %s, but got %s " , vde .Name , strings .Join (typeKinds , ", " ), received )
78
103
}
79
104
80
105
// EncodeContext is the contextual information required for a Codec to encode a
@@ -98,36 +123,31 @@ type ValueCodec interface {
98
123
ValueDecoder
99
124
}
100
125
101
- // ValueEncoder is the interface implemented by types that can handle the
102
- // encoding of a value. Implementations must handle both values and
103
- // pointers to values.
126
+ // ValueEncoder is the interface implemented by types that can handle the encoding of a value.
104
127
type ValueEncoder interface {
105
- EncodeValue (EncodeContext , bsonrw.ValueWriter , interface {} ) error
128
+ EncodeValue (EncodeContext , bsonrw.ValueWriter , reflect. Value ) error
106
129
}
107
130
108
- // ValueEncoderFunc is an adapter function that allows a function with the
109
- // correct signature to be used as a ValueEncoder.
110
- type ValueEncoderFunc func (EncodeContext , bsonrw.ValueWriter , interface {} ) error
131
+ // ValueEncoderFunc is an adapter function that allows a function with the correct signature to be
132
+ // used as a ValueEncoder.
133
+ type ValueEncoderFunc func (EncodeContext , bsonrw.ValueWriter , reflect. Value ) error
111
134
112
135
// EncodeValue implements the ValueEncoder interface.
113
- func (fn ValueEncoderFunc ) EncodeValue (ec EncodeContext , vw bsonrw.ValueWriter , val interface {} ) error {
136
+ func (fn ValueEncoderFunc ) EncodeValue (ec EncodeContext , vw bsonrw.ValueWriter , val reflect. Value ) error {
114
137
return fn (ec , vw , val )
115
138
}
116
139
117
- // ValueDecoder is the interface implemented by types that can handle the
118
- // decoding of a value. Implementations must handle pointers to values,
119
- // including pointers to pointer values. The implementation may create a new
120
- // value and assign it to the pointer if necessary.
140
+ // ValueDecoder is the interface implemented by types that can handle the decoding of a value.
121
141
type ValueDecoder interface {
122
- DecodeValue (DecodeContext , bsonrw.ValueReader , interface {} ) error
142
+ DecodeValue (DecodeContext , bsonrw.ValueReader , reflect. Value ) error
123
143
}
124
144
125
- // ValueDecoderFunc is an adapter function that allows a function with the
126
- // correct signature to be used as a ValueDecoder.
127
- type ValueDecoderFunc func (DecodeContext , bsonrw.ValueReader , interface {} ) error
145
+ // ValueDecoderFunc is an adapter function that allows a function with the correct signature to be
146
+ // used as a ValueDecoder.
147
+ type ValueDecoderFunc func (DecodeContext , bsonrw.ValueReader , reflect. Value ) error
128
148
129
149
// DecodeValue implements the ValueDecoder interface.
130
- func (fn ValueDecoderFunc ) DecodeValue (dc DecodeContext , vr bsonrw.ValueReader , val interface {} ) error {
150
+ func (fn ValueDecoderFunc ) DecodeValue (dc DecodeContext , vr bsonrw.ValueReader , val reflect. Value ) error {
131
151
return fn (dc , vr , val )
132
152
}
133
153
0 commit comments