Skip to content

Commit 4f79472

Browse files
authored
Merge pull request #52 from matyalatte/gui_as_root
Allow to define GUI elements in root object.
2 parents 0db6aa5 + 86d0a39 commit 4f79472

File tree

5 files changed

+80
-30
lines changed

5 files changed

+80
-30
lines changed

docs/changelog.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- Array brackets can now be omitted when there is only one element.
2+
- GUI elements can now be defined in root object.
3+
14
ver 0.7.2
25
- Added "optional" to ignore some options when a text box is empty.
36
- Added "prefix" and "suffix" options to append strings to user inputs.

schema/schema.json

+41-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
33
"type": "object",
4-
"properties": {
5-
"recommended": { "$ref": "#/definitions/types/version" },
6-
"recommended_version": { "$ref": "#/definitions/types/version" },
7-
"minimum_required": { "$ref": "#/definitions/types/version" },
8-
"minimum_required_version": { "$ref": "#/definitions/types/version" },
9-
"gui": {
10-
"if": { "type": "object" },
11-
"then": { "$ref": "#/definitions/types/gui_item" },
12-
"else": {
13-
"type": "array",
14-
"items": { "$ref": "#/definitions/types/gui_item" }
15-
}
16-
},
17-
"help": {
18-
"if": { "type": "object" },
19-
"then": { "$ref": "#/definitions/types/help_item" },
20-
"else": {
21-
"type": "array",
22-
"items": { "$ref": "#/definitions/types/help_item" }
23-
}
4+
"if": {
5+
"required": [ "gui" ]
6+
},
7+
"then": {
8+
"properties": {
9+
"recommended": { "$ref": "#/definitions/types/version" },
10+
"recommended_version": { "$ref": "#/definitions/types/version" },
11+
"minimum_required": { "$ref": "#/definitions/types/version" },
12+
"minimum_required_version": { "$ref": "#/definitions/types/version" },
13+
"gui": { "$ref": "#/definitions/types/gui_array" },
14+
"help": { "$ref": "#/definitions/types/help_array" }
2415
}
2516
},
26-
"required": [ "gui" ],
17+
"else": {
18+
"allOf": [
19+
{ "$ref": "#/definitions/types/gui_item" },
20+
{
21+
"properties": {
22+
"recommended": { "$ref": "#/definitions/types/version" },
23+
"recommended_version": { "$ref": "#/definitions/types/version" },
24+
"minimum_required": { "$ref": "#/definitions/types/version" },
25+
"minimum_required_version": { "$ref": "#/definitions/types/version" },
26+
"help": { "$ref": "#/definitions/types/help_array" }
27+
}
28+
}
29+
]
30+
},
2731
"definitions": {
2832
"types": {
2933
"version": {
@@ -183,6 +187,14 @@
183187
}
184188
]
185189
},
190+
"gui_array": {
191+
"if": { "type": "object" },
192+
"then": { "$ref": "#/definitions/types/gui_item" },
193+
"else": {
194+
"type": "array",
195+
"items": { "$ref": "#/definitions/types/gui_item" }
196+
}
197+
},
186198
"help_item": {
187199
"type": "object",
188200
"properties": {
@@ -221,6 +233,14 @@
221233
}
222234
}
223235
]
236+
},
237+
"help_array": {
238+
"if": { "type": "object" },
239+
"then": { "$ref": "#/definitions/types/help_item" },
240+
"else": {
241+
"type": "array",
242+
"items": { "$ref": "#/definitions/types/help_item" }
243+
}
224244
}
225245
},
226246
"components": {

src/json_utils.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ namespace json_utils {
229229

230230
// get default definition of gui
231231
void GetDefaultDefinition(rapidjson::Document& definition) {
232-
static const char* def_str = "{\"gui\":{"
232+
static const char* def_str = "{"
233233
"\"label\":\"Default GUI\","
234234
#ifdef _WIN32
235235
"\"command\":\"dir\","
@@ -239,7 +239,7 @@ namespace json_utils {
239239
"\"button\":\"run 'ls'\","
240240
#endif
241241
"\"components\":[]"
242-
"}}";
242+
"}";
243243
rapidjson::ParseResult ok = definition.Parse(def_str);
244244
assert(ok);
245245
JsonResult result = JSON_RESULT_OK;
@@ -685,7 +685,14 @@ namespace json_utils {
685685
}
686686

687687
void CheckDefinition(JsonResult& result, rapidjson::Document& definition) {
688-
CheckJsonArrayType(result, definition, "gui", JsonType::JSON, definition.GetAllocator());
688+
rapidjson::Document::AllocatorType& alloc = definition.GetAllocator();
689+
if (!definition.HasMember("gui")) {
690+
// definition["gui"] = definition
691+
rapidjson::Value n(rapidjson::kObjectType);
692+
n.CopyFrom(definition, alloc);
693+
definition.AddMember("gui", n, alloc);
694+
}
695+
CheckJsonArrayType(result, definition, "gui", JsonType::JSON, alloc);
689696
if (!result.ok) return;
690697
if (definition["gui"].Size() == 0) {
691698
result.ok = false;
@@ -694,7 +701,7 @@ namespace json_utils {
694701

695702
for (rapidjson::Value& sub_d : definition["gui"].GetArray()) {
696703
if (!result.ok) return;
697-
CheckSubDefinition(result, sub_d, definition.GetAllocator());
704+
CheckSubDefinition(result, sub_d, alloc);
698705
}
699706
}
700707

tests/json/relaxed.jsonc

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
/*
44
* Multi-line comments
55
*/
6-
"ary": [
7-
"",
8-
"trailing_comma",
9-
]
6+
"label": "test",
7+
"command": "echo hello",
8+
"components": [],
9+
"help": {
10+
"label": "test",
11+
"type": "url",
12+
"url": "example.com",
13+
},
1014
}

tests/json_check_test.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ TEST(JsonCheckTest, checkGUISuccess4) {
7171
EXPECT_TRUE(result.ok);
7272
}
7373

74+
TEST(JsonCheckTest, checkGUISuccessRelaxed) {
75+
rapidjson::Document test_json;
76+
json_utils::JsonResult result = json_utils::LoadJson(JSON_RELAXED, test_json);
77+
EXPECT_TRUE(result.ok);
78+
json_utils::CheckDefinition(result, test_json);
79+
EXPECT_TRUE(result.ok);
80+
}
81+
7482
void CheckGUIError(rapidjson::Document& test_json, const char* expected) {
7583
json_utils::JsonResult result = JSON_RESULT_OK;
7684
json_utils::CheckDefinition(result, test_json);
@@ -82,7 +90,7 @@ TEST(JsonCheckTest, checkGUIFail) {
8290
rapidjson::Document test_json;
8391
GetTestJson(test_json);
8492
test_json.RemoveMember("gui");
85-
CheckGUIError(test_json, "['gui'] not found.");
93+
CheckGUIError(test_json, "['components'] not found.");
8694
}
8795

8896
TEST(JsonCheckTest, checkGUIFail2) {
@@ -135,6 +143,14 @@ TEST(JsonCheckTest, checkGUIFail7) {
135143
" & echo textbox: __comp8__ & echo int: __comp9__ & echo float: __comp???__");
136144
}
137145

146+
TEST(JsonCheckTest, checkGUIFailRelaxed) {
147+
rapidjson::Document test_json;
148+
json_utils::JsonResult result = json_utils::LoadJson(JSON_RELAXED, test_json);
149+
EXPECT_TRUE(result.ok);
150+
test_json.AddMember("exit_success", "a", test_json.GetAllocator());
151+
CheckGUIError(test_json, "['exit_success'] should be an int.");
152+
}
153+
138154
TEST(JsonCheckTest, checkHelpSuccess) {
139155
rapidjson::Document test_json;
140156
GetTestJson(test_json);

0 commit comments

Comments
 (0)