import yaml
from typing import OrderedDict


def _ordered_dump(data, stream=None, dumper_klass=yaml.Dumper, **kwargs):
    """Helper function for ordered dumping of structs as YAML"""

    class OrderedDumper(dumper_klass):
        """Helper class"""

    def _dict_representer(dumper, data):
        return dumper.represent_mapping(
            yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items()
        )

    OrderedDumper.add_representer(OrderedDict, _dict_representer)
    return yaml.dump(data, stream, OrderedDumper, **kwargs)