Skip to content

Commit 8e5d6ae

Browse files
Add createDataStream, unit tests
1 parent 37442a2 commit 8e5d6ae

13 files changed

Lines changed: 522 additions & 71 deletions

CSAPI-lib/CSAPI-lib.vcxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,18 @@
136136
<ClInclude Include="APIResponse.h" />
137137
<ClInclude Include="ConnectedSystemsAPI.h" />
138138
<ClInclude Include="DataModels\Component\Boolean.h" />
139+
<ClInclude Include="DataModels\Component\BooleanBuilder.h" />
139140
<ClInclude Include="DataModels\Component\Category.h" />
140141
<ClInclude Include="DataModels\Component\CategoryRange.h" />
141142
<ClInclude Include="DataModels\Component\Count.h" />
142143
<ClInclude Include="DataModels\Component\CountRange.h" />
143144
<ClInclude Include="DataModels\Component\DataArray.h" />
144145
<ClInclude Include="DataModels\Component\DataChoice.h" />
145146
<ClInclude Include="DataModels\Component\DataComponent.h" />
147+
<ClInclude Include="DataModels\Component\DataComponentBuilder.h" />
146148
<ClInclude Include="DataModels\Component\DataComponentRegistry.h" />
147149
<ClInclude Include="DataModels\Component\DataRecord.h" />
150+
<ClInclude Include="DataModels\Component\DataRecordBuilder.h" />
148151
<ClInclude Include="DataModels\Component\NilValue.h" />
149152
<ClInclude Include="DataModels\Component\Quantity.h" />
150153
<ClInclude Include="DataModels\Component\QuantityRange.h" />
@@ -156,8 +159,10 @@
156159
<ClInclude Include="DataModels\Component\UnitOfMeasure.h" />
157160
<ClInclude Include="DataModels\Component\Vector.h" />
158161
<ClInclude Include="DataModels\DataStream.h" />
162+
<ClInclude Include="DataModels\DataStreamBuilder.h" />
159163
<ClInclude Include="DataModels\Link.h" />
160164
<ClInclude Include="DataModels\ObservationSchema.h" />
165+
<ClInclude Include="DataModels\ObservationSchemaBuilder.h" />
161166
<ClInclude Include="DataModels\ObservedProperty.h" />
162167
<ClInclude Include="DataModels\Properties.h" />
163168
<ClInclude Include="DataModels\PropertiesBuilder.h" />

CSAPI-lib/CSAPI-lib.vcxproj.filters

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,20 @@
153153
<ClInclude Include="Util\JsonUtils.h">
154154
<Filter>Header Files\Util</Filter>
155155
</ClInclude>
156+
<ClInclude Include="DataModels\DataStreamBuilder.h">
157+
<Filter>Header Files\DataModels</Filter>
158+
</ClInclude>
159+
<ClInclude Include="DataModels\ObservationSchemaBuilder.h">
160+
<Filter>Header Files\DataModels</Filter>
161+
</ClInclude>
162+
<ClInclude Include="DataModels\Component\DataComponentBuilder.h">
163+
<Filter>Header Files\DataModels\Component</Filter>
164+
</ClInclude>
165+
<ClInclude Include="DataModels\Component\DataRecordBuilder.h">
166+
<Filter>Header Files\DataModels\Component</Filter>
167+
</ClInclude>
168+
<ClInclude Include="DataModels\Component\BooleanBuilder.h">
169+
<Filter>Header Files\DataModels\Component</Filter>
170+
</ClInclude>
156171
</ItemGroup>
157172
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include <optional>
4+
5+
#include "Boolean.h"
6+
#include "DataComponentBuilder.h"
7+
8+
namespace ConnectedSystemsAPI {
9+
namespace DataModels {
10+
namespace Component {
11+
class BooleanBuilder : public DataComponentBuilder<BooleanBuilder, Boolean> {
12+
private:
13+
std::optional<bool> value;
14+
15+
void validate() const {
16+
this->validateBase();
17+
}
18+
19+
public:
20+
BooleanBuilder() = default;
21+
22+
BooleanBuilder& withValue(std::optional<bool> v) { value = std::move(v); return *this; }
23+
BooleanBuilder& withValue(bool v) { value = v; return *this; }
24+
BooleanBuilder& clearValue() { value.reset(); return *this; }
25+
26+
Boolean build() {
27+
validate();
28+
29+
Boolean b;
30+
this->applyBase(b);
31+
32+
if (value) b.setValue(value);
33+
34+
return b;
35+
}
36+
};
37+
}
38+
}
39+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}

CSAPI-lib/DataModels/Component/DataComponentRegistry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pragma once
22

3+
#include <iostream>
34
#include <string>
45
#include <unordered_map>
56
#include <nlohmann/json.hpp>
7+
68
#include "DataComponent.h"
79

810
namespace ConnectedSystemsAPI {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <memory>
5+
#include <stdexcept>
6+
7+
#include "DataRecord.h"
8+
#include "DataComponentBuilder.h"
9+
10+
namespace ConnectedSystemsAPI {
11+
namespace DataModels {
12+
namespace Component {
13+
class DataRecordBuilder : public DataComponentBuilder<DataRecordBuilder, DataRecord> {
14+
private:
15+
std::vector<std::unique_ptr<DataComponent>> fields;
16+
17+
void validate() const {
18+
this->validateBase();
19+
20+
for (const auto& field : fields) {
21+
if (field)
22+
field->validate();
23+
else
24+
throw std::runtime_error("DataRecordBuilder: fields cannot contain null pointers.");
25+
}
26+
}
27+
28+
public:
29+
DataRecordBuilder() = default;
30+
31+
DataRecordBuilder& withFields(std::vector<std::unique_ptr<DataComponent>> f) { fields = std::move(f); return *this; }
32+
DataRecordBuilder& addField(std::unique_ptr<DataComponent> field) { if (field) fields.push_back(std::move(field)); return *this; }
33+
DataRecordBuilder& clearFields() { fields.clear(); return *this; }
34+
35+
DataRecord build() {
36+
validate();
37+
38+
DataRecord r;
39+
this->applyBase(r);
40+
r.setFields(std::move(fields));
41+
42+
return r;
43+
}
44+
};
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)