From dab79a1a3d1e65914581f3f43929ab4255d19a2c Mon Sep 17 00:00:00 2001 From: Chris Janidlo Date: Fri, 6 Dec 2024 14:03:23 -0600 Subject: [PATCH] Variable-ize identifier length --- compute_sdk/globus_compute_sdk/serialize/base.py | 8 ++++++++ compute_sdk/globus_compute_sdk/serialize/facade.py | 6 ++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/compute_sdk/globus_compute_sdk/serialize/base.py b/compute_sdk/globus_compute_sdk/serialize/base.py index 9cb0f81d9..82d63150c 100644 --- a/compute_sdk/globus_compute_sdk/serialize/base.py +++ b/compute_sdk/globus_compute_sdk/serialize/base.py @@ -2,12 +2,20 @@ from globus_compute_sdk.errors import DeserializationError +# 2 unique characters and a newline +IDENTIFIER_LENGTH = 3 + class SerializationStrategy(metaclass=ABCMeta): """A SerializationStrategy is in charge of converting function source code or arguments into string data and back again. """ + def __init_subclass__(cls): + super().__init_subclass__() + if len(cls.identifier) != IDENTIFIER_LENGTH: + raise ValueError(f"Identifiers must be {IDENTIFIER_LENGTH} characters long") + @property @abstractmethod def identifier(self): diff --git a/compute_sdk/globus_compute_sdk/serialize/facade.py b/compute_sdk/globus_compute_sdk/serialize/facade.py index 9f2a32389..0a93e3081 100644 --- a/compute_sdk/globus_compute_sdk/serialize/facade.py +++ b/compute_sdk/globus_compute_sdk/serialize/facade.py @@ -4,7 +4,7 @@ import typing as t from globus_compute_sdk.errors import DeserializationError, SerializationError -from globus_compute_sdk.serialize.base import SerializationStrategy +from globus_compute_sdk.serialize.base import IDENTIFIER_LENGTH, SerializationStrategy from globus_compute_sdk.serialize.concretes import ( DEFAULT_STRATEGY_CODE, DEFAULT_STRATEGY_DATA, @@ -41,9 +41,7 @@ def validate(strategy: SerializationStrategy) -> SerializationStrategy: validate(strategy_data) if strategy_data else DEFAULT_STRATEGY_DATA ) - # grab a randomish ID from the map (all identifiers should be the same length) - identifier = next(iter(STRATEGIES_MAP.keys())) - self.header_size = len(identifier) + self.header_size = IDENTIFIER_LENGTH self.strategies = { header: strategy_class()