Skip to content

Commit

Permalink
generate.py: store separately if an element is present
Browse files Browse the repository at this point in the history
make it clear whether a numeric or a bool are specified in the JSON
file.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Jul 20, 2019
1 parent 957c2c0 commit f6c15f2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ def generate_C_json(obj, c_file, prefix):
c_file.write(" }\n")

elif is_numeric_type(i.typ):
c_file.write(' if (ptr->%s) {\n' % i.fixname)
c_file.write(' if (ptr->%s_present) {\n' % i.fixname)
c_file.write(' stat = reformat_map_key (g, (unsigned char *)"%s", %s);\n' % (i.origname, strlen(i.origname)))
c_file.write(" if (!stat)\n")
c_file.write(" return false;\n")
json_value_generator(c_file, 2, "ptr->%s" % i.fixname, 'g', i.typ)
c_file.write(" }\n")

elif i.typ == 'boolean':
c_file.write(' if (ptr->%s) {\n' % i.fixname)
c_file.write(' if (ptr->%s_present) {\n' % i.fixname)
c_file.write(' stat = reformat_map_key (g, (unsigned char *)"%s", %s);\n' % (i.origname, strlen(i.origname)))
c_file.write(" if (!stat)\n")
c_file.write(" return false;\n")
Expand Down Expand Up @@ -440,15 +440,21 @@ def read_value_generator(c_file, level, src, dest, typ):
c_file.write('%s}\n' % (' ' * level))
elif is_numeric_type(typ):
c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src))
c_file.write('%sif (val)\n' % (' ' * (level)))
c_file.write('%sif (val) {\n' % (' ' * (level)))
if typ.startswith("uint"):
c_file.write('%s%s = strtoull (YAJL_GET_NUMBER (val), NULL, 10);\n' % (' ' * (level + 1), dest))
else:
c_file.write('%s%s = strtoll (YAJL_GET_NUMBER (val), NULL, 10);\n' % (' ' * (level + 1), dest))
if '[' not in dest:
c_file.write('%s%s_present = 1;\n' % (' ' * (level + 1), dest))
c_file.write('%s}\n' % (' ' * (level)))
elif typ == 'boolean':
c_file.write('%syajl_val val = %s;\n' % (' ' * (level), src))
c_file.write('%sif (val)\n' % (' ' * (level)))
c_file.write('%sif (val) {\n' % (' ' * (level)))
c_file.write('%s%s = YAJL_IS_TRUE (val);\n' % (' ' * (level + 1), dest))
if '[' not in dest:
c_file.write('%s%s_present = 1;\n' % (' ' * (level + 1), dest))
c_file.write('%s}\n' % (' ' * (level)))

def json_value_generator(c_file, level, src, dst, typ):
if typ == 'mapStringString':
Expand Down Expand Up @@ -582,6 +588,11 @@ def append_type_C_header(obj, header, prefix):
else:
c_typ = make_pointer(i.name, i.typ, prefix) or c_types_mapping[i.typ]
header.write(" %s%s%s;\n" % (c_typ, " " if '*' not in c_typ else "", i.fixname))

for i in obj.subtypobj:
if is_numeric_type(i.typ) or i.typ == 'boolean':
header.write(" unsigned int %s_present : 1;\n" % (i.fixname))

typename = make_name_array(obj.name, prefix)
header.write("}\n%s;\n\n" % typename)
header.write("void free_%s (%s *ptr);\n\n" % (typename, typename))
Expand All @@ -606,6 +617,10 @@ def append_type_C_header(obj, header, prefix):
c_typ = make_pointer(i.name, i.typ, prefix) or c_types_mapping[i.typ]
header.write(" %s%s%s;\n\n" % (c_typ, " " if '*' not in c_typ else "", i.fixname))

for i in (obj.children or []):
if is_numeric_type(i.typ) or i.typ == 'boolean':
header.write(" unsigned int %s_present : 1;\n" % (i.fixname))

typename = make_name(obj.name, prefix)
header.write("}\n%s;\n\n" % typename)
header.write("void free_%s (%s *ptr);\n\n" % (typename, typename))
Expand Down
3 changes: 1 addition & 2 deletions tests/data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"width": 0
},
"user": {
"uid": 101,
"gid": 0
"uid": 101
},
"args": [
"ARGS1",
Expand Down
4 changes: 4 additions & 0 deletions tests/test-1.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ main (int argc, char *argv[])
exit (5);
if (container->process->user->uid != 101 || container_gen->process->user->uid != 101)
exit (5);
if (!container->process->terminal_present)
exit (5);
if (!container->process->user->uid_present || container_gen->process->user->gid_present)
exit (5);
if (strcmp (container->process->args[0], "ARGS1") && strcmp (container->process->args[0], container_gen->process->args[0]))
exit (5);
if (strcmp (container->mounts[0]->destination, "/proc") && strcmp (container->mounts[0]->destination, container_gen->mounts[0]->destination))
Expand Down

0 comments on commit f6c15f2

Please sign in to comment.