def gen_cs_struct(self, struct): content = '' fields = struct['fields'] if struct['options'][predef.PredefParseKVMode]: fields = genutil.get_struct_kv_fields(struct) inner_class_done = False inner_typename = '' inner_var_name = '' inner_field_names, inner_fields = genutil.get_inner_class_mapped_fields( struct) if len(inner_fields) > 0: content += self.gen_cs_inner_class(struct) inner_type_class = struct["options"][predef.PredefInnerTypeClass] inner_var_name = struct["options"][predef.PredefInnerTypeName] inner_typename = '%s[]' % inner_type_class content += '// %s, %s\n' % (struct['comment'], struct['file']) content += 'public class %s\n{\n' % struct['name'] vec_done = False vec_names, vec_name = genutil.get_vec_field_range(struct) max_name_len = strutil.max_field_length(fields, 'name', None) max_type_len = strutil.max_field_length(fields, 'original_type_name', lang.map_cs_type) if len(inner_typename) > max_type_len: max_type_len = len(inner_typename) for field in fields: field_name = field['name'] if field_name in inner_field_names: if not inner_class_done: typename = strutil.pad_spaces(inner_typename, max_type_len) content += ' public %s %s = null; \n' % (typename, inner_var_name) inner_class_done = True else: typename = lang.map_cs_type(field['original_type_name']) assert typename != "", field['original_type_name'] typename = strutil.pad_spaces(typename, max_type_len + 1) if field['name'] not in vec_names: name = lang.name_with_default_cs_value(field, typename) name = strutil.pad_spaces(name, max_name_len + 8) content += ' public %s %s // %s\n' % (typename, name, field['comment']) elif not vec_done: name = '%s = new %s[%d];' % (vec_name, typename.strip(), len(vec_names)) name = strutil.pad_spaces(name, max_name_len + 8) content += ' public %s[] %s // %s\n' % ( typename.strip(), name, field['comment']) vec_done = True return content
def gen_cpp_struct_define(self, struct): content = '// %s\n' % struct['comment'] content += 'struct %s \n{\n' % struct['name'] fields = struct['fields'] if struct['options'][predef.PredefParseKVMode]: fields = genutil.get_struct_kv_fields(struct) inner_class_done = False inner_typename = '' inner_var_name = '' inner_field_names, mapped_inner_fields = genutil.get_inner_class_mapped_fields( struct) if len(mapped_inner_fields) > 0: content += self.gen_inner_struct_define(struct) inner_type_class = struct["options"][predef.PredefInnerTypeClass] inner_var_name = struct["options"][predef.PredefInnerTypeName] inner_typename = 'std::vector<%s>' % inner_type_class vec_done = False vec_names, vec_name = genutil.get_vec_field_range(struct) max_name_len = strutil.max_field_length(fields, 'name', None) max_type_len = strutil.max_field_length(fields, 'original_type_name', lang.map_cpp_type) if len(inner_typename) > max_type_len: max_type_len = len(inner_typename) for field in fields: field_name = field['name'] if field_name in inner_field_names: if not inner_class_done: typename = strutil.pad_spaces(inner_typename, max_type_len + 1) name = strutil.pad_spaces(inner_var_name, max_name_len + 8) content += ' %s %s; //\n' % (typename, name) inner_class_done = True else: typename = lang.map_cpp_type(field['original_type_name']) assert typename != "", field['original_type_name'] typename = strutil.pad_spaces(typename, max_type_len + 1) if field_name not in vec_names: name = lang.name_with_default_cpp_value(field, typename) name = strutil.pad_spaces(name, max_name_len + 8) content += ' %s %s // %s\n' % (typename, name, field['comment']) elif not vec_done: name = '%s[%d];' % (vec_name, len(vec_names)) name = strutil.pad_spaces(name, max_name_len + 8) content += ' %s %s // %s\n' % (typename, name, field['comment']) vec_done = True return content
def gen_go_struct(self, struct, params): content = '' fields = struct['fields'] if struct['options'][predef.PredefParseKVMode]: fields = genutil.get_struct_kv_fields(struct) json_decorate = params.get(predef.OptionJsonDecorate, False) inner_class_done = False inner_typename = '' inner_var_name = '' inner_field_names, inner_fields = genutil.get_inner_class_mapped_fields( struct) if len(inner_fields) > 0: content += self.gen_go_inner_struct(struct, json_decorate) inner_type_class = struct["options"][predef.PredefInnerTypeClass] inner_var_name = struct["options"][predef.PredefInnerTypeName] inner_typename = '[]%s' % inner_type_class vec_done = False vec_names, vec_name = genutil.get_vec_field_range(struct) content += '// %s, %s\n' % (struct['comment'], struct['file']) content += 'type %s struct\n{\n' % struct['camel_case_name'] for field in fields: field_name = field['name'] if field_name in inner_field_names: if not inner_class_done: if json_decorate: content += ' %s %s `json:"%s"` //\n' % ( strutil.camel_case(inner_var_name), inner_typename, inner_var_name) else: content += ' %s %s //\n' % ( strutil.camel_case(inner_var_name), inner_typename) inner_class_done = True else: typename = lang.map_go_type(field['original_type_name']) assert typename != "", field['original_type_name'] if field_name not in vec_names: if json_decorate: content += ' %s %s `json:"%s"` // %s\n' % ( field['camel_case_name'], typename, field['comment'], field['name']) else: content += ' %s %s // %s\n' % ( field['camel_case_name'], typename, field['comment']) elif not vec_done: vec_done = True if json_decorate: content += ' %s [%d]%s `json:"%s"` // %s\n' % ( strutil.camel_case(vec_name), len(vec_names), typename, field['comment'], vec_name) else: content += ' %s [%d]%s // %s\n' % ( vec_name, len(vec_names), typename, field['comment']) return content