Skip to content

Commit

Permalink
remove match/case
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Mar 19, 2024
1 parent 293532c commit 6895c3d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
38 changes: 19 additions & 19 deletions src/automerge/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
Value = ObjType | tuple[ScalarType, ScalarValue]

def extract(doc: Document, obj_id: bytes = ROOT) -> Thing:
match doc.object_type(obj_id):
case ObjType.Map:
d: dict[str, Thing] = {}
for k in doc.keys(obj_id):
x = doc.get(obj_id, k)
assert x is not None
v, id = x
d[k] = extract(doc, id) if isinstance(v, ObjType) else v[1]
return d
case ObjType.List:
l: list[Thing] = []
for k2 in range(0, doc.length(obj_id)):
x = doc.get(obj_id, k2)
assert x is not None
v, id = x
l.append(extract(doc, id) if isinstance(v, ObjType) else v[1])
return l
case ObjType.Text:
return doc.text(obj_id)
ot = doc.object_type(obj_id)
if ot == ObjType.Map:
d: dict[str, Thing] = {}
for k in doc.keys(obj_id):
x = doc.get(obj_id, k)
assert x is not None
v, id = x
d[k] = extract(doc, id) if isinstance(v, ObjType) else v[1]
return d
elif ot == ObjType.List:
l: list[Thing] = []
for k2 in range(0, doc.length(obj_id)):
x = doc.get(obj_id, k2)
assert x is not None
v, id = x
l.append(extract(doc, id) if isinstance(v, ObjType) else v[1])
return l
elif ot == ObjType.Text:
return doc.text(obj_id)
raise Exception("unexpected result from doc.object_type")

__doc__ = _automerge.__doc__
Expand Down
18 changes: 8 additions & 10 deletions src/automerge/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ def to_py(self) -> core.Thing:
def _maybe_wrap(self, x: tuple[core.Value, bytes]) -> 'MapReadProxy | ListReadProxy | core.ScalarValue':
value, obj_id = x
if isinstance(value, core.ObjType):
match value:
case core.ObjType.List:
return ListReadProxy(self._doc, obj_id, self._heads)
case core.ObjType.Map:
return MapReadProxy(self._doc, obj_id, self._heads)
if value == core.ObjType.List:
return ListReadProxy(self._doc, obj_id, self._heads)
elif value == core.ObjType.Map:
return MapReadProxy(self._doc, obj_id, self._heads)
raise Exception("unknown obj type")
_, v = value
return v
Expand Down Expand Up @@ -142,11 +141,10 @@ def __getitem__(self, key: int | slice) -> MutableProxyThing | MutableSequence[M
if x is None: return None
value, obj_id = x
if isinstance(value, core.ObjType):
match value:
case core.ObjType.Map:
return MapWriteProxy(self._tx, obj_id, self._heads)
case core.ObjType.List:
return ListWriteProxy(self._tx, obj_id, self._heads)
if value == core.ObjType.Map:
return MapWriteProxy(self._tx, obj_id, self._heads)
elif value == core.ObjType.List:
return ListWriteProxy(self._tx, obj_id, self._heads)
raise Exception("unknown ObjType")
_, v = value
return v
Expand Down

0 comments on commit 6895c3d

Please sign in to comment.