Skip to content

Commit 4c076ef

Browse files
LeonidVasTotktonada
authored andcommitted
schema: update processing the field type
Update processing the field type in accordance with the same in tarantool. Part of #151
1 parent dc7523f commit 4c076ef

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/tarantool_schema.c

+48-13
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,53 @@ schema_space_free(struct mh_schema_space_t *schema) {
198198
}
199199
}
200200

201-
int parse_field_type(const char *sfield, size_t sfield_len) {
202-
if (sfield_len == 3) {
203-
if (tolower(sfield[0]) == 's' &&
204-
tolower(sfield[1]) == 't' &&
205-
tolower(sfield[2]) == 'r')
206-
return FT_STR;
207-
if (tolower(sfield[0]) == 'n' &&
208-
tolower(sfield[1]) == 'u' &&
209-
tolower(sfield[2]) == 'm')
210-
return FT_NUM;
201+
static const char *field_type_strs[] = {
202+
/* [FIELD_TYPE_ANY] = */ "any",
203+
/* [FIELD_TYPE_UNSIGNED] = */ "unsigned",
204+
/* [FIELD_TYPE_STRING] = */ "string",
205+
/* [FIELD_TYPE_NUMBER] = */ "number",
206+
/* [FIELD_TYPE_DOUBLE] = */ "double",
207+
/* [FIELD_TYPE_INTEGER] = */ "integer",
208+
/* [FIELD_TYPE_BOOLEAN] = */ "boolean",
209+
/* [FIELD_TYPE_VARBINARY] = */"varbinary",
210+
/* [FIELD_TYPE_SCALAR] = */ "scalar",
211+
/* [FIELD_TYPE_DECIMAL] = */ "decimal",
212+
/* [FIELD_TYPE_UUID] = */ "uuid",
213+
/* [FIELD_TYPE_ARRAY] = */ "array",
214+
/* [FIELD_TYPE_MAP] = */ "map",
215+
};
216+
217+
/**
218+
* Find a string in an array of strings.
219+
*/
220+
static uint32_t
221+
strnindex(const char **haystack, const char *needle, uint32_t len, uint32_t hmax)
222+
{
223+
if (len == 0)
224+
return hmax;
225+
for (unsigned index = 0; index != hmax && haystack[index]; ++index) {
226+
if (strncasecmp(haystack[index], needle, len) == 0 &&
227+
strlen(haystack[index]) == len)
228+
return index;
211229
}
212-
return FT_OTHER;
230+
return hmax;
231+
}
232+
233+
static enum field_type
234+
field_type_by_name(const char *name, size_t len)
235+
{
236+
enum field_type field_type = strnindex(field_type_strs, name, len,
237+
field_type_MAX);
238+
if (field_type != field_type_MAX)
239+
return field_type;
240+
/* 'num' and 'str' in _index are deprecated since Tarantool 1.7 */
241+
if (strncasecmp(name, "num", len) == 0)
242+
return FIELD_TYPE_UNSIGNED;
243+
else if (strncasecmp(name, "str", len) == 0)
244+
return FIELD_TYPE_STRING;
245+
else if (len == 1 && name[0] == '*')
246+
return FIELD_TYPE_ANY;
247+
return field_type_MAX;
213248
}
214249

215250
static int
@@ -231,7 +266,7 @@ parse_schema_space_value_value(struct schema_field_value *fld,
231266
if (mp_typeof(**tuple) != MP_STR)
232267
goto error;
233268
sfield = mp_decode_str(tuple, &sfield_len);
234-
fld->field_type = parse_field_type(sfield, sfield_len);
269+
fld->field_type = field_type_by_name(sfield, sfield_len);
235270
} else {
236271
mp_next(tuple);
237272
}
@@ -293,7 +328,7 @@ decode_index_parts_166(struct schema_field_value *parts, uint32_t part_count,
293328
return -1;
294329
uint32_t len;
295330
const char *str = mp_decode_str(data, &len);
296-
part->field_type = parse_field_type(str, len);
331+
part->field_type = field_type_by_name(str, len);
297332

298333
for (uint32_t j = 2; j < item_count; ++j)
299334
mp_next(data);

src/tarantool_schema.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,25 @@ struct schema_key {
99
uint32_t number;
1010
};
1111

12+
/**
13+
* Possible field data types.
14+
*/
1215
enum field_type {
13-
FT_STR = 0,
14-
FT_NUM = 1,
15-
FT_OTHER = 2
16+
FIELD_TYPE_ANY = 0,
17+
FIELD_TYPE_UNSIGNED,
18+
FIELD_TYPE_STRING,
19+
FIELD_TYPE_NUMBER,
20+
FIELD_TYPE_DOUBLE,
21+
FIELD_TYPE_INTEGER,
22+
FIELD_TYPE_BOOLEAN,
23+
FIELD_TYPE_VARBINARY,
24+
FIELD_TYPE_SCALAR,
25+
FIELD_TYPE_DECIMAL,
26+
FIELD_TYPE_UUID,
27+
FIELD_TYPE_ARRAY,
28+
FIELD_TYPE_MAP,
29+
/* Used for unknown type. */
30+
field_type_MAX
1631
};
1732

1833
#define COLL_NONE UINT32_MAX

0 commit comments

Comments
 (0)