Skip to content

Commit

Permalink
fix: properly handle tag replace
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed May 19, 2022
1 parent a3147de commit 6575092
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion beet/library/data_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class TagFile(JsonFile):
def merge(self: TagFileType, other: TagFileType) -> bool: # type: ignore
if other.data.get("replace"):
self.data["replace"] = True
self.data["values"] = deepcopy(other.data.get("values", []))
return True

values = self.data.setdefault("values", [])

Expand All @@ -172,10 +174,12 @@ def merge(self: TagFileType, other: TagFileType) -> bool: # type: ignore
values.append(deepcopy(value))
return True

def prepend(self: TagFileType, other: TagFileType) -> bool: # type: ignore
def prepend(self: TagFileType, other: TagFileType):
"""Prepend values from another tag."""
if other.data.get("replace"):
self.data["replace"] = True
self.data["values"] = deepcopy(other.data.get("values", []))
return

values = self.data.setdefault("values", [])

Expand Down
19 changes: 19 additions & 0 deletions tests/test_data_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ def test_with_tags():
assert p1 == p2


def test_tag_replace():
p1 = DataPack()
p1["demo:foo"] = Function(["say hello"], tags=["minecraft:load"])

p2 = DataPack()
p2["minecraft:load"] = FunctionTag({"values": ["demo:bar"], "replace": True})

p1.merge(p2)

assert p1 == {
"demo": {Function: {"foo": Function(["say hello"])}},
"minecraft": {
FunctionTag: {
"load": FunctionTag({"values": ["demo:bar"], "replace": True})
}
},
}


def test_context_manager(tmp_path: Path):
with DataPack(path=tmp_path / "foobar") as p1:
p1["hello:world"] = Function(["say hello"], tags=["minecraft:load"])
Expand Down

0 comments on commit 6575092

Please sign in to comment.