Пример #1
0
    def gen_java_class(self, struct):
        content = ''

        fields = struct['fields']
        if struct['options'][predef.PredefParseKVMode]:
            fields = structutil.get_struct_kv_fields(struct)

        content += '// %s, %s\n' % (struct['comment'], struct['file'])
        content += 'public class %s\n{\n' % struct['name']

        inner_class_done = False
        inner_typename = ''
        inner_var_name = ''
        inner_type_class = ''
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(
            struct)
        if len(inner_fields) > 0:
            content += self.gen_java_inner_class(struct)
            inner_type_class = struct["options"][predef.PredefInnerTypeClass]
            inner_var_name = struct["options"][predef.PredefInnerTypeName]

        vec_done = False
        vec_names, vec_name = structutil.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_java_type)
        if len(inner_typename) > max_type_len:
            max_type_len = len(inner_typename)

        for field in fields:
            if not field['enable']:
                continue
            text = ''
            field_name = field['name']
            if field_name in inner_field_names:
                if not inner_class_done:
                    typename = "ArrayList<>();"
                    text += '    public List<%s> %s = new %s \n' % (
                        inner_type_class, inner_var_name, typename)
                    inner_class_done = True
            else:
                typename = lang.map_java_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_java_value(field, typename)
                    name = strutil.pad_spaces(name, max_name_len + 8)
                    text += '    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)
                    text += '    public %s[] %s // %s\n' % (
                        typename.strip(), name, field['comment'])
                    vec_done = True
            content += text

        return content
Пример #2
0
    def gen_parse_method(self, struct):
        content = ''
        if struct['options'][predef.PredefParseKVMode]:
            return self.gen_kv_parse_method(struct)

        vec_idx = 0
        vec_names, vec_name = structutil.get_vec_field_range(struct)
        fields = structutil.enabled_fields(struct)

        inner_class_done = False
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(struct)

        content += '\n'
        content += '%s// parse object fields from a text row\n' % self.TAB_SPACE
        content += '%spublic void ParseFromRow(List<string> row)\n' % self.TAB_SPACE
        content += '%s{\n' % self.TAB_SPACE
        content += '%sif (row.Count < %d) {\n' % (self.TAB_SPACE*2, len(fields))
        content += '%sthrow new ArgumentException(string.Format("%s: row length too short {0}", row.Count));\n' % (
            self.TAB_SPACE * 3, struct['name'])
        content += '%s}\n' % (self.TAB_SPACE*2)

        idx = 0
        prefix = 'this.'
        for field in struct['fields']:
            if not field['enable']:
                continue
            text = ''
            field_name = field['name']
            if field_name in inner_field_names:
                if not inner_class_done:
                    inner_class_done = True
                    text += self.gen_cs_inner_class_assign(struct, prefix)
            else:
                origin_type_name = field['original_type_name']
                typename = lang.map_cs_type(origin_type_name)
                valuetext = 'row[%d]' % idx
                if origin_type_name.startswith('array'):
                    text += '%s{\n' % (self.TAB_SPACE * 2)
                    text += self.gen_field_array_assign_stmt(prefix, origin_type_name, field_name, valuetext, 3)
                    text += '%s}\n' % (self.TAB_SPACE * 2)
                elif origin_type_name.startswith('map'):
                    text += '%s{\n' % (self.TAB_SPACE * 2)
                    text += self.gen_field_map_assign_stmt(prefix, origin_type_name, field_name, valuetext, 3)
                    text += '%s}\n' % (self.TAB_SPACE * 2)
                else:
                    text += '%sif (row[%d].Length > 0) {\n' % (self.TAB_SPACE * 2, idx)
                    if field_name in vec_names:
                        name = '%s[%d]' % (vec_name, vec_idx)
                        text += self.gen_field_assign_stmt(prefix+name, typename, valuetext, 3)
                        vec_idx += 1
                    else:
                        text += self.gen_field_assign_stmt(prefix+field_name, typename, valuetext, 3)
                    text += '%s}\n' % (self.TAB_SPACE*2)
            idx += 1
            content += text
        content += '%s}\n' % self.TAB_SPACE
        return content
Пример #3
0
    def gen_cpp_struct_define(self, struct):
        content = '// %s\n' % struct['comment']
        content += 'struct %s \n{\n' % struct['name']

        inner_class_done = False
        inner_typename = ''
        inner_var_name = ''
        inner_field_names, mapped_inner_fields = structutil.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 = structutil.get_vec_field_range(struct)

        fields = struct['fields']
        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:
            if not field['enable']:
                continue
            text = ''
            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)
                    text += '    %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)
                    text += '    %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)
                    text += '    %s %s // %s\n' % (typename, name,
                                                   field['comment'])
                    vec_done = True
            content += text
        return content
Пример #4
0
    def gen_all_field_assign_stmt(self, struct, prefix, tabs):
        content = ''

        inner_class_done = False
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(
            struct)

        vec_names, vec_name = structutil.get_vec_field_range(struct)
        vec_idx = 0
        idx = 0
        space = self.TAB_SPACE * tabs
        for field in struct['fields']:
            if not field['enable']:
                continue
            text = ''
            field_name = field['name']
            assert idx >= 0
            if field_name in inner_field_names:
                if not inner_class_done:
                    inner_class_done = True
                    text += self.gen_inner_class_field_assgin_stmt(
                        struct, prefix)
            else:
                origin_type = field['original_type_name']
                typename = lang.map_cpp_type(origin_type)

                if typename != 'std::string' and field['name'] in vec_names:
                    text += '%s%s%s[%d] = %s;\n' % (
                        space, prefix, vec_name, vec_idx,
                        lang.default_value_by_cpp_type(origin_type))

                if origin_type.startswith('array'):
                    text += self.gen_field_array_assign_stmt(
                        prefix, origin_type, field_name, ('row[%d]' % idx),
                        tabs)
                elif origin_type.startswith('map'):
                    text += self.gen_field_map_assgin_stmt(
                        prefix, origin_type, field_name, ('row[%d]' % idx),
                        tabs)
                else:
                    if field['name'] in vec_names:
                        text += '%s%s%s[%d] = to<%s>(row[%d]);\n' % (
                            self.TAB_SPACE * tabs, prefix, vec_name, vec_idx,
                            typename, idx)
                        vec_idx += 1
                    else:
                        text += '%s%s%s = to<%s>(row[%d]);\n' % (
                            self.TAB_SPACE * tabs, prefix, field_name,
                            typename, idx)
            idx += 1
            content += text
        return content
Пример #5
0
    def gen_parse_method(self, struct):
        if struct['options'][predef.PredefParseKVMode]:
            return self.gen_kv_parse_method(struct)

        inner_class_done = False
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(
            struct)

        vec_idx = 0
        vec_names, vec_name = structutil.get_vec_field_range(struct)
        fields = structutil.enabled_fields(struct)

        content = ''
        content += 'func (p *%s) ParseFromRow(row []string) error {\n' % struct[
            'camel_case_name']
        content += '\tif len(row) < %d {\n' % len(fields)
        content += '\t\tlog.Panicf("%s: row length too short %%d", len(row))\n' % struct[
            'name']
        content += '\t}\n'

        idx = 0
        for field in struct['fields']:
            if not field['enable']:
                continue
            text = ''
            fname = field['name']
            prefix = 'p.'
            if fname in inner_field_names:
                if not inner_class_done:
                    inner_class_done = True
                    text += self.gen_inner_class_parse(struct, prefix)
            else:
                text += '\tif row[%d] != "" {\n' % idx
                origin_type_name = field['original_type_name']
                typename = lang.map_go_type(origin_type_name)
                field_name = field['camel_case_name']
                valuetext = 'row[%d]' % idx
                if origin_type_name.startswith('array'):
                    text += self.gen_field_array_assign_stmt(
                        prefix, field['original_type_name'], fname, valuetext,
                        2)
                elif origin_type_name.startswith('map'):
                    text += self.gen_field_map_assign_stmt(
                        prefix, field['original_type_name'], fname, valuetext,
                        2)
                else:
                    if field_name in vec_names:
                        name = '%s[%d]' % (vec_name, vec_idx)
                        text += self.gen_field_assign_stmt(
                            prefix + name, typename, valuetext, 2, 'row')
                        vec_idx += 1
                    else:
                        text += self.gen_field_assign_stmt(
                            prefix + field_name, typename, valuetext, 2, 'row')
                text += '%s}\n' % self.TAB_SPACE
            idx += 1
            content += text

        content += '%sreturn nil\n' % self.TAB_SPACE
        content += '}\n\n'
        return content
Пример #6
0
    def gen_parse_method(self, struct):
        content = ''
        if struct['options'][predef.PredefParseKVMode]:
            return self.gen_kv_parse_method(struct)

        vec_idx = 0
        vec_names, vec_name = structutil.get_vec_field_range(struct)

        inner_class_done = False
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(
            struct)
        fields = structutil.enabled_fields(struct)

        content += '%s// parse fields data from record\n' % self.TAB_SPACE
        content += '%spublic void parseFrom(CSVRecord record)\n' % self.TAB_SPACE
        content += '%s{\n' % self.TAB_SPACE
        content += '%sif (record.size() < %d) {\n' % (self.TAB_SPACE * 2,
                                                      len(fields))
        content += '%sthrow new RuntimeException(String.format("%s: record length too short %%d", record.size()));\n' % (
            self.TAB_SPACE * 3, struct['name'])
        content += '%s}\n' % (self.TAB_SPACE * 2)

        idx = 0
        prefix = 'this.'
        for field in struct['fields']:
            if not field['enable']:
                continue
            field_name = field['name']
            text = ''
            if field_name in inner_field_names:
                if not inner_class_done:
                    inner_class_done = True
                    content += self.gen_java_inner_class_assign(struct, prefix)
            else:
                origin_type_name = field['original_type_name']
                typename = lang.map_java_type(origin_type_name)
                valuetext = 'record.get(%d)' % idx
                if origin_type_name.startswith('array'):
                    text += '%s{\n' % (self.TAB_SPACE * 2)
                    text += self.gen_field_array_assign_stmt(
                        prefix, origin_type_name, field_name, valuetext, 3)
                    text += '%s}\n' % (self.TAB_SPACE * 2)
                elif origin_type_name.startswith('map'):
                    text += '%s{\n' % (self.TAB_SPACE * 2)
                    text += self.gen_field_map_assign_stmt(
                        prefix, origin_type_name, field_name, valuetext, 3)
                    text += '%s}\n' % (self.TAB_SPACE * 2)
                else:
                    text += '%sif (!record.get(%d).isEmpty()) {\n' % (
                        self.TAB_SPACE * 2, idx)
                    if field_name in vec_names:
                        name = '%s[%d]' % (vec_name, vec_idx)
                        text += self.gen_field_assign_stmt(
                            prefix + name, typename, valuetext, 3)
                        vec_idx += 1
                    else:
                        text += self.gen_field_assign_stmt(
                            prefix + field_name, typename, valuetext, 3)
                    text += '%s}\n' % (self.TAB_SPACE * 2)
            idx += 1
            content += text
        content += '%s}\n' % self.TAB_SPACE
        return content
Пример #7
0
    def gen_go_struct(self, struct, params):
        content = ''

        inner_class_done = False
        inner_typename = ''
        inner_var_name = ''
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(
            struct)
        if len(inner_fields) > 0:
            content += self.gen_go_inner_struct(struct, params.go_json_tag)
            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 = structutil.get_vec_field_range(struct)
        fields = struct['fields']

        content += '// %s, %s\n' % (struct['comment'], struct['file'])
        content += 'type %s struct {\n' % struct['camel_case_name']
        for field in fields:
            if not field['enable']:
                continue
            text = ''
            field_name = field['name']
            if field_name in inner_field_names:
                if not inner_class_done:
                    if params.go_json_tag:
                        if self.json_snake_case:
                            inner_var_name = strutil.camel_to_snake(
                                inner_var_name)
                        text += '    %s %s `json:"%s"` //\n' % (
                            strutil.camel_case(inner_var_name), inner_typename,
                            inner_var_name)
                    else:
                        text += '    %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 params.go_json_tag:
                        name = field['name']
                        if self.json_snake_case:
                            name = strutil.camel_to_snake(name)
                        text += '    %s %s `json:"%s"` // %s\n' % (
                            field['camel_case_name'], typename, name,
                            field['comment'])
                    else:
                        text += '    %s %s // %s\n' % (
                            field['camel_case_name'], typename,
                            field['comment'])
                elif not vec_done:
                    vec_done = True
                    if params.go_json_tag:
                        if self.json_snake_case:
                            vec_name = strutil.camel_to_snake(vec_name)
                        text += '    %s [%d]%s `json:"%s"` // %s\n' % (
                            strutil.camel_case(vec_name), len(vec_names),
                            typename, vec_name, field['comment'])
                    else:
                        text += '    %s [%d]%s // %s\n' % (
                            vec_name, len(vec_names), typename,
                            field['comment'])
            content += text

        return content