refactor(distributed): Un-nest generateGlobalObject's applyFieldValues function (#1667)

This commit is contained in:
Timothy Choi 2024-09-16 13:55:53 +02:00 committed by GitHub
parent f5401b19fa
commit 8caf582b32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 30 deletions

View File

@ -174,35 +174,6 @@ class ConnectionRepository(
return retVal
def generateGlobalObject(self, doId, dcname, values=None):
def applyFieldValues(distObj, dclass, values):
for i in range(dclass.getNumInheritedFields()):
field = dclass.getInheritedField(i)
if field.asMolecularField() is None:
value = values.get(field.getName(), None)
if value is None and field.isRequired():
# Gee, this could be better. What would really be
# nicer is to get value from field.getDefaultValue
# or similar, but that returns a binary string, not
# a python tuple, like the following does. If you
# want to change something better, please go ahead.
packer = DCPacker()
packer.beginPack(field)
packer.packDefaultValue()
packer.endPack()
unpacker = DCPacker()
unpacker.setUnpackData(packer.getString())
unpacker.beginUnpack(field)
value = unpacker.unpackObject()
unpacker.endUnpack()
if value is not None:
function = getattr(distObj, field.getName())
if function is not None:
function(*value)
else:
self.notify.error("\n\n\nNot able to find %s.%s"%(
distObj.__class__.__name__, field.getName()))
# Look up the dclass
dclass = self.dclassesByName.get(dcname+self.dcSuffix)
if dclass is None:
@ -229,7 +200,37 @@ class ConnectionRepository(
distObj.generateInit() # Only called when constructed
distObj.generate()
if values is not None:
applyFieldValues(distObj, dclass, values)
for i in range(dclass.getNumInheritedFields()):
field = dclass.getInheritedField(i)
if field.asMolecularField() is None:
value = values.get(field.getName(), None)
if value is None and field.isRequired():
# Gee, this could be better. What would really be
# nicer is to get value from field.getDefaultValue
# or similar, but that returns a binary string, not
# a python tuple, like the following does. If you
# want to change something better, please go ahead.
packer = DCPacker()
packer.beginPack(field)
packer.packDefaultValue()
packer.endPack()
unpacker = DCPacker()
unpacker.setUnpackData(packer.getString())
unpacker.beginUnpack(field)
value = unpacker.unpackObject()
unpacker.endUnpack()
if value is not None:
function = getattr(distObj, field.getName())
if function is not None:
function(*value)
else:
self.notify.error(
"\n\n\nNot able to find %s.%s" % (
distObj.__class__.__name__,
field.getName()
)
)
distObj.announceGenerate()
distObj.parentId = 0
distObj.zoneId = 0