Example #1
0
 def marshal(self, value):
     if value is None:
         if self.default is not None:
             return self.default
         raise ManifestError(f'"{self.key}" is a required field but it is missing')
     try:
         return_value = self.type.marshal(value)
     except Exception as err:
         raise ManifestError(f'Failed to marshal {self.key}: {err}')
     return return_value
Example #2
0
        def marshal(self, value):
            return_value = []
            value = value or []

            if not isinstance(value, list):
                raise ManifestError(f'"{self.key}" has to be of type list')

            try:
                for item in value:
                    return_value.append(self.type.marshal(item))
            except Exception as err:
                raise ManifestError(f'Failed to convert {self.key}={value} to array: {err}')

            return return_value
Example #3
0
 def unmarshal(self, value):
     try:
         if hasattr(value, 'deparse'):
             return value.deparse()
         return str(value)
     except Exception as err:
         raise ManifestError(f'Failed to unmarshal {value}: {err}')
Example #4
0
        def marshal(self, value: Optional[dict]):
            result = {}
            value = value or {}

            if not isinstance(value, dict):
                raise ManifestError(f'"{self.key}" field has to be a dictionary')

            for item in self.items:
                next_value = value.get(item.key)
                result[item.key] = item.marshal(next_value)
            return result
Example #5
0
        def marshal(self, value):
            if value is None:
                return []

            return_value = []
            try:
                for item in value:
                    return_value.append(self.type.marshal(item))
            except Exception as err:
                raise ManifestError(f'Failed to convert {self.key}={value} to array: {err}')

            return return_value
Example #6
0
 def marshal(self, value):
     if not isinstance(value, self.type):
         raise ManifestError(f'{value} is not of type {self.type.__name__}')
     return value
Example #7
0
 def marshal(self, value):
     try:
         return self.type.parse(value)
     except ValueError as err:
         raise ManifestError(f'Failed to marshal {value}: {err}')