import re


def _parse_param(key, val):
    """ Parse the query param looking for sparse fields params

    Ensure the `val` or what will become the sparse fields
    is always an array. If the query param is not a sparse
    fields query param then return None.

    :param key:
        the query parameter key in the request (left of =)
    :param val:
        the query parameter val in the request (right of =)
    :return:
        tuple of resource type to implement the sparse
        fields on & a array of the fields.
    """

    regex = re.compile(r'fields\[([A-Za-z]+)\]')
    match = regex.match(key)

    if match:
        if not isinstance(val, list):
            val = val.split(',')

        fields = [field.lower() for field in val]
        rtype = match.groups()[0].lower()

        return rtype, fields