1+ #pragma once
2+
3+ #include < string>
4+ #include < nlohmann/json.hpp>
5+ #include " Category.h"
6+ #include " DataComponent.h"
7+ #include " DataComponentRegistry.h"
8+
9+ namespace ConnectedSystemsAPI {
10+ namespace DataModels {
11+ namespace Component {
12+ class DataChoice ;
13+ void to_json (nlohmann::ordered_json& j, const DataChoice& v);
14+
15+ class DataChoice : public DataComponent {
16+ private:
17+ std::optional<Category> choiceValue;
18+ std::vector<std::unique_ptr<DataComponent>> items;
19+
20+ public:
21+ DataChoice () = default ;
22+
23+ void validate () const {
24+ DataComponent::validate ();
25+ }
26+
27+ nlohmann::ordered_json toJson () const override {
28+ nlohmann::ordered_json j;
29+ to_json (j, *this );
30+ return j;
31+ }
32+
33+ // / <summary>
34+ // / This category component marks the data stream element that will indicate the actual choice made.
35+ // / Possible choices are listed in the Category constraint section as an enumeration and should map to item names.
36+ // / </summary>
37+ const std::optional<Category> getChoiceValue () const { return choiceValue; }
38+ // / <summary>
39+ // / This category component marks the data stream element that will indicate the actual choice made.
40+ // / Possible choices are listed in the Category constraint section as an enumeration and should map to item names.
41+ // / </summary>
42+ void setChoiceValue (const std::optional<Category>& choiceValue) { this ->choiceValue = choiceValue; }
43+ // / <summary>
44+ // / Definition of the choice items.
45+ // / Items can be of any component types.
46+ // / </summary>
47+ const std::vector<std::unique_ptr<DataComponent>>& getItems () const { return items; }
48+ // / <summary>
49+ // / Definition of the choice items.
50+ // / Items can be of any component types.
51+ // / </summary>
52+ void setItems (std::vector<std::unique_ptr<DataComponent>> f) { items = std::move (f); }
53+ };
54+
55+ // Register with the DataComponentRegistry
56+ struct DataChoiceRegistrar {
57+ DataChoiceRegistrar () {
58+ DataComponentRegistry::registerType (
59+ " DataChoice" , [](const nlohmann::json& j) {
60+ return std::make_unique<DataChoice>(j.get <DataChoice>());
61+ }
62+ );
63+ }
64+ };
65+ static DataChoiceRegistrar dataChoiceRegistrar;
66+
67+ inline void from_json (const nlohmann::json& j, DataChoice& v) {
68+ from_json (j, static_cast <DataComponent&>(v));
69+
70+ if (j.contains (" choiceValue" ))
71+ v.setChoiceValue (j.at (" choiceValue" ).get <Category>());
72+
73+ if (j.contains (" items" ) && j[" items" ].is_array ()) {
74+ std::vector<std::unique_ptr<DataComponent>> items;
75+ for (const auto & item : j[" items" ]) {
76+ items.push_back (DataComponentRegistry::createDataComponent (item));
77+ }
78+ v.setItems (std::move (items));
79+ }
80+ }
81+
82+ inline void to_json (nlohmann::ordered_json& j, const DataChoice& v) {
83+ to_json (j, static_cast <const DataComponent&>(v));
84+
85+ if (v.getChoiceValue ())
86+ j[" choiceValue" ] = v.getChoiceValue ().value ();
87+
88+ if (!v.getItems ().empty ()) {
89+ j[" items" ] = nlohmann::ordered_json::array ();
90+ for (const auto & item : v.getItems ()) {
91+ j[" items" ].push_back (item->toJson ());
92+ }
93+ }
94+ }
95+ }
96+ }
97+ }
0 commit comments