Skip to content

Commit 9a2095b

Browse files
wpbonelliwpbonelli
authored andcommitted
typed records
1 parent 07b7795 commit 9a2095b

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

flopy4/mf6/codec/reader/grammar/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import jinja2
55
from modflow_devtools.dfn import Dfn, get_blocks, get_fields
66

7-
from flopy4.mf6.codec.reader.grammar.filters import field_type
7+
from flopy4.mf6.codec.reader.grammar.filters import field_type, record_child_type
88

99

1010
def _get_template_env():
@@ -16,6 +16,7 @@ def _get_template_env():
1616
keep_trailing_newline=True,
1717
)
1818
env.filters["field_type"] = field_type
19+
env.filters["record_child_type"] = record_child_type
1920
return env
2021

2122

flopy4/mf6/codec/reader/grammar/filters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,18 @@ def field_type(field: Field) -> str:
1313
return "record"
1414
case _:
1515
return field["type"]
16+
17+
18+
def record_child_type(field: Field) -> str:
19+
"""Get the grammar type for a field within a record context."""
20+
match field["type"]:
21+
case "string":
22+
return "string"
23+
case "integer":
24+
return "integer"
25+
case "double precision":
26+
return "double"
27+
case "keyword":
28+
return ""
29+
case _:
30+
return field["type"]

flopy4/mf6/codec/reader/grammar/templates/component.lark.jinja

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ block: {% for block_name in blocks.keys() %}{{ block_name }}_block{% if not loop
1313
{% endfor %}
1414
{% for field_name, field in fields.items() %}
1515
{% set field_type = field|field_type %}
16+
{% if field['type'] == 'record' and field.get('children') %}
17+
{{ field_name }}: "{{ field['name'] }}"i {{ field_name }}_record
18+
{{ field_name }}_record: {% for child_name, child in field['children'].items() %}{% set child_type = child|record_child_type %}{% if child['type'] == 'keyword' %}"{{ child['name'] }}"i{% else %}{{ child_type }}{% endif %}{% if not loop.last %} {% endif %}{% endfor %}
19+
20+
{% else %}
1621
{{ field_name }}: "{{ field['name'] }}"i {{ field_type }}
22+
{% endif %}
1723
{% endfor %}

0 commit comments

Comments
 (0)