Skip to content

Commit a81b501

Browse files
authored
Merge pull request #28 from django-components/jo-refactor-get-parserconfig-tags
2 parents 8525ebb + 37c54dd commit a81b501

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release notes
22

3+
## v1.2.2
4+
5+
- Add `get_tag()` method to `ParserConfig` to retrieve tag configurations
6+
- Remove `ParserConfig.tags` attribute (use `get_tag()` instead)
7+
38
## v1.2.1
49

510
- Allow to get allowed flags from `TagConfig`

crates/djc-template-parser/src/parser_config.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ impl ParserConfig {
155155
let tag_name = tag_config.tag_name().to_string();
156156
self.tags.insert(tag_name, tag_config);
157157
}
158+
159+
/// Get config for a tag
160+
pub fn get_tag(&self, tag_name: &str) -> Option<TagConfig> {
161+
self.tags.get(tag_name).cloned()
162+
}
158163
}
159164

160165
impl ParserConfig {
@@ -220,9 +225,7 @@ impl ParserConfig {
220225

221226
// Check that "for" is not specified, as it's a reserved special tag
222227
if self.tags.contains_key("for") {
223-
return Err(
224-
"Tag 'for' is reserved and cannot be specified in tag config".to_string(),
225-
);
228+
return Err("Tag 'for' is reserved and cannot be specified in tag config".to_string());
226229
}
227230

228231
Ok(flags_map)

djc_core/rust.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,10 @@ class template_parser:
287287
It can be constructed from Python or Rust.
288288
"""
289289
def __init__(self, version: "template_parser.TemplateVersion") -> None: ...
290-
tags: dict[str, "template_parser.TagConfig"] # Map from tag name to TagConfig
291290
version: "template_parser.TemplateVersion" # Template version
292291
def set_tag(
293292
self, tag_config: "template_parser.TagConfig"
294293
) -> None: ... # Set config for a tag
294+
def get_tag(
295+
self, tag_name: str
296+
) -> Optional["template_parser.TagConfig"]: ... # Get config for a tag

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "maturin"
44

55
[project]
66
name = "djc_core"
7-
version = "1.2.1"
7+
version = "1.2.2"
88
requires-python = ">=3.8, <4.0"
99
description = "Core library for django-components written in Rust."
1010
keywords = ["django", "components", "html"]

tests/test_template_parser__tag.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5153,6 +5153,71 @@ def test_get_flags_empty_flags(self):
51535153
assert len(flags) == 0
51545154

51555155

5156+
class TestParserConfigGetTag:
5157+
def test_get_tag_plain_tag(self):
5158+
config = ParserConfig(version=TemplateVersion.v1)
5159+
tag_config = TagConfig(
5160+
tag=TagSpec("my_tag", flags={"flag1", "flag2"}),
5161+
sections=None,
5162+
)
5163+
config.set_tag(tag_config)
5164+
5165+
retrieved = config.get_tag("my_tag")
5166+
assert retrieved is not None
5167+
assert retrieved.get_flags() == {"flag1", "flag2"}
5168+
5169+
def test_get_tag_tag_with_body(self):
5170+
config = ParserConfig(version=TemplateVersion.v1)
5171+
tag_config = TagConfig(
5172+
tag=TagSpec("if", flags={"flag1", "flag2", "flag3"}),
5173+
sections=[
5174+
TagSectionSpec(
5175+
tag=TagSpec("elif", flags={"section_flag"}),
5176+
repeatable=True,
5177+
)
5178+
],
5179+
)
5180+
config.set_tag(tag_config)
5181+
5182+
retrieved = config.get_tag("if")
5183+
assert retrieved is not None
5184+
assert retrieved.get_flags() == {"flag1", "flag2", "flag3"}
5185+
5186+
def test_get_tag_not_found(self):
5187+
config = ParserConfig(version=TemplateVersion.v1)
5188+
tag_config = TagConfig(
5189+
tag=TagSpec("my_tag", flags={"flag1"}),
5190+
sections=None,
5191+
)
5192+
config.set_tag(tag_config)
5193+
5194+
retrieved = config.get_tag("nonexistent")
5195+
assert retrieved is None
5196+
5197+
def test_get_tag_mutation_does_not_affect_original(self):
5198+
config = ParserConfig(version=TemplateVersion.v1)
5199+
original_tag_config = TagConfig(
5200+
tag=TagSpec("my_tag", flags={"flag1", "flag2"}),
5201+
sections=None,
5202+
)
5203+
config.set_tag(original_tag_config)
5204+
5205+
# Get the tag config
5206+
retrieved = config.get_tag("my_tag")
5207+
assert retrieved is not None
5208+
assert retrieved.get_flags() == {"flag1", "flag2"}
5209+
5210+
# Mutate the retrieved TagConfig's flags
5211+
retrieved_flags = retrieved.get_flags()
5212+
retrieved_flags.add("flag3")
5213+
retrieved_flags.discard("flag1")
5214+
5215+
# Verify the original config is NOT affected
5216+
retrieved_again = config.get_tag("my_tag")
5217+
assert retrieved_again is not None
5218+
assert retrieved_again.get_flags() == {"flag1", "flag2"}
5219+
5220+
51565221
class TestSelfClosing:
51575222
def test_self_closing_simple(self):
51585223
ast = parse_tag("{% my_tag / %}")

0 commit comments

Comments
 (0)