예제 #1
0
def _build_native_stub_function_definitions(murano_method):
    runtime_version = murano_method.declaring_type.package.runtime_version
    engine = choose_yaql_engine(runtime_version)

    @specs.method
    @specs.name(murano_method.name)
    @specs.meta(constants.META_MURANO_METHOD, murano_method)
    @specs.parameter('__receiver',
                     yaqltypes.NotOfType(dsl_types.MuranoTypeReference))
    def payload(__context, __receiver, *args, **kwargs):
        args = tuple(dsl.to_mutable(arg, engine) for arg in args)
        kwargs = dsl.to_mutable(kwargs, engine)
        return helpers.evaluate(
            murano_method.invoke(__receiver, args, kwargs, __context, True),
            __context)

    @specs.method
    @specs.name(murano_method.name)
    @specs.meta(constants.META_MURANO_METHOD, murano_method)
    @specs.parameter('__receiver',
                     yaqltypes.NotOfType(dsl_types.MuranoTypeReference))
    def extension_payload(__context, __receiver, *args, **kwargs):
        args = tuple(dsl.to_mutable(arg, engine) for arg in args)
        kwargs = dsl.to_mutable(kwargs, engine)
        return helpers.evaluate(
            murano_method.invoke(murano_method.declaring_type,
                                 (__receiver, ) + args, kwargs, __context,
                                 True), __context)

    @specs.method
    @specs.name(murano_method.name)
    @specs.meta(constants.META_MURANO_METHOD, murano_method)
    @specs.parameter('__receiver', dsl_types.MuranoTypeReference)
    def static_payload(__context, __receiver, *args, **kwargs):
        args = tuple(dsl.to_mutable(arg, engine) for arg in args)
        kwargs = dsl.to_mutable(kwargs, engine)
        return helpers.evaluate(
            murano_method.invoke(__receiver, args, kwargs, __context, True),
            __context)

    if murano_method.usage in dsl_types.MethodUsages.InstanceMethods:
        return specs.get_function_definition(payload), None
    elif murano_method.usage == dsl_types.MethodUsages.Static:
        return (specs.get_function_definition(payload),
                specs.get_function_definition(static_payload))
    elif murano_method.usage == dsl_types.MethodUsages.Extension:
        return (specs.get_function_definition(extension_payload),
                specs.get_function_definition(static_payload))
    else:
        raise ValueError('Unknown method usage ' + murano_method.usage)
예제 #2
0
    def test_chain(self):
        @specs.parameter(
            'arg', yaqltypes.Chain(yaqltypes.NotOfType(bool), int))
        def foo(arg):
            return True

        self.context.register_function(foo)
        self.assertTrue(self.eval('foo($)', data=123))
        self.assertRaises(
            exceptions.NoMatchingFunctionException,
            self.eval, 'foo($)', data=True)
        self.assertRaises(
            exceptions.NoMatchingFunctionException,
            self.eval, 'foo($)', data='abc')