diff --git a/compiler/fory_compiler/generators/cpp.py b/compiler/fory_compiler/generators/cpp.py index 91e6b49fdd..aa69dd5001 100644 --- a/compiler/fory_compiler/generators/cpp.py +++ b/compiler/fory_compiler/generators/cpp.py @@ -222,8 +222,9 @@ def generate_bytes_methods(self, class_name: str, indent: str) -> List[str]: lines.append(f"{indent}}}") lines.append("") lines.append( - f"{indent}static fory::Result<{class_name}, fory::Error> from_bytes(const std::vector& data) {{" + f"{indent}static fory::Result<{class_name}, fory::Error> from_bytes(" ) + lines.append(f"{indent} const std::vector& data) {{") lines.append( f"{indent} return detail::get_fory().deserialize<{class_name}>(data);" ) @@ -834,13 +835,13 @@ def generate_field_accessors( f"{indent}void set_{field_name}(Arg&& arg, Args&&... args) {{" ) if field.optional: - lines.append( - f"{indent} {member_name}.emplace(std::forward(arg), std::forward(args)...);" - ) + lines.append(f"{indent} {member_name}.emplace(") + lines.append(f"{indent} std::forward(arg),") + lines.append(f"{indent} std::forward(args)...);") else: - lines.append( - f"{indent} {member_name} = {value_type}(std::forward(arg), std::forward(args)...);" - ) + lines.append(f"{indent} {member_name} = {value_type}(") + lines.append(f"{indent} std::forward(arg),") + lines.append(f"{indent} std::forward(args)...);") lines.append(f"{indent}}}") else: lines.append(f"{indent}void set_{field_name}({value_type} value) {{") @@ -921,7 +922,14 @@ def generate_message_definition( conditions = [ self.get_field_eq_expression(field, lineage) for field in message.fields ] - lines.append(f"{body_indent} return {' && '.join(conditions)};") + return_line = f"{body_indent} return {' && '.join(conditions)};" + if len(return_line) > 80: + lines.append(f"{body_indent} return") + for index, condition in enumerate(conditions): + suffix = " &&" if index + 1 < len(conditions) else ";" + lines.append(f"{body_indent} {condition}{suffix}") + else: + lines.append(return_line) else: lines.append(f"{body_indent} return true;") lines.append(f"{body_indent}}}") @@ -939,12 +947,19 @@ def generate_message_definition( lines.append(f"{field_indent}{field_type} {member_name};") lines.append("") lines.append(f"{body_indent}public:") - field_members = ", ".join( - self.get_field_macro_entry(f) for f in message.fields - ) - lines.append( - f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});" - ) + macro_entries = [self.get_field_macro_entry(f) for f in message.fields] + field_members = ", ".join(macro_entries) + macro_line = f"{body_indent}FORY_STRUCT({struct_type_name}, {field_members});" + + if len(macro_line) > 80: + lines.append(f"{body_indent}FORY_STRUCT(") + lines.append(f"{body_indent} {struct_type_name},") + for index, entry in enumerate(macro_entries): + suffix = "," if index + 1 < len(macro_entries) else "" + lines.append(f"{body_indent} {entry}{suffix}") + lines.append(f"{body_indent});") + else: + lines.append(macro_line) else: lines.append(f"{body_indent}FORY_STRUCT({struct_type_name});") @@ -2014,11 +2029,12 @@ def generate_registration(self) -> List[str]: lines.append("") lines.append("namespace detail {") lines.append("inline fory::serialization::ThreadSafeFory& get_fory() {") - lines.append( - " static fory::serialization::ThreadSafeFory fory = " - "fory::serialization::Fory::builder().xlang(true).track_ref(true)" - ".compatible(true).build_thread_safe();" - ) + lines.append(" static fory::serialization::ThreadSafeFory fory =") + lines.append(" fory::serialization::Fory::builder()") + lines.append(" .xlang(true)") + lines.append(" .track_ref(true)") + lines.append(" .compatible(true)") + lines.append(" .build_thread_safe();") lines.append(" static const bool initialized = []() {") for ns in self._collect_imported_namespaces(): lines.append(f" {ns}::register_types(fory);") diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 383dd8a6e3..7d24002267 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -1525,9 +1525,11 @@ def generate_registration(self) -> List[str]: def generate_fory_helpers(self) -> List[str]: lines: List[str] = [] lines.append("func createFory() *fory.Fory {") - lines.append( - "\tf := fory.New(fory.WithXlang(true), fory.WithRefTracking(true), fory.WithCompatible(true))" - ) + lines.append("\tf := fory.New(") + lines.append("\t\tfory.WithXlang(true),") + lines.append("\t\tfory.WithRefTracking(true),") + lines.append("\t\tfory.WithCompatible(true),") + lines.append("\t)") for alias, _, _ in self._collect_imported_type_infos(): lines.append(f"\tif err := {alias}.RegisterTypes(f); err != nil {{") lines.append("\t\tpanic(err)") diff --git a/compiler/fory_compiler/generators/java.py b/compiler/fory_compiler/generators/java.py index 9b9b7906d8..7ee72f3a12 100644 --- a/compiler/fory_compiler/generators/java.py +++ b/compiler/fory_compiler/generators/java.py @@ -2180,9 +2180,12 @@ def generate_module_file( lines.append(" }") lines.append("") lines.append(" private static ThreadSafeFory createFory() {") - lines.append( - " return Fory.builder().withXlang(true).withCompatible(true).withRefTracking(true).withModule(INSTANCE).buildThreadSafeFory();" - ) + lines.append(" return Fory.builder()") + lines.append(" .withXlang(true)") + lines.append(" .withCompatible(true)") + lines.append(" .withRefTracking(true)") + lines.append(" .withModule(INSTANCE)") + lines.append(" .buildThreadSafeFory();") lines.append(" }") lines.append("") lines.append(" private static class Holder {") diff --git a/compiler/fory_compiler/generators/python.py b/compiler/fory_compiler/generators/python.py index 442c3c7ba1..f13cabff26 100644 --- a/compiler/fory_compiler/generators/python.py +++ b/compiler/fory_compiler/generators/python.py @@ -623,14 +623,26 @@ def generate_field( field_args.append(f"default_factory={default_factory}") else: field_args.append(f"default={default_expr}") - field_default = f"pyfory.field({', '.join(field_args)}){trailing_comment}" + + field_line = ( + f"{field_name}: {python_type} = " + f"pyfory.field({', '.join(field_args)}){trailing_comment}" + ) + + if len(f" {field_line}") > 80: + lines.append(f"{field_name}: {python_type} = pyfory.field(") + for arg in field_args: + lines.append(f" {arg},") + lines.append(f"){trailing_comment}") + else: + lines.append(field_line) else: if default_factory is not None: field_default = f"field(default_factory={default_factory})" else: field_default = f"{default_expr}{trailing_comment}" - lines.append(f"{field_name}: {python_type} = {field_default}") + lines.append(f"{field_name}: {python_type} = {field_default}") return lines @@ -683,7 +695,14 @@ def generate_repr_method(self, message: Message, indent: int = 0) -> List[str]: expr = f"({placeholder_literal} if self.{field_name} is not None else 'None')" else: expr = f"repr(self.{field_name})" - lines.append(f'{ind} parts.append("{field_name}=" + {expr})') + line = f'{ind} parts.append("{field_name}=" + {expr})' + if len(line) > 80: + lines.append(f"{ind} parts.append(") + lines.append(f'{ind} "{field_name}="') + lines.append(f"{ind} + {expr}") + lines.append(f"{ind} )") + else: + lines.append(line) lines.append(f'{ind} return "{message.name}(" + ", ".join(parts) + ")"') return lines @@ -1013,9 +1032,9 @@ def generate_fory_helpers(self) -> List[str]: lines.append(" if _threadsafe_fory is None:") lines.append(" with _fory_lock:") lines.append(" if _threadsafe_fory is None:") - lines.append( - " _threadsafe_fory = pyfory.ThreadSafeFory(fory_factory=_create_fory)" - ) + lines.append(" _threadsafe_fory = pyfory.ThreadSafeFory(") + lines.append(" fory_factory=_create_fory") + lines.append(" )") lines.append(" return _threadsafe_fory") return lines diff --git a/compiler/fory_compiler/generators/rust.py b/compiler/fory_compiler/generators/rust.py index 54ba2f7588..41fd32728e 100644 --- a/compiler/fory_compiler/generators/rust.py +++ b/compiler/fory_compiler/generators/rust.py @@ -615,16 +615,16 @@ def _format_imported_type_name( def generate_bytes_impl(self, type_name: str) -> List[str]: lines = [] lines.append(f"impl {type_name} {{") - lines.append( - " pub fn to_bytes(&self) -> ::std::result::Result<::std::vec::Vec, ::fory::Error> {" - ) + lines.append(" pub fn to_bytes(") + lines.append(" &self,") + lines.append(" ) -> ::std::result::Result<::std::vec::Vec, ::fory::Error> {") lines.append(" let fory = detail::get_fory();") lines.append(" fory.serialize(self)") lines.append(" }") lines.append("") - lines.append( - f" pub fn from_bytes(data: &[u8]) -> ::std::result::Result<{type_name}, ::fory::Error> {{" - ) + lines.append(" pub fn from_bytes(") + lines.append(" data: &[u8],") + lines.append(f" ) -> ::std::result::Result<{type_name}, ::fory::Error> {{") lines.append(" let fory = detail::get_fory();") lines.append(" fory.deserialize(data)") lines.append(" }") @@ -1364,7 +1364,20 @@ def generate_field( ) field_name = self.get_field_identifier(parent_stack[-1], field) - lines.append(f"pub {field_name}: {rust_type},") + field_line = f"pub {field_name}: {rust_type}," + + if len(f" {field_line}") > 80 and rust_type.startswith( + "::std::collections::HashMap<" + ): + inner_type = rust_type.removeprefix("::std::collections::HashMap<").removesuffix(">") + key_type, value_type = inner_type.split(", ", 1) + + lines.append(f"pub {field_name}: ::std::collections::HashMap<") + lines.append(f" {key_type},") + lines.append(f" {value_type},") + lines.append(">,") + else: + lines.append(field_line) return lines @@ -1622,9 +1635,9 @@ def generate_registration(self) -> List[str]: """Generate the Fory registration function.""" lines = [] - lines.append( - "pub fn register_types(fory: &mut ::fory::Fory) -> ::std::result::Result<(), ::fory::Error> {" - ) + lines.append("pub fn register_types(") + lines.append(" fory: &mut ::fory::Fory,") + lines.append(") -> ::std::result::Result<(), ::fory::Error> {") # Register enums (top-level) for enum in self.schema.enums: @@ -1655,9 +1668,8 @@ def generate_fory_helpers(self) -> List[str]: lines.append(" use super::*;") lines.append("") lines.append(" pub(super) fn get_fory() -> &'static ::fory::Fory {") - lines.append( - " static FORY: ::std::sync::OnceLock<::fory::Fory> = ::std::sync::OnceLock::new();" - ) + lines.append(" static FORY: ::std::sync::OnceLock<::fory::Fory> =") + lines.append(" ::std::sync::OnceLock::new();") lines.append(" FORY.get_or_init(|| {") lines.append(" let mut fory = ::fory::Fory::builder()") lines.append(" .xlang(true)")