Abstract en(de)coder assertion code in bsoncodec

XMLWordPrintableJSON

    • Type: Spec Change
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • 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
              Reporter:
              Preston Vasquez
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated: