Exemplo n.º 1
0
 def check_type(self, data, context, default, calling_type):
     if helpers.is_passkey(data):
         return False
     try:
         self._execute(self._prepare_check_type_context,
                       data,
                       context,
                       default,
                       calling_type=calling_type)
         return True
     except exceptions.ContractViolationException:
         return False
Exemplo n.º 2
0
 def _map(self, data, spec, context, path):
     if helpers.is_passkey(data):
         return data
     child_context = context.create_child_context()
     if isinstance(spec, dsl_types.YaqlExpression):
         child_context[''] = data
         try:
             result = spec(context=child_context)
             return result
         except exceptions.ContractViolationException as e:
             e.path = path
             raise
     elif isinstance(spec, utils.MappingType):
         return self._map_dict(data, spec, child_context, path)
     elif utils.is_sequence(spec):
         return self._map_list(data, spec, child_context, path)
     else:
         return self._map_scalar(data, spec)
Exemplo n.º 3
0
    def _execute(self, base_context_func, data, context, default, **kwargs):
        # TODO(ativelkov, slagun): temporary fix, need a better way of handling
        # composite defaults
        # A bug (#1313694) has been filed

        if data is dsl.NO_VALUE:
            data = helpers.evaluate(default, context)

        if helpers.is_passkey(data):
            return data

        contract_context = base_context_func(
            self._runtime_version).create_child_context()
        contract_context['root_context'] = context
        for key, value in six.iteritems(kwargs):
            contract_context[key] = value
        contract_context[constants.CTX_NAMES_SCOPE] = \
            context[constants.CTX_NAMES_SCOPE]
        return self._map(data, self._spec, contract_context, '')
Exemplo n.º 4
0
def _pass12_serialize(value, parent, serialized_objects,
                      designer_attributes_getter, executor, serialize_actions,
                      serialization_type, allow_refs,
                      with_destruction_dependencies):
    if isinstance(value, dsl.MuranoObjectInterface):
        value = value.object
    if isinstance(value,
                  (six.string_types, int, float, bool)) or value is None:
        return value, False
    if isinstance(value, dsl_types.MuranoObject):
        if value.owner is not parent or value.object_id in serialized_objects:
            return ObjRef(value), True
    elif isinstance(value, ObjRef):
        can_move = value.ref_obj.object_id not in serialized_objects
        if can_move and allow_refs and value.ref_obj.owner is not None:
            can_move = (is_nested_in(parent, value.ref_obj.owner) and
                        value.ref_obj.owner.object_id in serialized_objects)
        if can_move:
            value = value.ref_obj
        else:
            return value, False
    if isinstance(value,
                  (dsl_types.MuranoType, dsl_types.MuranoTypeReference)):
        return helpers.format_type_string(value), False
    if helpers.is_passkey(value):
        return value, False
    if isinstance(value, dsl_types.MuranoObject):
        result = value.to_dictionary(
            serialization_type=serialization_type,
            allow_refs=allow_refs,
            with_destruction_dependencies=with_destruction_dependencies)
        if designer_attributes_getter is not None:
            if serialization_type == dsl_types.DumpTypes.Inline:
                system_data = result
            else:
                system_data = result['?']
            system_data.update(designer_attributes_getter(value.object_id))
            if serialize_actions:
                # deserialize and merge list of actions
                system_data['_actions'] = _serialize_available_action(
                    value, system_data.get('_actions', {}), executor)
        serialized_objects.add(value.object_id)
        return _pass12_serialize(result, value, serialized_objects,
                                 designer_attributes_getter, executor,
                                 serialize_actions, serialization_type,
                                 allow_refs, with_destruction_dependencies)
    elif isinstance(value, utils.MappingType):
        result = {}
        need_another_pass = False

        for d_key, d_value in value.items():
            if (isinstance(d_key, dsl_types.MuranoType) and serialization_type
                    == dsl_types.DumpTypes.Serializable):
                result_key = str(d_key)
            else:
                result_key = d_key
            if (result_key == 'type'
                    and isinstance(d_value, dsl_types.MuranoType)
                    and serialization_type == dsl_types.DumpTypes.Mixed):
                result_value = d_value, False
            else:
                result_value = _pass12_serialize(
                    d_value, parent, serialized_objects,
                    designer_attributes_getter, executor, serialize_actions,
                    serialization_type, allow_refs,
                    with_destruction_dependencies)
            result[result_key] = result_value[0]
            if result_value[1]:
                need_another_pass = True
        return result, need_another_pass
    elif utils.is_sequence(value) or isinstance(value, utils.SetType):
        need_another_pass = False
        result = []
        for t in value:
            v, nmp = _pass12_serialize(t, parent, serialized_objects,
                                       designer_attributes_getter, executor,
                                       serialize_actions, serialization_type,
                                       allow_refs,
                                       with_destruction_dependencies)
            if nmp:
                need_another_pass = True
            result.append(v)
        return result, need_another_pass
    else:
        raise ValueError()