[GODRIVER-2890] Abstract en(de)coder assertion code in bsoncodec Created: 05/Jul/23  Updated: 10/Jul/23

Status: Backlog
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Spec Change Priority: Unknown
Reporter: Preston Vasquez Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to GODRIVER-2887 bson: remove use of reflect.Value.Met... Closed

 Description   

There are multiple references to type asserting data in the bsoncodec package. For example:

m, ok := val.Interface().(ValueUnmarshaler)
if !ok {
    // NB: this error should be unreachable due to the above checks
    return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val}
}

and

m, ok := val.Interface().(Unmarshaler)
if !ok {
    // NB: this error should be unreachable due to the above checks
    return ValueDecoderError{Name: "UnmarshalerDecodeValue", Types: []reflect.Type{tUnmarshaler}, Received: val}
}

It would be helpful for readability and reuse to create a single function that handles all of this logic, something like the following:

switch v := val.Interface().(type) {
case ValueUnmarshaler:
    // Handle ValueUnmarshaler case
    return ValueDecoderError{Name: "ValueUnmarshalerDecodeValue", Types: []reflect.Type{tValueUnmarshaler}, Received: val}
case Unmarshaler:
    // Handle Unmarshaler case
    return ValueDecoderError{Name: "UnmarshalerDecodeValue", Types: []reflect.Type{tUnmarshaler}, Received: val}
default:
    // Handle the case when neither interface is implemented
    return vw.WriteNull()
}


Generated at Thu Feb 08 08:39:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.