Beispiel #1
0
    def gen_field_map_assign_stmt(self, prefix, typename, name, row_name,
                                  map_delims, tabs):
        assert len(map_delims) == 2, map_delims
        delim1 = map_delims[0].strip()
        if delim1 == '\\':
            delim1 = '\\\\'
        delim2 = map_delims[1].strip()
        if delim2 == '\\':
            delim2 = '\\\\'

        space = self.TAB_SPACE * tabs
        k, v = types.map_key_value_types(typename)
        key_type = lang.map_java_type(k)
        val_type = lang.map_java_type(v)

        content = '%sString[] tokens = %s.split("\\\\%s");\n' % (
            space, row_name, delim1)
        content += '%sfor(int i = 0; i < tokens.length; i++) {\n' % space
        content += '%s    String text = tokens[i];\n' % space
        content += '%s    if (text.isEmpty()) {\n' % space
        content += '%s        continue;\n' % space
        content += '%s    }\n' % space
        content += '%s    String[] item = text.split("\\\\%s");\n' % (space,
                                                                      delim2)
        prefix1 = '%s key' % key_type
        prefix2 = '%s value' % val_type
        content += self.gen_field_assgin_stmt(prefix1, key_type, 'item[0]',
                                              tabs + 1)
        content += self.gen_field_assgin_stmt(prefix2, val_type, 'item[1]',
                                              tabs + 1)
        content += '%s    %s%s.put(key, value);\n' % (space, prefix, name)
        content += '%s}\n' % space
        return content
Beispiel #2
0
    def gen_field_map_assign_stmt(self, prefix, typename, name, row_name,
                                  map_delims, tabs):
        assert len(map_delims) == 2, map_delims
        delim1 = map_delims[0].strip()
        if delim1 == '\\':
            delim1 = '\\\\'
        delim2 = map_delims[1].strip()
        if delim2 == '\\':
            delim2 = '\\\\'

        space = self.TAB_SPACE * tabs
        k, v = types.map_key_value_types(typename)
        key_type = lang.map_cs_type(k)
        val_type = lang.map_cs_type(v)

        content = "%svar items = %s.Split(new char[]{'%s'}, StringSplitOptions.RemoveEmptyEntries);\n" % (
            space, row_name, delim1)
        content += '%s%s%s = new Dictionary<%s,%s>();\n' % (
            space, prefix, name, key_type, val_type)
        content += "%sforeach(string text in items) {\n" % space
        content += '%s    if (text.Length == 0) {\n' % space
        content += '%s        continue;\n' % space
        content += '%s    }\n' % space
        content += "%s    var item = text.Split(new char[]{'%s'}, StringSplitOptions.RemoveEmptyEntries);\n" % (
            space, delim2)
        content += self.gen_field_assgin_stmt('var key', key_type, 'item[0]',
                                              tabs + 1)
        content += self.gen_field_assgin_stmt('var value', val_type, 'item[1]',
                                              tabs + 1)
        content += '%s    %s%s[key] = value;\n' % (space, prefix, name)
        content += '%s}\n' % space
        return content
Beispiel #3
0
    def gen_field_map_assgin_stmt(self, prefix, typename, name, row_name,
                                  map_delims, tabs):
        assert len(map_delims) == 2, map_delims
        delim1 = map_delims[0].strip()
        if delim1 == '\\':
            delim1 = '\\\\'
        delim2 = map_delims[1].strip()
        if delim2 == '\\':
            delim2 = '\\\\'

        k, v = types.map_key_value_types(typename)
        key_type = lang.map_cpp_type(k)
        val_type = lang.map_cpp_type(v)
        space = self.TAB_SPACE * (tabs + 1)
        content = '%s{\n' % (self.TAB_SPACE * tabs)
        content += '%sconst auto& mapitems = Split(%s, "%s", true);\n' % (
            space, row_name, delim1)
        content += '%sfor (size_t i = 0; i < mapitems.size(); i++)\n' % space
        content += '%s{\n' % space
        content += '%s    const auto& kv = Split(mapitems[i], "%s", true);\n' % (
            space, delim2)
        content += '%s    ASSERT(kv.size() == 2);\n' % space
        content += '%s    if(kv.size() == 2)\n' % space
        content += '%s    {\n' % space
        content += '%s        const auto& key = ParseValue<%s>(kv[0]);\n' % (
            space, key_type)
        content += '%s        ASSERT(%s%s.count(key) == 0);\n' % (space,
                                                                  prefix, name)
        content += '%s        %s%s[key] = ParseValue<%s>(kv[1]);\n' % (
            space, prefix, name, val_type)
        content += '%s    }\n' % space
        content += '%s}\n' % space
        content += '%s}\n' % (self.TAB_SPACE * tabs)
        return content
Beispiel #4
0
    def gen_field_map_assign_stmt(self, prefix, typename, name, row_name, map_delims, tabs):
        assert len(map_delims) == 2, map_delims
        delim1 = map_delims[0].strip()
        if delim1 == '\\':
            delim1 = '\\\\'
        delim2 = map_delims[1].strip()
        if delim2 == '\\':
            delim2 = '\\\\'

        space = self.TAB_SPACE * tabs
        k, v = types.map_key_value_types(typename)
        key_type = lang.map_go_type(k)
        val_type = lang.map_go_type(v)

        content = ''
        content += '%s%s%s = map[%s]%s{}\n' % (space, prefix, name, key_type, val_type)
        content += '%sfor _, text := range strings.Split(%s, "%s") {\n' % (space, row_name, delim1)
        content += '%s    if text == "" {\n' % space
        content += '%s        continue\n' % space
        content += '%s    }\n' % space
        content += '%s    var items = strings.Split(text, "%s")\n' % (space, delim2)
        content += '%s    var value = MustParseTextValue("%s", items[0], %s)\n' % (space, key_type, row_name)
        content += '%s    var key = value.(%s)\n' % (space, key_type)
        content += '%s    value = MustParseTextValue("%s", items[1], %s)\n' % (space, val_type, row_name)
        content += '%s    var val = value.(%s)\n' % (space, val_type)
        content += '%s    %s%s[key] = val\n' % (space, prefix, name)
        content += '%s}\n' % space
        return content
Beispiel #5
0
    def parse_value(self, struct, typename, text):
        array_delim = struct['options'].get(predef.OptionArrayDelimeter,
                                            predef.DefaultArrayDelimiter)
        map_delims = struct['options'].get(predef.OptionMapDelimeters,
                                           predef.DefaultMapDelimiters)
        abs_type = types.is_abstract_type(typename)
        if abs_type is None:
            return self.parse_primary_value(typename, text)

        if abs_type == 'array':
            elem_type = types.array_element_type(typename)
            return self.parse_to_array(elem_type, text, array_delim)
        elif abs_type == 'map':
            ktype, vtype = types.map_key_value_types(typename)
            return self.parse_to_dict(ktype, vtype, text, map_delims)