|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <string_view> |
| 5 | +#include <optional> |
| 6 | +#include <memory> |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +#include "CommandSchema.h" |
| 10 | +#include "Component/DataComponent.h" |
| 11 | +#include "Component/DataComponentRegistry.h" |
| 12 | +#include "ValidationException.h" |
| 13 | + |
| 14 | +namespace ConnectedSystemsAPI::DataModels { |
| 15 | + /// <summary> |
| 16 | + /// Builder for CommandSchema instances following the fluent builder pattern. |
| 17 | + /// This class is NOT thread-safe. Each thread should use its own builder instance. |
| 18 | + /// </summary> |
| 19 | + class CommandSchemaBuilder { |
| 20 | + private: |
| 21 | + std::optional<std::string> commandFormat; |
| 22 | + std::unique_ptr<Component::DataComponent> parametersSchema; |
| 23 | + std::unique_ptr<Component::DataComponent> resultSchema; |
| 24 | + std::unique_ptr<Component::DataComponent> feasibilityResultSchema; |
| 25 | + |
| 26 | + static std::unique_ptr<Component::DataComponent> cloneSchema(const Component::DataComponent* source) { |
| 27 | + if (!source) return nullptr; |
| 28 | + nlohmann::ordered_json j = source->toJson(); |
| 29 | + return Component::DataComponentRegistry::createDataComponent(j); |
| 30 | + } |
| 31 | + |
| 32 | + public: |
| 33 | + CommandSchemaBuilder() = default; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Create a builder initialized with values from an existing CommandSchema. |
| 37 | + /// </summary> |
| 38 | + explicit CommandSchemaBuilder(const CommandSchema& cs) { withCommandSchema(cs); } |
| 39 | + |
| 40 | + CommandSchemaBuilder(const CommandSchemaBuilder& other) |
| 41 | + : commandFormat(other.commandFormat), |
| 42 | + parametersSchema(cloneSchema(other.parametersSchema.get())), |
| 43 | + resultSchema(cloneSchema(other.resultSchema.get())), |
| 44 | + feasibilityResultSchema(cloneSchema(other.feasibilityResultSchema.get())) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + CommandSchemaBuilder& operator=(const CommandSchemaBuilder& other) { |
| 49 | + if (this != &other) { |
| 50 | + commandFormat = other.commandFormat; |
| 51 | + parametersSchema = cloneSchema(other.parametersSchema.get()); |
| 52 | + resultSchema = cloneSchema(other.resultSchema.get()); |
| 53 | + feasibilityResultSchema = cloneSchema(other.feasibilityResultSchema.get()); |
| 54 | + } |
| 55 | + return *this; |
| 56 | + } |
| 57 | + |
| 58 | + CommandSchemaBuilder(CommandSchemaBuilder&&) noexcept = default; |
| 59 | + CommandSchemaBuilder& operator=(CommandSchemaBuilder&&) noexcept = default; |
| 60 | + ~CommandSchemaBuilder() = default; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Static factory method to create a builder from an existing CommandSchema. |
| 64 | + /// </summary> |
| 65 | + static CommandSchemaBuilder from(const CommandSchema& cs) { |
| 66 | + return CommandSchemaBuilder(cs); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Initialize the builder with values from an existing CommandSchema instance. |
| 71 | + /// Note: This will perform a deep copy of all fields; use before modifying any fields or they will be overwritten. |
| 72 | + /// </summary> |
| 73 | + CommandSchemaBuilder& withCommandSchema(const CommandSchema& cs) { |
| 74 | + commandFormat = cs.getCommandFormat(); |
| 75 | + parametersSchema = cloneSchema(cs.getParametersSchema()); |
| 76 | + resultSchema = cloneSchema(cs.getResultSchema()); |
| 77 | + feasibilityResultSchema = cloneSchema(cs.getFeasibilityResultSchema()); |
| 78 | + return *this; |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Reset the builder to its default state, clearing all fields. |
| 83 | + /// </summary> |
| 84 | + CommandSchemaBuilder& reset() noexcept { |
| 85 | + commandFormat.reset(); |
| 86 | + parametersSchema.reset(); |
| 87 | + resultSchema.reset(); |
| 88 | + feasibilityResultSchema.reset(); |
| 89 | + return *this; |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Encoding format of the command. |
| 94 | + /// </summary> |
| 95 | + CommandSchemaBuilder& withCommandFormat(std::string_view v) noexcept { |
| 96 | + commandFormat = std::string(v); |
| 97 | + return *this; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Set the parameters schema by transferring ownership of a unique_ptr. |
| 102 | + /// Schema for the command parameters property. If omitted, parameters are not included in the control stream. |
| 103 | + /// </summary> |
| 104 | + CommandSchemaBuilder& withParametersSchema(std::unique_ptr<Component::DataComponent> v) noexcept { |
| 105 | + parametersSchema = std::move(v); |
| 106 | + return *this; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Set the parameters schema by copying from an existing DataComponent instance. |
| 111 | + /// Schema for the command parameters property. If omitted, parameters are not included in the control stream. |
| 112 | + /// Note: This performs a deep copy via JSON serialization. |
| 113 | + /// </summary> |
| 114 | + CommandSchemaBuilder& withParametersSchema(const Component::DataComponent& v) { |
| 115 | + parametersSchema = cloneSchema(&v); |
| 116 | + return *this; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Set the result schema by transferring ownership of a unique_ptr. |
| 121 | + /// Schema for the command result property describing the observed properties and their structure. |
| 122 | + /// </summary> |
| 123 | + CommandSchemaBuilder& withResultSchema(std::unique_ptr<Component::DataComponent> v) noexcept { |
| 124 | + resultSchema = std::move(v); |
| 125 | + return *this; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Set the result schema by copying from an existing DataComponent instance. |
| 130 | + /// Schema for the command result property describing the observed properties and their structure. |
| 131 | + /// Note: This performs a deep copy via JSON serialization. |
| 132 | + /// </summary> |
| 133 | + CommandSchemaBuilder& withResultSchema(const Component::DataComponent& v) { |
| 134 | + resultSchema = cloneSchema(&v); |
| 135 | + return *this; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Set the feasibility result schema by transferring ownership of a unique_ptr. |
| 140 | + /// Schema for the feasibility result property describing the structure of the feasibility assessment result. |
| 141 | + /// </summary> |
| 142 | + CommandSchemaBuilder& withFeasibilityResultSchema(std::unique_ptr<Component::DataComponent> v) noexcept { |
| 143 | + feasibilityResultSchema = std::move(v); |
| 144 | + return *this; |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Set the feasibility result schema by copying from an existing DataComponent instance. |
| 149 | + /// Schema for the feasibility result property describing the structure of the feasibility assessment result. |
| 150 | + /// Note: This performs a deep copy via JSON serialization. |
| 151 | + /// </summary> |
| 152 | + CommandSchemaBuilder& withFeasibilityResultSchema(const Component::DataComponent& v) { |
| 153 | + feasibilityResultSchema = cloneSchema(&v); |
| 154 | + return *this; |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Clear the parameters schema. |
| 159 | + /// </summary> |
| 160 | + CommandSchemaBuilder& clearParametersSchema() noexcept { |
| 161 | + parametersSchema.reset(); |
| 162 | + return *this; |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Clear the result schema. |
| 167 | + /// </summary> |
| 168 | + CommandSchemaBuilder& clearResultSchema() noexcept { |
| 169 | + resultSchema.reset(); |
| 170 | + return *this; |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Clear the feasibility result schema. |
| 175 | + /// </summary> |
| 176 | + CommandSchemaBuilder& clearFeasibilityResultSchema() noexcept { |
| 177 | + feasibilityResultSchema.reset(); |
| 178 | + return *this; |
| 179 | + } |
| 180 | + |
| 181 | + const std::optional<std::string>& getCommandFormat() const noexcept { return commandFormat; } |
| 182 | + const Component::DataComponent* getParametersSchema() const noexcept { return parametersSchema.get(); } |
| 183 | + const Component::DataComponent* getResultSchema() const noexcept { return resultSchema.get(); } |
| 184 | + const Component::DataComponent* getFeasibilityResultSchema() const noexcept { return feasibilityResultSchema.get(); } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Check if the current builder state is valid. Optionally populate a vector with validation errors. |
| 188 | + /// </summary> |
| 189 | + /// <param name="outErrors">Optional pointer to a vector to populate with validation errors. If nullptr, only validation result is returned.</param> |
| 190 | + /// <returns>True if validation passes, false otherwise.</returns> |
| 191 | + bool isValid(std::vector<ConnectedSystemsAPI::ValidationException::ValidationError>* outErrors = nullptr) const { |
| 192 | + std::vector<ConnectedSystemsAPI::ValidationException::ValidationError> errors; |
| 193 | + |
| 194 | + if (!commandFormat || commandFormat->empty()) |
| 195 | + errors.emplace_back("commandFormat", "Field is required and cannot be empty"); |
| 196 | + |
| 197 | + if (!parametersSchema) |
| 198 | + errors.emplace_back("parametersSchema", "Field is required"); |
| 199 | + |
| 200 | + bool valid = errors.empty(); |
| 201 | + |
| 202 | + if (outErrors) |
| 203 | + *outErrors = std::move(errors); |
| 204 | + |
| 205 | + return valid; |
| 206 | + } |
| 207 | + |
| 208 | + /// <summary> |
| 209 | + /// Build a CommandSchema instance from the current builder state. |
| 210 | + /// Note: This method clones the schemas, so the builder can be reused for multiple builds. |
| 211 | + /// </summary> |
| 212 | + /// <returns>A new CommandSchema instance.</returns> |
| 213 | + /// <exception cref="ValidationException">Thrown if validation fails.</exception> |
| 214 | + CommandSchema build() const { |
| 215 | + if (std::vector<ConnectedSystemsAPI::ValidationException::ValidationError> errors; !isValid(&errors)) { |
| 216 | + throw ConnectedSystemsAPI::ValidationException("CommandSchemaBuilder", errors); |
| 217 | + } |
| 218 | + |
| 219 | + return CommandSchema( |
| 220 | + commandFormat.value(), |
| 221 | + cloneSchema(parametersSchema.get()), |
| 222 | + cloneSchema(resultSchema.get()), |
| 223 | + cloneSchema(feasibilityResultSchema.get()) |
| 224 | + ); |
| 225 | + } |
| 226 | + }; |
| 227 | +} |
0 commit comments