Merge "Operations on reflected entities"
This commit is contained in:
@@ -16,7 +16,9 @@ import semantic_version
|
||||
from yaql.language import specs
|
||||
from yaql import yaqlization
|
||||
|
||||
from murano.dsl import dsl
|
||||
from murano.dsl import dsl_types
|
||||
from murano.dsl import helpers
|
||||
|
||||
|
||||
@specs.yaql_property(dsl_types.MuranoClass)
|
||||
@@ -94,6 +96,31 @@ def property_owner(murano_property):
|
||||
return murano_property.murano_class
|
||||
|
||||
|
||||
@specs.name('get_value')
|
||||
@specs.parameter('property_', dsl_types.MuranoProperty)
|
||||
@specs.parameter('object_', dsl.MuranoObjectParameterType(nullable=True))
|
||||
@specs.method
|
||||
def property_get_value(context, property_, object_):
|
||||
if object_ is None:
|
||||
return property_.murano_class.get_property(
|
||||
name=property_.name, context=context)
|
||||
return object_.cast(property_.murano_class).get_property(
|
||||
name=property_.name, context=context)
|
||||
|
||||
|
||||
@specs.name('set_value')
|
||||
@specs.parameter('property_', dsl_types.MuranoProperty)
|
||||
@specs.parameter('object_', dsl.MuranoObjectParameterType(nullable=True))
|
||||
@specs.method
|
||||
def property_set_value(context, property_, object_, value):
|
||||
if object_ is None:
|
||||
property_.murano_class.set_property(
|
||||
name=property_.name, value=value, context=context)
|
||||
else:
|
||||
object_.cast(property_.murano_class).set_property(
|
||||
name=property_.name, value=value, context=context)
|
||||
|
||||
|
||||
@specs.yaql_property(dsl_types.MuranoMethod)
|
||||
@specs.name('name')
|
||||
def method_name(murano_method):
|
||||
@@ -113,6 +140,15 @@ def method_owner(murano_method):
|
||||
return murano_method.murano_class
|
||||
|
||||
|
||||
@specs.parameter('method', dsl_types.MuranoMethod)
|
||||
@specs.parameter('__object', dsl.MuranoObjectParameterType(nullable=True))
|
||||
@specs.name('invoke')
|
||||
@specs.method
|
||||
def method_invoke(context, method, __object, *args, **kwargs):
|
||||
executor = helpers.get_executor(context)
|
||||
return method.invoke(executor, __object, args, kwargs, context)
|
||||
|
||||
|
||||
@specs.yaql_property(dsl_types.MuranoPackage)
|
||||
def types(murano_package):
|
||||
return tuple(
|
||||
@@ -167,8 +203,8 @@ def register(context):
|
||||
funcs = (
|
||||
class_name, methods, properties, ancestors, package, class_version,
|
||||
property_name, property_has_default, property_owner,
|
||||
property_usage,
|
||||
method_name, arguments, method_owner,
|
||||
property_usage, property_get_value, property_set_value,
|
||||
method_name, arguments, method_owner, method_invoke,
|
||||
types, package_name, package_version,
|
||||
argument_name, argument_has_default, argument_owner,
|
||||
type_to_type_ref
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
Name: TestReflection
|
||||
|
||||
Properties:
|
||||
prop:
|
||||
property:
|
||||
Contract: $.string()
|
||||
Default: qwerty
|
||||
Default: object
|
||||
Usage: InOut
|
||||
|
||||
staticProperty:
|
||||
Contract: $.string()
|
||||
Default: static
|
||||
Usage: Static
|
||||
|
||||
Methods:
|
||||
testTypeInfo:
|
||||
@@ -16,8 +22,8 @@ Methods:
|
||||
versionMinor: $typeinfo.version.minor
|
||||
versionPatch: $typeinfo.version.patch
|
||||
ancestors: $typeinfo.ancestors.name
|
||||
properties: $typeinfo.properties.name
|
||||
methods: $typeinfo.methods.name.orderBy($)
|
||||
properties: $typeinfo.properties.name.orderBy($)
|
||||
methods: $typeinfo.methods.name.where(not $.startsWith(test)).orderBy($)
|
||||
package: $typeinfo.package.name
|
||||
|
||||
testMethodInfo:
|
||||
@@ -36,7 +42,7 @@ Methods:
|
||||
|
||||
testPropertyInfo:
|
||||
Body:
|
||||
- $property: typeinfo($).properties.first()
|
||||
- $property: typeinfo($).properties.orderBy($.name).first()
|
||||
- Return:
|
||||
name: $property.name
|
||||
hasDefault: $property.hasDefault
|
||||
@@ -49,3 +55,31 @@ Methods:
|
||||
Default: null
|
||||
- baz:
|
||||
Contract: $.string()
|
||||
Body:
|
||||
Return: $bar + $baz
|
||||
|
||||
testPropertyRead:
|
||||
Body:
|
||||
- $instanceValues: typeinfo($).properties.orderBy($.name).
|
||||
select($.getValue($this))
|
||||
- $staticValues: typeinfo($).properties.orderBy($.name).
|
||||
where($.usage = Static).select($.getValue(null))
|
||||
- Return: [$instanceValues, $staticValues]
|
||||
|
||||
testPropertyWrite:
|
||||
Body:
|
||||
- $instanceProperty: typeinfo($).properties.where($.name = property).single()
|
||||
- $instanceProperty.setValue($, 'new object')
|
||||
- $staticProperty: typeinfo($).properties.where($.name = staticProperty).single()
|
||||
- $staticProperty.setValue($, 'new static')
|
||||
- Return: $.testPropertyRead()
|
||||
|
||||
testMethodInvoke:
|
||||
Body:
|
||||
- $method: typeinfo($).methods.where($.name = foo).single()
|
||||
- Return: $method.invoke($, 'bar ', baz => baz)
|
||||
|
||||
testInstanceCreate:
|
||||
Body:
|
||||
- $obj: new(typeinfo($).type, property => test)
|
||||
- Return: $obj.property
|
||||
|
||||
@@ -30,12 +30,9 @@ class TestReflection(test_case.DslTestCase):
|
||||
'versionMinor': 0,
|
||||
'versionPatch': 0,
|
||||
'ancestors': ['io.murano.Object'],
|
||||
'properties': ['prop'],
|
||||
'properties': ['property', 'staticProperty'],
|
||||
'package': 'tests',
|
||||
'methods': [
|
||||
'foo', 'getAttr', 'setAttr',
|
||||
'testMethodInfo', 'testPropertyInfo', 'testTypeInfo'
|
||||
]
|
||||
'methods': ['foo', 'getAttr', 'setAttr']
|
||||
},
|
||||
self._runner.testTypeInfo())
|
||||
|
||||
@@ -55,8 +52,24 @@ class TestReflection(test_case.DslTestCase):
|
||||
def test_property_info(self):
|
||||
self.assertEqual(
|
||||
{
|
||||
'name': 'prop',
|
||||
'name': 'property',
|
||||
'hasDefault': True,
|
||||
'usage': 'In'
|
||||
'usage': 'InOut'
|
||||
},
|
||||
self._runner.testPropertyInfo())
|
||||
|
||||
def test_property_read(self):
|
||||
self.assertEqual(
|
||||
[['object', 'static'], ['static']],
|
||||
self._runner.testPropertyRead())
|
||||
|
||||
def test_property_write(self):
|
||||
self.assertEqual(
|
||||
[['new object', 'new static'], ['new static']],
|
||||
self._runner.testPropertyWrite())
|
||||
|
||||
def test_method_invoke(self):
|
||||
self.assertEqual('bar baz', self._runner.testMethodInvoke())
|
||||
|
||||
def test_instance_create(self):
|
||||
self.assertEqual('test', self._runner.testInstanceCreate())
|
||||
|
||||
@@ -3,4 +3,5 @@ features:
|
||||
- Basic reflection capabilities were added to MuranoPL. Now it is possible
|
||||
to get type info with typeinfo() function and using it as a starting point
|
||||
obtain information about the class, its methods and properties as well as
|
||||
the package of the class.
|
||||
the package of the class. Reflected properties can be used to obtain or
|
||||
set its value in a given object or invoke its method.
|
||||
Reference in New Issue
Block a user