示例#1
0
    def _map_dict(self, data, spec, context, path):
        if data is None or data is dsl.NO_VALUE:
            data = {}
        if not isinstance(data, utils.MappingType):
            raise exceptions.ContractViolationException(
                'Value {0} is not of a dictionary type'.format(
                    helpers.format_scalar(data)))
        if not spec:
            return data
        result = {}
        yaql_key = None
        for key, value in six.iteritems(spec):
            if isinstance(key, dsl_types.YaqlExpression):
                if yaql_key is not None:
                    raise exceptions.DslContractSyntaxError(
                        'Dictionary contract '
                        'cannot have more than one expression key')
                else:
                    yaql_key = key
            else:
                result[key] = self._map(
                    data.get(key), value, context,
                    '{0}[{1}]'.format(path, helpers.format_scalar(key)))

        if yaql_key is not None:
            yaql_value = spec[yaql_key]
            for key, value in six.iteritems(data):
                if key in result:
                    continue
                key = self._map(key, yaql_key, context, path)
                result[key] = self._map(
                    value, yaql_value, context,
                    '{0}[{1}]'.format(path, helpers.format_scalar(key)))

        return utils.FrozenDict(result)
示例#2
0
    def _map_dict(self, data, spec, context):
        if data is None or data is dsl.NO_VALUE:
            data = {}
        if not isinstance(data, utils.MappingType):
            raise exceptions.ContractViolationException(
                'Supplied is not of a dictionary type')
        if not spec:
            return data
        result = {}
        yaql_key = None
        for key, value in spec.iteritems():
            if isinstance(key, dsl_types.YaqlExpression):
                if yaql_key is not None:
                    raise exceptions.DslContractSyntaxError(
                        'Dictionary contract '
                        'cannot have more than one expression keys')
                else:
                    yaql_key = key
            else:
                result[key] = self._map(data.get(key), value, context)

        if yaql_key is not None:
            yaql_value = spec[yaql_key]
            for key, value in data.iteritems():
                if key in result:
                    continue
                result[self._map(key, yaql_key,
                                 context)] = self._map(value, yaql_value,
                                                       context)

        return utils.FrozenDict(result)