1+ #pragma once
2+
3+ #include < string>
4+ #include < optional>
5+ #include < stdexcept>
6+
7+ #include " DataComponent.h"
8+
9+ namespace ConnectedSystemsAPI {
10+ namespace DataModels {
11+ namespace Component {
12+ template <typename Derived, typename Target>
13+ class DataComponentBuilder {
14+ protected:
15+ std::string type;
16+ std::optional<std::string> id;
17+ std::optional<std::string> name;
18+ std::optional<std::string> label;
19+ std::optional<std::string> description;
20+ std::optional<bool > updatable;
21+ std::optional<bool > optional;
22+ std::optional<std::string> definition;
23+
24+ Derived& self () noexcept { return *reinterpret_cast <Derived*>(this ); }
25+ const Derived& self () const noexcept { return *reinterpret_cast <const Derived*>(this ); }
26+
27+ public:
28+ DataComponentBuilder () = default ;
29+
30+ // Fluent setters for DataComponent base properties
31+ Derived& withType (const std::string& v) { type = v; return self (); }
32+ Derived& withId (const std::string& v) { id = v; return self (); }
33+ Derived& withName (const std::string& v) { name = v; return self (); }
34+ Derived& withLabel (const std::string& v) { label = v; return self (); }
35+ Derived& withDescription (const std::string& v) { description = v; return self (); }
36+ Derived& withUpdatable (bool v) { updatable = v; return self (); }
37+ Derived& withOptional (bool v) { optional = v; return self (); }
38+ Derived& withDefinition (const std::string& v) { definition = v; return self (); }
39+
40+ protected:
41+ void validateBase () const {
42+ if (type.empty ())
43+ throw std::runtime_error (" DataComponentBuilder: type is required." );
44+ }
45+
46+ void applyBase (Target& target) const {
47+ if (!type.empty ()) target.setType (type);
48+ if (name) target.setName (name);
49+ if (id) target.setId (id);
50+ if (label) target.setLabel (label);
51+ if (description) target.setDescription (description);
52+ if (updatable) target.setUpdatable (updatable);
53+ if (optional) target.setOptional (optional);
54+ if (definition) target.setDefinition (definition);
55+ }
56+ };
57+ }
58+ }
59+ }
0 commit comments