Coverage for formkit_ninja / parser / converters_examples.py: 56.52%
23 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-20 04:40 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-20 04:40 +0000
1"""
2Example converters for FormKit node type conversion.
4These converters demonstrate how to create custom converters that match nodes
5by options patterns or field names. They can be used as-is or as templates
6for project-specific converters.
8Example usage:
9 from formkit_ninja.parser.converters_examples import OptionsPatternConverter, FieldNameConverter
10 from formkit_ninja.parser.converters import TypeConverterRegistry
12 registry = TypeConverterRegistry()
13 registry.register(OptionsPatternConverter(pattern="$ida(", pydantic_type="int"))
14 registry.register(FieldNameConverter(names={"district", "suco"}, pydantic_type="int"))
15"""
17from __future__ import annotations
19from formkit_ninja.formkit_schema import FormKitType
20from formkit_ninja.parser.converters import BaseConverter
23class OptionsPatternConverter(BaseConverter):
24 """
25 Converter that matches nodes by options pattern.
27 This converter is useful for matching nodes based on their options attribute,
28 such as IDA options ($ida(...)) or other option patterns.
30 Example:
31 converter = OptionsPatternConverter(pattern="$ida(", pydantic_type="int")
32 # Matches nodes with options like "$ida(yesno)", "$ida(output)", etc.
33 """
35 def __init__(self, pattern: str, pydantic_type: str):
36 """
37 Initialize the converter.
39 Args:
40 pattern: The pattern to match at the start of options string (e.g., "$ida(")
41 pydantic_type: The Pydantic type to return (e.g., "int", "str")
42 """
43 self.pattern = pattern
44 self.pydantic_type = pydantic_type
46 def can_convert(self, node: FormKitType) -> bool:
47 """
48 Check by formkit - return False to allow options-based matching.
50 This method returns False so that the registry will try
51 can_convert_by_options() instead.
52 """
53 return False
55 def can_convert_by_options(self, options: str) -> bool:
56 """
57 Check if this converter matches the given options pattern.
59 Args:
60 options: The options attribute of the node (as string)
62 Returns:
63 True if options starts with the pattern, False otherwise
64 """
65 return options.startswith(self.pattern)
67 def to_pydantic_type(self, node: FormKitType) -> str:
68 """
69 Convert the node to a Pydantic type string.
71 Args:
72 node: The FormKit node to convert
74 Returns:
75 The configured pydantic type string
76 """
77 return self.pydantic_type
80class FieldNameConverter(BaseConverter):
81 """
82 Converter that matches nodes by field name.
84 This converter is useful for matching nodes based on their name attribute,
85 such as specific field names that should have a particular type.
87 Example:
88 converter = FieldNameConverter(
89 names={"district", "suco", "aldeia"},
90 pydantic_type="int"
91 )
92 # Matches nodes with name "district", "suco", or "aldeia"
93 """
95 def __init__(self, names: set[str] | list[str], pydantic_type: str):
96 """
97 Initialize the converter.
99 Args:
100 names: Set or list of node names to match (e.g., {"district", "suco"})
101 pydantic_type: The Pydantic type to return (e.g., "int", "Decimal")
102 """
103 self.names = set(names) if isinstance(names, list) else names
104 self.pydantic_type = pydantic_type
106 def can_convert(self, node: FormKitType) -> bool:
107 """
108 Check by formkit - return False to allow name-based matching.
110 This method returns False so that the registry will try
111 can_convert_by_name() instead.
112 """
113 return False
115 def can_convert_by_name(self, node_name: str) -> bool:
116 """
117 Check if this converter matches the given node name.
119 Args:
120 node_name: The name attribute of the node
122 Returns:
123 True if node_name is in the configured names set, False otherwise
124 """
125 return node_name in self.names
127 def to_pydantic_type(self, node: FormKitType) -> str:
128 """
129 Convert the node to a Pydantic type string.
131 Args:
132 node: The FormKit node to convert
134 Returns:
135 The configured pydantic type string
136 """
137 return self.pydantic_type