Skip to content

Commit 0a7f658

Browse files
author
Lavisha Gupta
committed
test case fix
1 parent d9b0575 commit 0a7f658

3 files changed

Lines changed: 100 additions & 9 deletions

File tree

compiler/fory_compiler/generators/go.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,45 @@ def message_has_unions(self, message: Message) -> bool:
199199
PrimitiveKind.ANY: "any",
200200
}
201201

202+
def wrap_line(
203+
self,
204+
line: str,
205+
max_width: int = 80,
206+
indent: str = "",
207+
continuation_indent: str = None,
208+
) -> List[str]:
209+
"""Override base wrap_line to handle Go-specific syntax.
210+
211+
Go has specific constructs that should not be wrapped:
212+
1. Function signatures - must not split between params and return type
213+
2. Struct field tags - backtick strings must not be split
214+
3. Single-line function bodies - must not split "{ return ... }"
215+
"""
216+
if continuation_indent is None:
217+
continuation_indent = indent + " "
218+
219+
# If line is already short enough, return as is
220+
if len(line) <= max_width:
221+
return [line]
222+
223+
stripped = line.lstrip()
224+
225+
# Don't wrap comments
226+
if stripped.startswith("//") or stripped.startswith("/*") or stripped.startswith("*"):
227+
return [line]
228+
229+
# Don't wrap lines with backticks (struct tags)
230+
if "`" in line:
231+
return [line]
232+
233+
# Don't wrap function definitions (including one-liners like "func Foo() Type { return ... }")
234+
# Detect by checking if line starts with "func " and contains "{"
235+
if stripped.startswith("func ") and "{" in line:
236+
return [line]
237+
238+
# For all other cases, use base class wrapping
239+
return super().wrap_line(line, max_width, indent, continuation_indent)
240+
202241
def generate(self) -> List[GeneratedFile]:
203242
"""Generate Go files for the schema."""
204243
files = []
@@ -586,15 +625,13 @@ def generate_union(
586625
lines.append(f"\tcase {case_type}{case_name}:")
587626
lines.append(f"\t\tv, ok := u.value.({case_type_name})")
588627
lines.append("\t\tif !ok {")
589-
lines.append(
590-
f'\t\t\treturn fmt.Errorf("corrupted {type_name}: case={case_name} but invalid value")'
591-
)
628+
lines.append(f'\t\t\treturn fmt.Errorf("corrupted {type_name}: " +')
629+
lines.append(f'\t\t\t\t"case={case_name} but invalid value")')
592630
lines.append("\t\t}")
593631
if case_type_name.startswith("*"):
594632
lines.append("\t\tif v == nil {")
595-
lines.append(
596-
f'\t\t\treturn fmt.Errorf("corrupted {type_name}: case={case_name} but nil value")'
597-
)
633+
lines.append(f'\t\t\treturn fmt.Errorf("corrupted {type_name}: " +')
634+
lines.append(f'\t\t\t\t"case={case_name} but nil value")')
598635
lines.append("\t\t}")
599636
lines.append(f"\t\tif visitor.{case_name} != nil {{")
600637
lines.append(f"\t\t\treturn visitor.{case_name}(v)")
@@ -864,7 +901,8 @@ def generate_field(
864901

865902
if tags:
866903
tag_str = ",".join(tags)
867-
lines.append(f'{field_name} {go_type} `fory:"{tag_str}"`')
904+
# Concatenate parts to ensure single line in output
905+
lines.append(f"{field_name} {go_type} `fory:\"{tag_str}\"`")
868906
else:
869907
lines.append(f"{field_name} {go_type}")
870908

compiler/fory_compiler/generators/java.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ def generate_union_class(
658658
lines.append(f"{ind} return {type_id_expr};")
659659
lines.append(f"{ind} default:")
660660
lines.append(
661-
f'{ind} throw new IllegalStateException("Unknown {union.name} case id: " + caseId);'
661+
f'{ind} throw new IllegalStateException("Unknown " + '
662+
f'"{union.name} case id: " + caseId);'
662663
)
663664
lines.append(f"{ind} }}")
664665
lines.append(f"{ind} }}")
@@ -694,7 +695,8 @@ def generate_union_class(
694695
lines.append(f"{ind} return {case_enum}.{case_enum_name};")
695696
lines.append(f"{ind} default:")
696697
lines.append(
697-
f'{ind} throw new IllegalStateException("Unknown {union.name} case id: " + index);'
698+
f'{ind} throw new IllegalStateException("Unknown " + '
699+
f'"{union.name} case id: " + index);'
698700
)
699701
lines.append(f"{ind} }}")
700702
lines.append(f"{ind} }}")

compiler/fory_compiler/generators/python.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,57 @@ class PythonGenerator(BaseGenerator):
141141
PrimitiveKind.ANY: "None",
142142
}
143143

144+
def wrap_line(
145+
self,
146+
line: str,
147+
max_width: int = 80,
148+
indent: str = "",
149+
continuation_indent: str = None,
150+
) -> List[str]:
151+
"""Override base wrap_line to handle Python-specific syntax.
152+
153+
Python has specific constructs that should not be wrapped:
154+
1. Assignment statements - must not split between variable and value
155+
2. Conditional statements with logical operators (and, or, not)
156+
3. Raise statements with string literals
157+
"""
158+
if continuation_indent is None:
159+
continuation_indent = indent + " "
160+
161+
# If line is already short enough, return as is
162+
if len(line) <= max_width:
163+
return [line]
164+
165+
stripped = line.lstrip()
166+
167+
# Don't wrap comments
168+
if stripped.startswith("#"):
169+
return [line]
170+
171+
# Don't wrap raise statements (they often have long string literals)
172+
if stripped.startswith("raise "):
173+
return [line]
174+
175+
# Don't wrap assignment statements (lines containing " = " at the statement level)
176+
# This prevents splitting like: _threadsafe_fory = pyfory.ThreadSafeFory(...)
177+
# Only check for simple assignments (not comparisons or keyword args)
178+
if " = " in line and not line.strip().startswith("if ") and not line.strip().startswith("elif ") and not line.strip().startswith("while "):
179+
# Check if this looks like a simple assignment (has = but not == or <= or >=)
180+
# and the = comes before any opening parentheses
181+
eq_pos = line.find(" = ")
182+
paren_pos = line.find("(")
183+
if eq_pos > 0 and (paren_pos < 0 or eq_pos < paren_pos):
184+
# This is likely an assignment statement, don't wrap it
185+
return [line]
186+
187+
# Don't wrap conditional statements (if/elif/while) with logical operators
188+
# This prevents splitting like: if condition and not\n isinstance(...)
189+
if stripped.startswith(("if ", "elif ", "while ")) and (" and " in line or " or " in line):
190+
return [line]
191+
192+
# For all other cases, use base class wrapping
193+
return super().wrap_line(line, max_width, indent, continuation_indent)
194+
144195
def safe_name(self, name: str) -> str:
145196
"""Return a Python-safe identifier."""
146197
if keyword.iskeyword(name):

0 commit comments

Comments
 (0)