MCPcopy Create free account
hub / github.com/apache/fory / generate_union

Method generate_union

compiler/fory_compiler/generators/csharp.py:825–890  ·  view source on GitHub ↗
(
        self,
        union: Union,
        indent: int = 0,
        parent_stack: Optional[List[Message]] = None,
    )

Source from the content-addressed store, hash-verified

823 return lines
824
825 def generate_union(
826 self,
827 union: Union,
828 indent: int = 0,
829 parent_stack: Optional[List[Message]] = None,
830 ) -> List[str]:
831 lines: List[str] = []
832 ind = self.indent_str * indent
833 type_name = self.safe_type_identifier(union.name)
834 module_class = self.get_module_class_name()
835 full_type_ref = self._type_reference_for_local(union)
836
837 comment = self.format_type_id_comment(union, f"{ind}//")
838 if comment:
839 lines.append(comment)
840 lines.append(f"{ind}[ForyUnion]")
841 lines.append(f"{ind}public abstract partial record {type_name}")
842 lines.append(f"{ind}{{")
843 lines.append(f"{ind}{self.indent_str}private {type_name}()")
844 lines.append(f"{ind}{self.indent_str}{{")
845 lines.append(f"{ind}{self.indent_str}}}")
846 lines.append("")
847
848 lines.append(f"{ind}{self.indent_str}[ForyUnknownCase]")
849 lines.append(
850 f"{ind}{self.indent_str}public sealed partial record Unknown(UnknownCase Value) : {type_name};"
851 )
852 lines.append("")
853
854 for field in union.fields:
855 case_name = self._union_case_type_name(field)
856 case_type = self.generate_type(
857 field.field_type,
858 nullable=False,
859 parent_stack=parent_stack,
860 )
861 schema_type = self._schema_type_hint(field.field_type)
862 if schema_type:
863 lines.append(
864 f"{ind}{self.indent_str}[ForyCase({field.number}, Type = typeof({schema_type}))]"
865 )
866 else:
867 lines.append(f"{ind}{self.indent_str}[ForyCase({field.number})]")
868 lines.append(
869 f"{ind}{self.indent_str}public sealed partial record {case_name}({case_type} Value) : {type_name};"
870 )
871 lines.append("")
872
873 lines.append(f"{ind}{self.indent_str}public byte[] ToBytes()")
874 lines.append(f"{ind}{self.indent_str}{{")
875 lines.append(
876 f"{ind}{self.indent_str * 2}return {module_class}.GetFory().Serialize(this);"
877 )
878 lines.append(f"{ind}{self.indent_str}}}")
879 lines.append("")
880 lines.append(
881 f"{ind}{self.indent_str}public static {type_name} FromBytes(byte[] data)"
882 )

Callers 2

generate_fileMethod · 0.95
generate_messageMethod · 0.95

Calls 8

safe_type_identifierMethod · 0.95
get_module_class_nameMethod · 0.95
_union_case_type_nameMethod · 0.95
generate_typeMethod · 0.95
_schema_type_hintMethod · 0.95
appendMethod · 0.45

Tested by

no test coverage detected