Skip to content

Commit bc0fa04

Browse files
committed
update processing the field type
Update processing the field type in accordance with the same in tarantool. Part of #151
1 parent cf264b6 commit bc0fa04

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/tarantool_schema.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,49 @@ 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_INTEGER] = */ "integer",
207+
/* [FIELD_TYPE_BOOLEAN] = */ "boolean",
208+
/* [FIELD_TYPE_SCALAR] = */ "scalar",
209+
/* [FIELD_TYPE_ARRAY] = */ "array",
210+
/* [FIELD_TYPE_MAP] = */ "map",
211+
};
212+
213+
/**
214+
* Find a string in an array of strings.
215+
*/
216+
static uint32_t
217+
strnindex(const char **haystack, const char *needle, uint32_t len, uint32_t hmax)
218+
{
219+
if (len == 0)
220+
return hmax;
221+
for (unsigned index = 0; index != hmax && haystack[index]; ++index) {
222+
if (strncasecmp(haystack[index], needle, len) == 0 &&
223+
strlen(haystack[index]) == len)
224+
return index;
211225
}
212-
return FT_OTHER;
226+
return hmax;
227+
}
228+
229+
static enum field_type
230+
field_type_by_name(const char *name, size_t len)
231+
{
232+
enum field_type field_type = strnindex(field_type_strs, name, len,
233+
field_type_MAX);
234+
if (field_type != field_type_MAX)
235+
return field_type;
236+
/* 'num' and 'str' in _index are deprecated since Tarantool 1.7 */
237+
if (strncasecmp(name, "num", len) == 0)
238+
return FIELD_TYPE_UNSIGNED;
239+
else if (strncasecmp(name, "str", len) == 0)
240+
return FIELD_TYPE_STRING;
241+
else if (len == 1 && name[0] == '*')
242+
return FIELD_TYPE_ANY;
243+
return field_type_MAX;
213244
}
214245

215246
static int
@@ -231,7 +262,7 @@ parse_schema_space_value_value(struct schema_field_value *fld,
231262
if (mp_typeof(**tuple) != MP_STR)
232263
goto error;
233264
sfield = mp_decode_str(tuple, &sfield_len);
234-
fld->field_type = parse_field_type(sfield, sfield_len);
265+
fld->field_type = field_type_by_name(sfield, sfield_len);
235266
} else {
236267
mp_next(tuple);
237268
}
@@ -293,7 +324,7 @@ decode_index_parts_166(struct schema_field_value *parts, uint32_t part_count,
293324
return -1;
294325
uint32_t len;
295326
const char *str = mp_decode_str(data, &len);
296-
part->field_type = parse_field_type(str, len);
327+
part->field_type = field_type_by_name(str, len);
297328

298329
for (uint32_t j = 2; j < item_count; ++j)
299330
mp_next(data);

src/tarantool_schema.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,20 @@ 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_INTEGER,
21+
FIELD_TYPE_BOOLEAN,
22+
FIELD_TYPE_SCALAR,
23+
FIELD_TYPE_ARRAY,
24+
FIELD_TYPE_MAP,
25+
field_type_MAX
1626
};
1727

1828
#define COLL_NONE UINT32_MAX

0 commit comments

Comments
 (0)