Repository navigation
Expand file tree
/
Copy pathLabProgramContract.cpp
More file actions
186 lines (166 loc) · 5.89 KB
/
Copy pathLabProgramContract.cpp
File metadata and controls
186 lines (166 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Lab Program Contract
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <limits>
#include <optional>
#include <string>
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
#include "Common/Cpp/Options/EnumDropdownOption.h"
#include "Common/Cpp/Options/FloatingPointOption.h"
#include "Common/Cpp/Options/GroupOption.h"
#include "Common/Cpp/Options/SimpleIntegerOptionBase.h"
#include "Common/Cpp/Options/StringOption.h"
#include "CommonTools/Options/StringSelectOption.h"
#include "LabProgramContract.h"
namespace PokemonAutomation{
namespace{
// A floating-point bound, or null for the "no bound" default.
JsonValue number_bound(double bound){
if (bound <= -std::numeric_limits<double>::max() || bound >= std::numeric_limits<double>::max()){
return JsonValue();
}
return bound;
}
JsonArray enum_entries(const IntegerEnumDropdownDatabase& database){
JsonArray entries;
for (size_t value : database.all_values()){
const EnumEntry* entry = database.find(value);
JsonObject item;
item["slug"] = entry->slug;
item["enabled"] = entry->enabled;
entries.push_back(std::move(item));
}
return entries;
}
JsonArray select_entries(const StringSelectDatabase& database){
JsonArray entries;
for (const StringSelectEntry& entry : database.case_list()){
JsonObject item;
item["slug"] = entry.slug;
item["enabled"] = true;
entries.push_back(std::move(item));
}
return entries;
}
// A group's "type" and options. Nothing if `option` is not a group.
std::optional<JsonObject> describe_group(const ConfigOption& option){
JsonObject out;
const BatchOption* batch = dynamic_cast<const BatchOption*>(&option);
if (batch == nullptr){
return std::nullopt;
}
const GroupOption* group = dynamic_cast<const GroupOption*>(&option);
out["type"] = "group";
out["toggleable"] = group != nullptr && group->toggleable();
out["options"] = lab_option_contract(*batch);
return out;
}
// A boolean's, number's or string's "type", "default" and bounds. Nothing
// if `option` is none of them.
std::optional<JsonObject> describe_value(const ConfigOption& option){
JsonObject out;
if (const BooleanCheckBoxCell* cell = dynamic_cast<const BooleanCheckBoxCell*>(&option)){
out["type"] = "boolean";
out["default"] = cell->default_value();
return out;
}
if (const SimpleIntegerCellBase* cell = dynamic_cast<const SimpleIntegerCellBase*>(&option)){
out["type"] = "integer";
out["default"] = cell->default_value();
out["min"] = cell->min_value();
out["max"] = cell->max_value();
return out;
}
if (const FloatingPointCell* cell = dynamic_cast<const FloatingPointCell*>(&option)){
out["type"] = "number";
out["default"] = cell->default_value();
out["min"] = number_bound(cell->min_value());
out["max"] = number_bound(cell->max_value());
return out;
}
if (const StringCell* cell = dynamic_cast<const StringCell*>(&option)){
out["type"] = "string";
out["default"] = cell->default_value();
return out;
}
return std::nullopt;
}
// A choice's "type", "default" and "entries". Nothing if `option` is not a
// choice.
std::optional<JsonObject> describe_choice(const ConfigOption& option){
JsonObject out;
if (const IntegerEnumDropdownCell* cell = dynamic_cast<const IntegerEnumDropdownCell*>(&option)){
const IntegerEnumDropdownDatabase& database = cell->database();
out["type"] = "enum";
out["default"] = database.find(cell->default_value())->slug;
out["entries"] = enum_entries(database);
return out;
}
if (const StringSelectCell* cell = dynamic_cast<const StringSelectCell*>(&option)){
out["type"] = "select";
out["default"] = cell->default_slug();
out["entries"] = select_entries(cell->database());
return out;
}
return std::nullopt;
}
JsonArray precheck_list(const std::vector<PrecheckSummary>& prechecks){
JsonArray list;
for (const PrecheckSummary& check : prechecks){
JsonObject item;
item["name"] = check.name;
item["needs"] = check.needs;
item["skippable"] = check.skippable;
list.push_back(std::move(item));
}
return list;
}
// The fields of `option` for its entry: "type" and those of that type.
JsonObject describe_option(const ConfigOption& option){
std::optional<JsonObject> described = describe_group(option);
if (!described){
described = describe_value(option);
}
if (!described){
described = describe_choice(option);
}
if (described){
return std::move(*described);
}
JsonObject other;
other["type"] = "other";
return other;
}
} // namespace
JsonArray lab_option_contract(const BatchOption& options){
JsonArray contract;
for (const auto& item : keyed_options(options)){
if (item.second.empty()){
continue;
}
JsonObject entry = describe_option(*item.first);
entry["key"] = item.second;
contract.push_back(std::move(entry));
}
return contract;
}
JsonObject lab_program_contract(
std::string_view id,
const BatchOption& options,
const std::optional<std::vector<PrecheckSummary>>& prechecks
){
JsonObject contract;
contract["schema"] = LAB_PROGRAM_CONTRACT_SCHEMA;
contract["id"] = std::string(id);
contract["options"] = lab_option_contract(options);
if (prechecks){
contract["prechecks"] = precheck_list(*prechecks);
}else{
contract["prechecks"] = "unsupported";
}
return contract;
}
} // namespace PokemonAutomation