Skip to content

Commit

Permalink
Rename mapping properties and add mapping to Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dkraczkowski committed Nov 8, 2023
1 parent 102d56a commit 9ac1089
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions chili/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ class Decoder(Generic[T]):
def __init__(self, decoders: Union[TypeDecoders, Dict[Any, TypeDecoder]] = None, mapper: Optional[Mapper] = None):
if decoders and not isinstance(decoders, TypeDecoders):
decoders = TypeDecoders(decoders)
self.mapper = mapper
self.decode_mapper = mapper
self.type_decoders = decoders

@property
Expand All @@ -555,8 +555,8 @@ def decode(self, obj: Dict[str, StateObject]) -> T:
if hasattr(self.__generic__, _DECODE_MAPPER):
mapper = getattr(self.__generic__, _DECODE_MAPPER)
obj = mapper.map(obj)
elif self.mapper:
obj = self.mapper.map(obj)
elif self.decode_mapper:
obj = self.decode_mapper.map(obj)

for key, prop in self.schema.items():
if key not in obj:
Expand Down
6 changes: 3 additions & 3 deletions chili/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class Encoder(Generic[T]):
def __init__(self, encoders: Union[Dict, TypeEncoders] = None, mapper: Optional[Mapper] = None):
if encoders and not isinstance(encoders, TypeEncoders):
encoders = TypeEncoders(encoders)
self.mapper = mapper
self.encode_mapper = mapper
self.type_encoders = encoders

def encode(self, obj: T) -> StateObject:
Expand All @@ -466,8 +466,8 @@ def encode(self, obj: T) -> StateObject:
if hasattr(self.__generic__, _ENCODE_MAPPER):
mapper = getattr(self.__generic__, _ENCODE_MAPPER)
return mapper.map(result)
elif self.mapper:
return self.mapper.map(result)
elif self.encode_mapper:
return self.encode_mapper.map(result)

return result

Expand Down
12 changes: 9 additions & 3 deletions chili/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@
class Serializer(Encoder, Decoder, Generic[T]):
__generic__: Type[T]

def __init__(self, type_encoders: Any = None, type_decoders: Any = None):
Encoder.__init__(self, type_encoders)
Decoder.__init__(self, type_decoders)
def __init__(
self,
type_encoders: Any = None,
type_decoders: Any = None,
encode_mapper: Optional[Mapper] = None,
decode_mapper: Optional[Mapper] = None,
):
Encoder.__init__(self, type_encoders, mapper=encode_mapper)
Decoder.__init__(self, type_decoders, mapper=decode_mapper)

@classmethod
def __class_getitem__(cls, item: Type[T]) -> Type[Serializer]: # noqa: E501
Expand Down

0 comments on commit 9ac1089

Please sign in to comment.