def generate_java_main_method_lines(self) -> List[str]: lines = [indent(1) + "public static void main(String[] args) {"] for line in self.generate_java_example_lines(): lines.append(indent(2) + line) lines.append(indent(1) + "}") lines.append('') return lines
def generate_java_getter_lines(self, field: str, t: Type) -> List[str]: getter_lines = [ indent(1) + f"public {t.to_java} get{field[0].upper()}{field[1:]}() {{", indent(2) + f'return this.{field};', indent(1) + '}', '' ] return getter_lines
def generate_c_example_code_lines(self) -> List[str]: lines = [] example_lines = ["int main() {"] for line in self.generate_c_example_lines(): example_lines.append(indent(1) + line) example_lines += [indent(1) + "return 0;", '}'] lines += example_lines return lines
def generate_java_setter_lines(self, field: str, t: Type) -> List[str]: setter_lines = [ indent(1) + f"public void set{field[0].upper()}{field[1:]}({t.to_java} {field}) {{", indent(2) + f'this.{field} = {field};', indent(1) + '}', '' ] return setter_lines
def generate_python_main_function_lines(self) -> List[str]: main_function_lines = ['', 'def main():'] for line in self.generate_python_example_lines(): main_function_lines.append(indent(1) + line) main_function_lines.append('') main_function_lines.append("""if __name__ == '__main__':""") main_function_lines.append(indent(1) + "main()") return main_function_lines
def generate_java_constructor_lines(self) -> List[str]: constructor_lines = [indent(1) + f"public {self.java_name}("] for field, t in self.get_java_fields().items(): constructor_lines[0] += f"{t.to_java} {field}, " constructor_lines.append(indent(2) + f"this.{field} = {field};") # Remove trailing ", " and close signature / open body constructor_lines[0] = constructor_lines[0][:-2] + ") {" constructor_lines.append(indent(1) + "}") return constructor_lines
def generate_python_from_dict_classmethod_lines(self) -> List[str]: from_dict_lines = ['', indent(1) + '@classmethod', indent(1) + "def from_dict(cls, d: dict):"] string_body = indent(2) + f"return cls(" if self.fields: for field, t in self.get_python_fields().items(): string_body += f"{field}={t.to_python_from_dict_value()}, " string_body = string_body.rstrip(", ") + ")" from_dict_lines.append(string_body) return from_dict_lines
def generate_python_constructor_lines(self) -> List[str]: constructor_lines = [indent(1) + "def __init__(self, "] if self.fields: for field, t in self.get_python_fields().items(): constructor_lines[0] += f"{field}: {t.to_python}, " constructor_lines.append(indent(2) + f"self.{field} = {field}") else: constructor_lines.append(indent(2) + "pass") # Remove trailing ", " and close signature / open body constructor_lines[0] = constructor_lines[0].rstrip(', ') + "):" return constructor_lines
def generate_python_repr_method_lines(self) -> List[str]: repr_lines = ['', indent(1) + "def __repr__(self):"] if self.fields: repr_lines.append(indent(2) + f"return f\"{self.python_name}(\" \\") for field, t in self.get_python_fields().items(): # 7 is the number of spaces in "return " repr_lines.append(indent(1) + " " * 7 + f"f\"{field}={{self.{field}!r}}, \" \\") repr_lines[-1] = repr_lines[-1].rstrip(", \" \\") + ")\"" else: repr_lines.append(indent(2) + f"return f\"{self.python_name}()\"") return repr_lines
def generate_c_constructor_lines(self) -> List[str]: lines = [] constructor_signature = f"{self.c_name}* {self.c_name}_new(" for field, t in self.get_c_fields().items(): constructor_signature += f"{t.to_c} {field}{'[]' if t.c_is_variable_length_array else ''}, " constructor_signature = constructor_signature.rstrip(", ") + ") {" lines.append(constructor_signature) lines.append(indent(1) + f"{self.c_name}* p = malloc(sizeof({self.c_name}));") for field, t in self.get_c_fields().items(): lines.append(indent(1) + f"p->{field} = {field};") lines += [indent(1) + "return p;", '}'] lines.append('') return lines
def generate_python_to_dict_method_lines(self) -> List[str]: to_dict_lines = ['', indent(1) + "def to_dict(self) -> dict:"] if self.fields: first_item_prefix = indent(2) + "return {" other_item_prefix = indent(2) + " " for field, t in self.get_python_fields().items(): k, v = t.to_python_to_dict_pair(field) to_dict_lines.append(f"{first_item_prefix or other_item_prefix}{k!r}: {v}, ") first_item_prefix = "" to_dict_lines[-1] = to_dict_lines[-1].rstrip(", ") + "}" else: to_dict_lines.append(indent(2) + "return {}") return to_dict_lines
def generate_java_to_string_method_lines(self) -> List[str]: lines = [indent(1) + "public String toString() {"] string_body = indent(2) + f'return "{self.java_name}(' if self.fields: for field, t in self.get_java_fields().items(): if isinstance(t, Array): string_body += f'{field}=" + Arrays.toString(this.{field}) + ", ' else: string_body += f'{field}=" + this.{field} + ", ' string_body = string_body.rstrip(', ') string_body += ')";' lines.append(string_body) lines.append(indent(1) + "}") lines.append('') return lines
def generate_c_struct_lines(self) -> List[str]: lines = [f"struct {self.c_name} {{"] for field, t in self.get_c_fields().items(): lines.append(indent(1) + f"{t.to_c} {'* ' if t.c_is_variable_length_array else ''}{field};") lines.append("};") lines.append(f"typedef struct {self.c_name} {self.c_name};") lines.append('') return lines
def generate_c_struct_print_function(self) -> List[str]: lines = [f"void {self.c_name}_print({self.c_name}* p) {{"] print_statements = [indent(1) + f"printf_s(\"{self.c_name}(\");"] for field, t in self.get_c_fields().items(): print_statements.append(indent(1) + t.to_c_printf(field)) if print_statements[-1].count('",') == 1: print_statements[-1] = print_statements[-1].replace('",', ', ",') else: print_statements.append(indent(1) + 'printf_s(", ");') if self.fields: if print_statements[-1] == indent(1) + 'printf_s(", ");': print_statements.pop() else: print_statements[-1] = print_statements[-1].replace(', ",', '",') print_statements.append(indent(1) + 'printf_s(")");') lines += print_statements lines += ["}", ""] return lines
def generate_go_struct_lines(self) -> List[str]: lines = [] lines.append(f"type {self.go_name} struct {{") struct_lines = [] for field, t in self.get_go_fields().items(): struct_lines.append(indent(1) + f"{field} {t.to_go} `json:\"{t.original_name}\"`") # TODO: Scope lines += struct_lines lines.append("}") lines.append('') return lines
def generate_go_constructor_lines(self) -> List[str]: lines = [] constructor_signature = f"func New{self.go_name}(" constructor_return = indent(1) + f"return &{self.go_name}{{" for field, t in self.get_go_fields().items(): lower_field_name = any_to_lower_camel(field) constructor_signature += f"{lower_field_name} {t.to_go}, " constructor_return += f"{field}: {lower_field_name}, " constructor_signature = constructor_signature.rstrip(", ") + f") *{self.go_name} {{" constructor_return = constructor_return.rstrip(", ") + "}" lines.append(constructor_signature) lines.append(constructor_return) lines.append("}") return lines
def generate_python_from_json_classmethod_lines(self) -> List[str]: from_json_lines = ['', indent(1) + '@classmethod', indent(1) + "def from_json(cls, data: str):", indent(2) + "return cls.from_dict(json.loads(data))"] return from_json_lines
def generate_java_field_lines(self) -> List[str]: field_lines = [] for field, t in self.get_java_fields().items(): field_lines.append(indent(1) + f"private {t.to_java} {field};") return field_lines
def generate_python_to_json_method_lines(self) -> List[str]: to_json_lines = ['', indent(1) + "def to_json(self) -> str:", indent(2) + "return json.dumps(self.to_dict())"] return to_json_lines