Skip to content

Commit

Permalink
Fix yaml bool handling.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <[email protected]>
  • Loading branch information
timrulebosch committed Feb 2, 2024
1 parent da5356f commit c54602c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
9 changes: 4 additions & 5 deletions dse/clib/util/yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,21 @@ DLL_PUBLIC const char** dse_yaml_get_array(
DLL_PUBLIC int dse_yaml_get_bool(YamlNode* node, const char* name, bool* value)
{
if (node == NULL && name == NULL && value == NULL) return EINVAL;
if (node->node_type != YAML_SCALAR_NODE) return EINVAL;
const char* _scalar = dse_yaml_get_scalar(node, name);
if (_scalar == NULL) return EINVAL;
/* True? */
const char* bool_true[] = { "y", "Y", "yes", "Yes", "YES", "true", "True",
"TRUE", "on", "On", "ON", NULL };
for (const char** p = bool_true; *p; p++) {
if (strcmp(*p, node->scalar)) continue;
if (strcmp(*p, _scalar)) continue;
*value = true;
return 0;
}
/* False? */
const char* bool_false[] = { "n", "N", "no", "No", "NO", "false", "False",
"FALSE", "off", "Off", "OFF", NULL };
for (const char** p = bool_false; *p; p++) {
if (strcmp(*p, node->scalar)) continue;
if (strcmp(*p, _scalar)) continue;
*value = false;
return 0;
}
Expand Down Expand Up @@ -267,8 +266,8 @@ DLL_PUBLIC int dse_yaml_get_int(YamlNode* node, const char* name, int* value)
/* Fallback to bool? */
bool _bool;
if (dse_yaml_get_bool(node, name, &_bool) == 0) {
value = NULL;
return EINVAL;
*value = _bool;
return 0;
}
return EINVAL;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ target_link_libraries(test_util
# -Wl,--wrap=strdup
)
set(YAML_EXAMPLE_RESOURCE_FILES
data/bool.yaml
data/uint.yaml
data/test.yaml
data/dict_dup.yaml
data/empty_doc.yaml
data/test.yaml
data/uint.yaml
data/values.yaml
)
install(TARGETS test_util)
install(
Expand Down
9 changes: 0 additions & 9 deletions tests/util/data/bool.yaml

This file was deleted.

21 changes: 10 additions & 11 deletions tests/util/test_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define FILE "util/data/values.yaml"
#define EMPTY_FILE "util/data/empty_doc.yaml"
#define UINT_FILE "util/data/uint.yaml"
#define BOOL_FILE "util/data/bool.yaml"
#define DICT_DUP_FILE "util/data/dict_dup.yaml"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
Expand Down Expand Up @@ -46,8 +45,8 @@ void test_yaml_get_uint(void** state)
/* Boolean cases. */
{ .node = "bools/numeric_true", .ev_uint = 1, .ex_rc = 0 },
{ .node = "bools/numeric_false", .ev_uint = 0, .ex_rc = 0 },
{ .node = "bools/lc_true", .ev_uint = 0, .ex_rc = 22 },
{ .node = "bools/lc_false", .ev_uint = 0, .ex_rc = 22 },
{ .node = "bools/lc_true", .ev_uint = 1, .ex_rc = 0 },
{ .node = "bools/lc_false", .ev_uint = 0, .ex_rc = 0 },
};

const char* a = FILE;
Expand Down Expand Up @@ -82,8 +81,8 @@ void test_yaml_get_int(void** state)
/* Boolean cases. */
{ .node = "bools/numeric_true", .ev_int = 1, .ex_rc = 0 },
{ .node = "bools/numeric_false", .ev_int = 0, .ex_rc = 0 },
{ .node = "bools/lc_true", .ev_int = 0, .ex_rc = 22 },
{ .node = "bools/lc_false", .ev_int = 0, .ex_rc = 22 },
{ .node = "bools/lc_true", .ev_int = 1, .ex_rc = 0 },
{ .node = "bools/lc_false", .ev_int = 0, .ex_rc = 0 },
};

const char* a = FILE;
Expand Down Expand Up @@ -181,11 +180,11 @@ void test_yaml_get_bool(void** state)

test_case tc[] = {
/* Good cases. */
{ .node = "lc_true", .ev_bool = 1, .ex_rc = 0 },
{ .node = "lc_false", .ev_bool = 0, .ex_rc = 0 },
{ .node = "bools/lc_true", .ev_bool = 1, .ex_rc = 0 },
{ .node = "bools/lc_false", .ev_bool = 0, .ex_rc = 0 },
/* Boolean Negative cases. */
{ .node = "numeric_true", .ev_bool = 0, .ex_rc = 22 },
{ .node = "numeric_false", .ev_bool = 0, .ex_rc = 22 },
{ .node = "bools/numeric_true", .ev_bool = 0, .ex_rc = 22 },
{ .node = "bools/numeric_false", .ev_bool = 0, .ex_rc = 22 },
/* Negative cases. */
{ .node = "integers/positive", .ev_bool = 0, .ex_rc = 22 },
{ .node = "integers/zero", .ev_bool = 0, .ex_rc = 22 },
Expand All @@ -202,8 +201,8 @@ void test_yaml_get_bool(void** state)
bool value;

log_debug("Testing node: %s", tc[i].node);
YamlNode* node = dse_yaml_find_node(doc, tc[i].node);
rc = dse_yaml_get_bool(node, tc[i].node, &value);
// YamlNode* node = dse_yaml_find_node(doc, tc[i].node);
rc = dse_yaml_get_bool(doc, tc[i].node, &value);
assert_int_equal(rc, tc[i].ex_rc);
assert_int_equal(value, tc[i].ev_bool);
}
Expand Down

0 comments on commit c54602c

Please sign in to comment.