import inspect


def convert_to_dict(val, args, kwargs):
    """
    Use the val's as_dict method if it exists and return the result from that or
    return val as is.

    We also see if as_dict takes in arguments and if it does, we pass in args
    and kwargs to the as_dict.
    """
    if not hasattr(val, "as_dict"):
        return val

    if hasattr(val, "is_dict") and not val.is_dict:
        return val

    if inspect.signature(val.as_dict).parameters:
        return val.as_dict(*args, **kwargs)
    else:
        return val.as_dict()