Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-2890

Abstract en(de)coder assertion code in bsoncodec

    • Type: Icon: Spec Change Spec Change
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      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()
      }

            Assignee:
            Unassigned Unassigned
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: