Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ SOURCE_FILES = \
src/ocispec/basic_test_top_double_array_int.c \
src/ocispec/basic_test_top_double_array_obj.c \
src/ocispec/basic_test_top_double_array_refobj.c \
src/ocispec/basic_test_top_double_array_string.c
src/ocispec/basic_test_top_double_array_string.c \
src/ocispec/basic_test_map_string_string_array.c

HEADER_FILES = $(SOURCE_FILES:.c=.h)

Expand Down Expand Up @@ -137,14 +138,16 @@ src/ocispec/basic_test_double_array_item.h \
src/ocispec/basic_test_top_double_array_obj.h \
src/ocispec/basic_test_top_double_array_refobj.h \
src/ocispec/basic_test_top_double_array_string.h \
src/ocispec/basic_test_map_string_string_array.h \
src/ocispec/basic_test_double_array_item.c \
src/ocispec/basic_test_double_array.c \
src/ocispec/basic_test_top_array_int.c \
src/ocispec/basic_test_top_array_string.c \
src/ocispec/basic_test_top_double_array_int.c \
src/ocispec/basic_test_top_double_array_obj.c \
src/ocispec/basic_test_top_double_array_refobj.c \
src/ocispec/basic_test_top_double_array_string.c: src/basic-test_stamp
src/ocispec/basic_test_top_double_array_string.c \
src/ocispec/basic_test_map_string_string_array.c: src/basic-test_stamp

$(HEADER_FILES): %.h: %.c src/ocispec/generate.py

Expand Down Expand Up @@ -216,6 +219,9 @@ tests_test_12_LDADD = $(TESTS_LDADD)
tests_test_13_SOURCES = tests/test-13.c
tests_test_13_LDADD = $(TESTS_LDADD)

tests_test_14_SOURCES = tests/test-14.c
tests_test_14_LDADD = $(TESTS_LDADD)

src_ocispec_validate_SOURCES = src/ocispec/validate.c
src_ocispec_validate_LDADD = $(TESTS_LDADD)

Expand All @@ -231,7 +237,8 @@ TESTS = tests/test-1 \
tests/test-10 \
tests/test-11 \
tests/test-12 \
tests/test-13
tests/test-13 \
tests/test-14

noinst_PROGRAMS = src/ocispec/validate $(TESTS)

Expand Down Expand Up @@ -260,6 +267,8 @@ EXTRA_DIST = autogen.sh \
tests/data/top_double_array_obj.json \
tests/data/top_double_array_refobj.json \
tests/data/top_double_array_string.json \
tests/data/config-netdevices.json \
tests/data/map-string-string-array.json \
tests/test-spec \
src/ocispec/generate.py \
src/ocispec/headers.py \
Expand Down
99 changes: 90 additions & 9 deletions src/ocispec/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,19 @@ def emit_clone_body(self, c_file, obj, prefix):
if (src->{i.fixname})
{{
size_t i;
ret->len = src->len;
ret->keys = calloc (src->len + 1, sizeof (*ret->keys));
if (ret->keys == NULL)
return NULL;
for (i = 0; i < src->len; i++)
{{
ret->keys[i] = strdup (src->keys[i]);
if (ret->keys[i] == NULL)
return NULL;
}}
ret->{i.fixname} = calloc (src->len + 1, sizeof (*ret->{i.fixname}));
if (ret->{i.fixname} == NULL)
return NULL;
for (i = 0; i < src->len; i++)
{{
ret->{i.fixname}[i] = clone_{node_name} (src->{i.fixname}[i]);
Expand Down Expand Up @@ -1393,21 +1405,22 @@ def emit_parse(self, c_file, obj, prefix, obj_typename):
if obj.nested_array:
emit(c_file, f'''
yajl_val *items = YAJL_GET_ARRAY_NO_CHECK(tmp)->values;
ret->{obj.fixname} = calloc ( YAJL_GET_ARRAY_NO_CHECK(tmp)->len + 1, sizeof (*ret->{obj.fixname}));
ret->{obj.fixname}_len = YAJL_GET_ARRAY_NO_CHECK(tmp)->len;
ret->{obj.fixname} = calloc (ret->{obj.fixname}_len + 1, sizeof (*ret->{obj.fixname}));
''', indent=4)
null_check_return(c_file, f'ret->{obj.fixname}[i]', indent=4)
emit(c_file, '''
null_check_return(c_file, f'ret->{obj.fixname}', indent=4)
emit(c_file, f'''
size_t j;
for (j = 0; j < YAJL_GET_ARRAY_NO_CHECK(tmp)->len; j++)
{
char *str = YAJL_GET_STRING (itmes[j]);
for (j = 0; j < ret->{obj.fixname}_len; j++)
{{
char *str = YAJL_GET_STRING (items[j]);
''', indent=4)
emit(c_file, f'''
ret->{obj.fixname}[j] = (uint8_t *)strdup (str ? str : "");
''', indent=5)
null_check_return(c_file, f'ret->{obj.fixname}[j]', indent=5)
emit(c_file, '''
};
}
''', indent=5)
else:
emit(c_file, '''
Expand Down Expand Up @@ -1446,7 +1459,7 @@ def emit_generate(self, c_file, obj, prefix):
{{
if (ptr->{obj.fixname}[i] != NULL)
str = (const char *)ptr->{obj.fixname}[i];
else ()
else
str = "";
stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)str, strlen(str));
}}
Expand Down Expand Up @@ -1683,6 +1696,62 @@ def _emit_clone_string(self, c_file, obj, indent):
class BasicMapArrayHandler(ArraySubtypeHandler):
"""Handler for arrays of basic map types."""

def emit_parse(self, c_file, obj, prefix, obj_typename):
map_func = helpers.make_basic_map_name(obj.subtyp)
emit(c_file, f'''
do
{{
yajl_val tmp = get_val (tree, "{obj.origname}", yajl_t_array);
if (tmp != NULL && YAJL_GET_ARRAY (tmp) != NULL)
{{
size_t i;
size_t len = YAJL_GET_ARRAY_NO_CHECK (tmp)->len;
yajl_val *values = YAJL_GET_ARRAY_NO_CHECK (tmp)->values;
ret->{obj.fixname}_len = len;
''', indent=1)
calloc_with_check(c_file, f'ret->{obj.fixname}', 'len + 1', f'*ret->{obj.fixname}', indent=3)
emit(c_file, f'''
for (i = 0; i < len; i++)
{{
yajl_val val = values[i];
ret->{obj.fixname}[i] = make_{map_func} (val, ctx, err);
if (ret->{obj.fixname}[i] == NULL)
return NULL;
}}
}}
}}
while (0);
''', indent=1)

def emit_generate(self, c_file, obj, prefix):
map_func = helpers.make_basic_map_name(obj.subtyp)
emit(c_file, f'''
if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->{obj.fixname} != NULL))
{{
size_t len = 0, i;
''', indent=1)
emit_gen_key_with_check(c_file, obj.origname, indent=2)
emit(c_file, f'''
if (ptr != NULL && ptr->{obj.fixname} != NULL)
len = ptr->{obj.fixname}_len;
''', indent=2)
emit_beautify_off(c_file, '!len', indent=2)
emit_gen_array_open(c_file, indent=2)
check_gen_status(c_file, indent=2)
emit(c_file, f'''
for (i = 0; i < len; i++)
{{
stat = gen_{map_func} (g, ptr->{obj.fixname}[i], ctx, err);
if (stat != yajl_gen_status_ok)
GEN_SET_ERROR_AND_RETURN (stat, err);
}}
''', indent=2)
emit_gen_array_close(c_file, indent=2)
emit_beautify_on(c_file, '!len', indent=2)
emit(c_file, '''
}
''', indent=1)

def emit_free(self, c_file, obj, prefix):
free_func = helpers.make_basic_map_name(obj.subtyp)
emit(c_file, f'''
Expand All @@ -1703,6 +1772,18 @@ def emit_free(self, c_file, obj, prefix):
}
''', indent=1)

def emit_clone(self, c_file, obj, prefix, indent):
# Clone function doesn't use json_ prefix
clone_func = helpers.make_basic_map_name(obj.subtyp).replace('json_', '', 1)
emit(c_file, f'''
if (src->{obj.fixname}[i] != NULL)
{{
ret->{obj.fixname}[i] = clone_{clone_func} (src->{obj.fixname}[i]);
if (ret->{obj.fixname}[i] == NULL)
return NULL;
}}
''', indent=indent)


def _get_array_subtype_handler(obj):
"""Get the appropriate handler for an array's element type."""
Expand Down Expand Up @@ -2372,7 +2453,7 @@ def get_c_epilog_for_array_make_gen(c_file, prefix, typ, obj):
{
if (ptr->items[i] != NULL)
str = (const char *)ptr->items[i];
else ()
else
str = "";
stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *)str, strlen(str));
}
Expand Down
17 changes: 17 additions & 0 deletions tests/data/config-netdevices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"ociVersion": "1.0.0",
"root": {
"path": "rootfs"
},
"linux": {
"netDevices": {
"eth0": {
"name": "container_eth0"
},
"ens4": {
"name": "container_ens4"
},
"lo": {}
}
}
}
13 changes: 13 additions & 0 deletions tests/data/map-string-string-array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"maps": [
{
"key1": "value1",
"key2": "value2"
},
{
"foo": "bar",
"baz": "qux"
},
{}
]
}
Loading
Loading