in packages/@jsii/python-runtime/src/jsii/_reference_map.py [0:0]
def resolve(self, kernel, ref):
# First we need to check our reference map to see if we have any instance that
# already matches this reference.
try:
# TODO: Handle discovery of possible new interfaces on the ObjRef
return self._refs[ref.ref]
except KeyError:
pass
# If we got to this point, then we didn't have a referene for this, in that case
# we want to create a new instance, but we need to create it in such a way that
# we don't try to recreate the type inside of the JSII interface.
class_fqn = ref.ref.rsplit("@", 1)[0]
if class_fqn in _types:
klass = _types[class_fqn]
# If this class is an abstract class, then we'll use the generated proxy
# class instead of the abstract class to handle return values for this type.
if inspect.isabstract(klass):
klass = klass.__jsii_proxy_class__()
# Create our instance, bypassing __init__ by directly calling __new__, and
# then assign our reference to __jsii_ref__
inst = klass.__new__(klass)
inst.__jsii_ref__ = ref
if ref.interfaces is not None:
return InterfaceDynamicProxy(
[inst] + self.build_interface_proxies_for_ref(ref)
)
else:
return inst
# Legacy code path - Kernel invariant ought to guarantee that class_fqn can't be Struct (they're interfaces)
elif class_fqn in _data_types:
# Data types have been serialized by-reference (see aws/jsii#400).
# We retrieve all of its properties right now and then construct a value
# object from it. This will be slow :(.
# Ugly delayed import here because I can't solve the cyclic
# package dependency right now :(.
from ._runtime import python_jsii_mapping
data_type = _data_types[class_fqn]
remote_struct = _FakeReference(ref)
python_props = {
python_name: kernel.get(remote_struct, jsii_name)
for python_name, jsii_name in (
python_jsii_mapping(data_type) or {}
).items()
}
return data_type(**python_props)
elif class_fqn in _enums:
return _enums[class_fqn]
elif class_fqn == "Object":
# If any one interface is a struct, all of them are guaranteed to be (Kernel invariant)
if ref.interfaces is not None and any(
fqn in _data_types for fqn in ref.interfaces
):
# Ugly delayed import here because I can't solve the cyclic
# package dependency right now :(.
from ._runtime import python_jsii_mapping
structs = [_data_types[fqn] for fqn in ref.interfaces]
remote_struct = _FakeReference(ref)
if len(structs) == 1:
struct = structs[0]
else:
struct = new_combined_struct(structs)
return struct(
**{
python_name: kernel.get(remote_struct, jsii_name)
for python_name, jsii_name in (
python_jsii_mapping(struct) or {}
).items()
}
)
else:
return InterfaceDynamicProxy(self.build_interface_proxies_for_ref(ref))
else:
raise ValueError(f"Unknown type: {class_fqn}")