Skip to content

Commit

Permalink
Attempt to translate conditional enum names if possible
Browse files Browse the repository at this point in the history
Internally apteryx uses numeric values for YANG enums. This change
attempts to convert an enum name used in a conditional (when or must)
clause into a numeric value if possible before evaluating the condition.
  • Loading branch information
gcampbell512 authored and carlgsmith committed Oct 15, 2024
1 parent c9c9fca commit a828e02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions models/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<NODE name="*" mode="rw" help="List of houses"/>
</NODE>
</NODE>
<NODE name="cages" when="../type = 'big'">
<NODE name="cage" help="This is a leaf list of pet houses">
<NODE name="*" mode="rw" help="List of houses"/>
</NODE>
</NODE>
<NODE name="claws" when="../../wombat" mode="rw" help="claws per paw"/>
<NODE name="friend" must="../../cat" mode="rw" help="animal friend"/>
<NODE name="n-type" when="derived-from-or-self(./type, 'a-types:type')" mode="rw" help="animal n-type"/>
Expand Down
22 changes: 20 additions & 2 deletions sch_conditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct _cond_result
char *path;
char *value;
char *step_value; /* actual value of step if step_exists has been called */
sch_instance *instance;
} cond_result;

typedef enum
Expand Down Expand Up @@ -125,6 +126,12 @@ sch_step_exists_traverse_nodes (GNode *node, gpointer data)
if (APTERYX_HAS_VALUE (node))
{
exists->value = g_strdup (APTERYX_VALUE (node));
sch_node *s_node = sch_lookup (exists->instance, path);
if (s_node)
{
/* We now have the schema node for the value, see if any translation is needed */
exists->value = sch_translate_from (s_node, exists->value);
}
}

/* Returning true stops the tree traverse */
Expand All @@ -136,11 +143,12 @@ sch_step_exists_traverse_nodes (GNode *node, gpointer data)
}

static void
sch_step_exists (GNode *root, char *path, cond_result *presult)
sch_step_exists (sch_instance *instance, GNode *root, char *path, cond_result *presult)
{
cond_result exists = { };

exists.path = path;
exists.instance = instance;
g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1,
sch_step_exists_traverse_nodes, &exists);
presult->result = exists.result;
Expand Down Expand Up @@ -211,6 +219,16 @@ sch_process_operator (sch_instance *instance, GNode *root, char *path,
else if (xnode->left->type == XPATH_TYPE_STEP)
{
/* We already looked up step value and saved it. */
if (rresult.result && rresult.value)
{
/* The left node retains the path to the node with the value */
sch_node *s_node = sch_lookup (instance, lresult.value);
if (s_node)
{
/* We now have the schema node for the value, see if any translation is needed */
rresult.value = sch_translate_from (s_node, rresult.value);
}
}
g_free (lresult.value);
lresult.value = lresult.step_value;
lresult.step_value = NULL;
Expand Down Expand Up @@ -704,7 +722,7 @@ sch_process_xnode (sch_instance *instance, GNode *root, char *path, char *step_p
if (presult->result)
{
/* This tests if a path exists, and gets its value */
sch_step_exists (root, presult->value, &result);
sch_step_exists (instance, root, presult->value, &result);
presult->result = result.result;
g_free (presult->step_value);
presult->step_value = result.value;
Expand Down

0 comments on commit a828e02

Please sign in to comment.