Skip to content

Commit 805e277

Browse files
committed
JsonConfig: adds 'succeeded' module condition to JSON config
Fixes #1908
1 parent 3ce3de2 commit 805e277

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

doc/json_schema.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@
237237
"description": "Null to disable this condition"
238238
}
239239
]
240+
},
241+
"succeeded": {
242+
"description": "Whether the module succeeded in the last run",
243+
"oneOf": [
244+
{
245+
"type": "boolean",
246+
"description": "True to only show the module if it succeeded, false to only show it if it failed"
247+
},
248+
{
249+
"type": "null",
250+
"description": "Null to disable this condition"
251+
}
252+
]
240253
}
241254
}
242255
},

src/common/jsonconfig.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,6 @@ const char* ffJsonConfigParseEnum(yyjson_val* val, int* result, FFKeyValuePair p
101101
return "Invalid enum value type; must be a string or integer";
102102
}
103103

104-
static inline void genJsonResult(FFModuleBaseInfo* baseInfo, void* options, yyjson_mut_doc* doc)
105-
{
106-
yyjson_mut_val* module = yyjson_mut_arr_add_obj(doc, doc->root);
107-
yyjson_mut_obj_add_str(doc, module, "type", baseInfo->name);
108-
if (baseInfo->generateJsonResult)
109-
baseInfo->generateJsonResult(options, doc, module);
110-
else
111-
yyjson_mut_obj_add_str(doc, module, "error", "Unsupported for JSON format");
112-
}
113-
114104
static bool parseModuleJsonObject(const char* type, yyjson_val* jsonVal, yyjson_mut_doc* jsonDoc)
115105
{
116106
if(!ffCharIsEnglishAlphabet(type[0])) return false;
@@ -123,14 +113,39 @@ static bool parseModuleJsonObject(const char* type, yyjson_val* jsonVal, yyjson_
123113
uint8_t optionBuf[FF_OPTION_MAX_SIZE];
124114
baseInfo->initOptions(optionBuf);
125115
if (jsonVal) baseInfo->parseJsonObject(optionBuf, jsonVal);
126-
if (__builtin_expect(jsonDoc != NULL, false))
127-
genJsonResult(baseInfo, optionBuf, jsonDoc);
116+
bool succeeded;
117+
if (jsonDoc)
118+
{
119+
yyjson_mut_val* module = yyjson_mut_arr_add_obj(jsonDoc, jsonDoc->root);
120+
yyjson_mut_obj_add_str(jsonDoc, module, "type", baseInfo->name);
121+
if (baseInfo->generateJsonResult)
122+
succeeded = baseInfo->generateJsonResult(optionBuf, jsonDoc, module);
123+
else
124+
{
125+
yyjson_mut_obj_add_str(jsonDoc, module, "error", "Unsupported for JSON format");
126+
succeeded = false;
127+
}
128+
}
128129
else
129-
baseInfo->printModule(optionBuf);
130+
succeeded = baseInfo->printModule(optionBuf);
130131
baseInfo->destroyOptions(optionBuf);
131-
return true;
132+
return succeeded;
132133
}
133134
}
135+
136+
if (jsonDoc)
137+
{
138+
yyjson_mut_val* module = yyjson_mut_arr_add_obj(jsonDoc, jsonDoc->root);
139+
yyjson_mut_obj_add_strcpy(jsonDoc, module, "type", type);
140+
yyjson_mut_obj_add_str(jsonDoc, module, "error", "Unknown module type");
141+
}
142+
else
143+
{
144+
FFModuleArgs moduleArgs;
145+
ffOptionInitModuleArg(&moduleArgs, "");
146+
ffPrintError(type, 0, &moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown module type");
147+
ffOptionDestroyModuleArg(&moduleArgs);
148+
}
134149
return false;
135150
}
136151

@@ -217,6 +232,7 @@ static const char* printJsonConfig(bool prepare, yyjson_mut_doc* jsonDoc)
217232
if (!modules) return NULL;
218233
if (!yyjson_is_arr(modules)) return "Property 'modules' must be an array of strings or objects";
219234

235+
bool succeeded = true;
220236
int32_t thres = instance.config.display.stat;
221237
yyjson_val* item;
222238
size_t idx, max;
@@ -253,6 +269,15 @@ static const char* printJsonConfig(bool prepare, yyjson_mut_doc* jsonDoc)
253269
arch = yyjson_obj_get(conditions, "!arch");
254270
if (arch && matchesJsonArray(ffVersionResult.architecture, arch))
255271
continue;
272+
273+
yyjson_val* previousSucceeded = yyjson_obj_get(conditions, "succeeded");
274+
if (previousSucceeded && !unsafe_yyjson_is_null(previousSucceeded))
275+
{
276+
if (!unsafe_yyjson_is_bool(previousSucceeded))
277+
return "Property 'succeeded' in 'condition' must be a boolean";
278+
if (succeeded != unsafe_yyjson_get_bool(previousSucceeded))
279+
continue;
280+
}
256281
}
257282

258283
type = yyjson_get_str(yyjson_obj_get(module, "type"));
@@ -265,8 +290,8 @@ static const char* printJsonConfig(bool prepare, yyjson_mut_doc* jsonDoc)
265290

266291
if(prepare)
267292
prepareModuleJsonObject(type, module);
268-
else if(!parseModuleJsonObject(type, module, jsonDoc))
269-
return "Unknown module type";
293+
else
294+
succeeded = parseModuleJsonObject(type, module, jsonDoc);
270295

271296
if(!prepare && thres >= 0)
272297
{

0 commit comments

Comments
 (0)