File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
python/selfie-lib/selfie_lib Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 1+ from typing import Any , Generic , TypeVar
2+
3+ # Define generic type variables
4+ T = TypeVar ("T" )
5+ SerializedForm = TypeVar ("SerializedForm" )
6+
7+
8+ class Roundtrip (Generic [T , SerializedForm ]):
9+ def serialize (self , value : T ) -> SerializedForm :
10+ """Serialize a value of type T to its SerializedForm."""
11+ raise NotImplementedError
12+
13+ def parse (self , serialized : SerializedForm ) -> T :
14+ """Parse the SerializedForm back to type T."""
15+ raise NotImplementedError
16+
17+ @classmethod
18+ def identity (cls ) -> "Roundtrip[T, T]" :
19+ """Return an identity Roundtrip that does no transformation."""
20+
21+ class Identity (Roundtrip [Any , Any ]):
22+ def serialize (self , value : Any ) -> Any :
23+ return value
24+
25+ def parse (self , serialized : Any ) -> Any :
26+ return serialized
27+
28+ return Identity ()
Original file line number Diff line number Diff line change 1010from .Literals import LiteralValue as LiteralValue
1111from .ParseException import ParseException as ParseException
1212from .PerCharacterEscaper import PerCharacterEscaper as PerCharacterEscaper
13- from .RoundTrip import Roundtrip as Roundtrip
13+ from .Roundtrip import Roundtrip as Roundtrip
1414from .Selfie import expect_selfie as expect_selfie
1515from .Slice import Slice as Slice
1616from .Snapshot import Snapshot as Snapshot
You can’t perform that action at this time.
0 commit comments