Skip to content

Commit 2270d92

Browse files
committed
Added evaluation_order json serialization
Signed-off-by: Victor Moene <[email protected]>
1 parent 543b815 commit 2270d92

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

libpromises/eval_context.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,6 +4074,32 @@ void EvalContextSetAgentEvalOrder(EvalContext *ctx, EvalOrder eval_order)
40744074
ctx->agent_eval_order = eval_order;
40754075
}
40764076

4077+
const char *EvalContextEvaluationOrderToString(EvalOrder evaluation_order)
4078+
{
4079+
if (evaluation_order == EVAL_ORDER_CLASSIC)
4080+
{
4081+
return "classic";
4082+
}
4083+
if (evaluation_order == EVAL_ORDER_TOP_DOWN)
4084+
{
4085+
return "top_down";
4086+
}
4087+
return "undefined";
4088+
}
4089+
4090+
EvalOrder EvalContextEvaluationOrderFromString(const char *evaluation_order_string)
4091+
{
4092+
if (StringEqual(evaluation_order_string, "classic"))
4093+
{
4094+
return EVAL_ORDER_CLASSIC;
4095+
}
4096+
if (StringEqual(evaluation_order_string, "top_down"))
4097+
{
4098+
return EVAL_ORDER_TOP_DOWN;
4099+
}
4100+
return EVAL_ORDER_UNDEFINED;
4101+
}
4102+
40774103
bool EvalContextIsClassicOrder(EvalContext *ctx, const Bundle *bp)
40784104
{
40794105
assert(ctx != NULL);

libpromises/eval_context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ void EvalContextProfilingEnd(EvalContext *ctx, const Policy *policy);
454454

455455
void EvalContextSetCommonEvalOrder(EvalContext *ctx, EvalOrder eval_order);
456456
void EvalContextSetAgentEvalOrder(EvalContext *ctx, EvalOrder eval_order);
457+
const char *EvalContextEvaluationOrderToString(EvalOrder evaluation_order);
458+
EvalOrder EvalContextEvaluationOrderFromString(const char *evaluation_order_string);
457459
bool EvalContextIsClassicOrder(EvalContext *ctx, const Bundle *bp);
458460

459461
#endif

libpromises/expand.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ void BundleResolve(EvalContext *ctx, const Bundle *bundle)
844844
static void ResolveControlBody(EvalContext *ctx, GenericAgentConfig *config,
845845
const Body *control_body)
846846
{
847+
assert(control_body != NULL);
847848
const char *filename = control_body->source_path;
848849

849850
assert(CFG_CONTROLBODY[COMMON_CONTROL_MAX].lval == NULL);

libpromises/parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ static void ParserStateReset(ParserState *p, bool discard)
106106

107107
static void ParserStateClean(ParserState *p)
108108
{
109+
assert(p != NULL);
110+
109111
free(p->current_namespace);
110112
p->current_namespace = NULL;
111113

libpromises/policy.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ static JsonElement *BundleContextsToJson(const Seq *promises)
19081908
*/
19091909
JsonElement *BundleToJson(const Bundle *bundle)
19101910
{
1911+
assert(bundle != NULL);
19111912
JsonElement *json_bundle = JsonObjectCreate(10);
19121913

19131914
if (bundle->source_path)
@@ -1919,6 +1920,7 @@ JsonElement *BundleToJson(const Bundle *bundle)
19191920
JsonObjectAppendString(json_bundle, "namespace", bundle->ns);
19201921
JsonObjectAppendString(json_bundle, "name", bundle->name);
19211922
JsonObjectAppendString(json_bundle, "bundleType", bundle->type);
1923+
JsonObjectAppendString(json_bundle, "evaluation_order", EvalContextEvaluationOrderToString(bundle->evaluation_order));
19221924

19231925
{
19241926
JsonElement *json_args = JsonArrayCreate(10);
@@ -2303,6 +2305,7 @@ static Bundle *PolicyAppendBundleJson(Policy *policy, JsonElement *json_bundle)
23032305
const char *name = JsonObjectGetAsString(json_bundle, "name");
23042306
const char *type = JsonObjectGetAsString(json_bundle, "bundleType");
23052307
const char *source_path = JsonObjectGetAsString(json_bundle, "sourcePath");
2308+
const char *evaluation_order_string = JsonObjectGetAsString(json_bundle, "evaluation_order");
23062309

23072310
Rlist *args = NULL;
23082311
{
@@ -2314,7 +2317,7 @@ static Bundle *PolicyAppendBundleJson(Policy *policy, JsonElement *json_bundle)
23142317
}
23152318

23162319
// TODO: add eval order in json
2317-
Bundle *bundle = PolicyAppendBundle(policy, ns, name, type, args, source_path, EVAL_ORDER_UNDEFINED);
2320+
Bundle *bundle = PolicyAppendBundle(policy, ns, name, type, args, source_path, EvalContextEvaluationOrderFromString(evaluation_order_string));
23182321

23192322
{
23202323
JsonElement *json_promise_types = JsonObjectGetAsArray(json_bundle, "promiseTypes");

tests/unit/expand_test.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void test_map_iterators_from_rval_empty(void **state)
8181
EvalContext *ctx = *state;
8282

8383
Policy *p = PolicyNew();
84-
Bundle *bp = PolicyAppendBundle(p, "default", "none", "agent", NULL, NULL);
84+
Bundle *bp = PolicyAppendBundle(p, "default", "none", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
8585

8686
Rlist *lists = NULL;
8787
Rlist *scalars = NULL;
@@ -99,7 +99,7 @@ static void test_map_iterators_from_rval_literal(void **state)
9999
{
100100
EvalContext *ctx = *state;
101101
Policy *p = PolicyNew();
102-
Bundle *bp = PolicyAppendBundle(p, "default", "none", "agent", NULL, NULL);
102+
Bundle *bp = PolicyAppendBundle(p, "default", "none", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
103103

104104
Rlist *lists = NULL;
105105
Rlist *scalars = NULL;
@@ -117,7 +117,7 @@ static void test_map_iterators_from_rval_naked_list_var(void **state)
117117
{
118118
EvalContext *ctx = *state;
119119
Policy *p = PolicyNew();
120-
Bundle *bp = PolicyAppendBundle(p, "default", "scope", "agent", NULL, NULL);
120+
Bundle *bp = PolicyAppendBundle(p, "default", "scope", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
121121

122122
{
123123
Rlist *list = NULL;
@@ -190,7 +190,7 @@ static void test_map_iterators_from_rval_naked_list_var_namespace(void **state)
190190
{
191191
EvalContext *ctx = *state;
192192
Policy *p = PolicyNew();
193-
Bundle *bp = PolicyAppendBundle(p, "ns", "scope", "agent", NULL, NULL);
193+
Bundle *bp = PolicyAppendBundle(p, "ns", "scope", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
194194

195195
{
196196
Rlist *list = NULL;
@@ -421,7 +421,7 @@ static void test_expand_promise_array_with_scalar_arg(void **state)
421421
}
422422

423423
Policy *policy = PolicyNew();
424-
Bundle *bundle = PolicyAppendBundle(policy, NamespaceDefault(), "bundle", "agent", NULL, NULL);
424+
Bundle *bundle = PolicyAppendBundle(policy, NamespaceDefault(), "bundle", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
425425
BundleSection *section = BundleAppendSection(bundle, "dummy");
426426
Promise *promise = BundleSectionAppendPromise(section, "$(foo[$(bar)])", (Rval) { NULL, RVAL_TYPE_NOPROMISEE }, "any", NULL);
427427

@@ -476,7 +476,7 @@ static void test_expand_promise_slist(void **state)
476476

477477

478478
Policy *policy = PolicyNew();
479-
Bundle *bundle = PolicyAppendBundle(policy, NamespaceDefault(), "bundle", "agent", NULL, NULL);
479+
Bundle *bundle = PolicyAppendBundle(policy, NamespaceDefault(), "bundle", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
480480
BundleSection *section = BundleAppendSection(bundle, "dummy");
481481
Promise *promise = BundleSectionAppendPromise(section, "$(foo)", (Rval) { NULL, RVAL_TYPE_NOPROMISEE }, "any", NULL);
482482

@@ -544,7 +544,7 @@ static void test_expand_promise_array_with_slist_arg(void **state)
544544

545545

546546
Policy *policy = PolicyNew();
547-
Bundle *bundle = PolicyAppendBundle(policy, NamespaceDefault(), "bundle", "agent", NULL, NULL);
547+
Bundle *bundle = PolicyAppendBundle(policy, NamespaceDefault(), "bundle", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);
548548
BundleSection *section = BundleAppendSection(bundle, "dummy");
549549
Promise *promise = BundleSectionAppendPromise(section, "$(arr[$(keys)])", (Rval) { NULL, RVAL_TYPE_NOPROMISEE }, "any", NULL);
550550

0 commit comments

Comments
 (0)