import six


def match_type(s, t):
    """
    Match string type

    For example::

        >>> assert type(match_type(b'hello', six.binary_type)) is six.binary_type
        >>> assert type(match_type(u'hello', six.text_type)) is six.text_type
        >>> assert type(match_type(b'hello', six.text_type)) is six.text_type
        >>> assert type(match_type(u'hello', six.binary_type)) is six.binary_type
    """
    if isinstance(s, t):
        return s
    if t is six.text_type:
        return s.decode("utf-8")
    else:
        return s.encode("utf-8")