Skip to content

Commit e443abc

Browse files
committed
Merge branch '7-string-clear-if-set-to-null' into 'dev'
Clear string if value ptr is null + optional fields: reset C++ field on update if not set See merge request objectbox/objectbox-generator!7
2 parents 503210c + 5ba5627 commit e443abc

File tree

7 files changed

+492
-225
lines changed

7 files changed

+492
-225
lines changed

internal/generator/c/templates/binding-cpp.go

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ var CppBindingTemplate = template.Must(template.New("binding-cpp").Funcs(funcMap
3636
#include <cmath>
3737
{{end -}}
3838
{{range $entity := .Model.EntitiesWithMeta}}
39-
{{- range $property := $entity.Properties}}
39+
{{- range $property := $entity.Properties}}
4040
const
41-
{{- if $property.RelationTarget}} obx::RelationProperty<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$property.Meta.CppNameRelationTarget}}>
42-
{{- else}} obx::Property<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, OBXPropertyType_{{PropTypeName $property.Type}}>
43-
{{- end}} {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$property.Meta.CppName}}({{$property.Id.GetId}});
44-
{{- end}}
45-
{{- range $relation := $entity.Relations}}
41+
{{- if $property.RelationTarget}} obx::RelationProperty<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$property.Meta.CppNameRelationTarget}}>
42+
{{- else}} obx::Property<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, OBXPropertyType_{{PropTypeName $property.Type}}>
43+
{{- end}} {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$property.Meta.CppName}}({{$property.Id.GetId}});
44+
{{- end}}
45+
{{- range $relation := $entity.Relations}}
4646
const obx::RelationStandalone<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$relation.Target.Meta.CppNamespacePrefix}}{{$relation.Target.Meta.CppName}}> {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$relation.Meta.CppName}}({{$relation.Id.GetId}});
47-
{{- end}}
47+
{{- end}}
4848
4949
void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo::toFlatBuffer(flatbuffers::FlatBufferBuilder& fbb, const {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}& object) {
5050
fbb.Clear();
@@ -86,38 +86,78 @@ std::unique_ptr<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}> {{$
8686
void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}& outObject) {
8787
const auto* table = flatbuffers::GetRoot<flatbuffers::Table>(data);
8888
assert(table);
89-
{{range $property := $entity.Properties}}
90-
{{- if $property.Meta.Optional}}if (table->CheckField({{$property.FbvTableOffset}})) {{end}}
91-
{{- if eq "std::vector<std::string>" $property.Meta.CppType}}{
89+
{{- range $property := $entity.Properties}}
90+
{{- if eq "std::string" $property.Meta.CppType}}
91+
{
92+
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
93+
if (ptr) {
94+
outObject.{{$property.Meta.CppName}}
95+
{{- if $property.Meta.Optional}}
96+
{{- if IsOptionalPtr $.Optional -}}
97+
.reset(new std::string(ptr->c_str(), ptr->size()));
98+
{{- else -}}
99+
.emplace(ptr->c_str(), ptr->size());
100+
{{- end}}
101+
{{- else -}}
102+
.assign(ptr->c_str(), ptr->size());
103+
{{- end}}
104+
} else {
105+
outObject.{{$property.Meta.CppName}}
106+
{{- if $property.Meta.Optional -}}
107+
.reset();
108+
{{- else -}}
109+
.clear();
110+
{{- end}}
111+
}
112+
}
113+
{{- else if eq "std::vector<std::string>" $property.Meta.CppType}}
114+
{
92115
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>({{$property.FbvTableOffset}});
93116
if (ptr) {
94117
{{- if $property.Meta.Optional}}
95-
outObject.{{$property.Meta.CppName}}{{if IsOptionalPtr $property.Meta.Optional}}.reset(new {{$property.Meta.CppType}}({{else}} = {{$property.Meta.CppType}}(){{end}}{{template "field-value-assign-post" $property.Meta}};{{end}}
118+
outObject.{{$property.Meta.CppName}}{{if IsOptionalPtr $property.Meta.Optional}}.reset(new {{$property.Meta.CppType}}({{else}} = {{$property.Meta.CppType}}(){{end}}{{template "field-value-assign-post" $property.Meta}};
119+
{{- end}}
96120
outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}reserve(ptr->size());
97121
for (flatbuffers::uoffset_t i = 0; i < ptr->size(); i++) {
98122
auto* itemPtr = ptr->Get(i);
99123
if (itemPtr) outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}emplace_back(itemPtr->c_str());
100124
}
125+
} else {
126+
outObject.{{$property.Meta.CppName}}
127+
{{- if $property.Meta.Optional -}}
128+
.reset();
129+
{{- else -}}
130+
.clear();
131+
{{- end}}
101132
}
102-
}{{else if eq "std::string" $property.Meta.CppType}}{
103-
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
104-
if (ptr) outObject.{{$property.Meta.CppName}}
105-
{{- if $property.Meta.Optional}}{{template "field-value-assign-pre" $property.Meta}}ptr->c_str(){{template "field-value-assign-post" $property.Meta}}
106-
{{- else}}.assign(ptr->c_str())
107-
{{- end}};
108-
}{{else if $property.Meta.FbIsVector}}{
133+
}
134+
{{- else if $property.Meta.FbIsVector}}
135+
{
109136
auto* ptr = table->GetPointer<const {{$property.Meta.FbOffsetType}}*>({{$property.FbvTableOffset}});
110137
if (ptr) outObject.{{$property.Meta.CppName}}
111138
{{- if IsOptionalPtr $property.Meta.Optional}}{{template "field-value-assign-pre" $property.Meta}}ptr->begin(), ptr->end(){{template "field-value-assign-post" $property.Meta}}
112139
{{- else if $property.Meta.Optional}} = {{$property.Meta.CppType}}(ptr->begin(), ptr->end())
113140
{{- else}}.assign(ptr->begin(), ptr->end())
114141
{{- end}};
115-
}{{- else}}outObject.{{$property.Meta.CppName}}
116-
{{- template "field-value-assign-pre" $property.Meta -}}
142+
else {
143+
outObject.{{$property.Meta.CppName}}
144+
{{- if $property.Meta.Optional -}}
145+
.reset();
146+
{{- else -}}
147+
.clear();
148+
{{- end}}
149+
}
150+
}
151+
{{- else }}
152+
{{ if $property.Meta.Optional -}}
153+
if (table->CheckField({{$property.FbvTableOffset}})) {{end -}}
154+
outObject.{{$property.Meta.CppName}}
155+
{{- template "field-value-assign-pre" $property.Meta -}}
117156
table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, {{$property.Meta.FbDefaultValue}}){{if eq "bool" $property.Meta.CppType}} != 0{{end}}
118-
{{- template "field-value-assign-post" $property.Meta}};
157+
{{- template "field-value-assign-post" $property.Meta}};
158+
{{- if $property.Meta.Optional}} else outObject.{{$property.Meta.CppName}}.reset();{{- end}}
159+
{{- end }}
119160
{{- end}}
120-
{{end}}
121161
}
122162
{{end}}
123163
`))

test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
9090
outObject.bool_ = table->GetField<uint8_t>(26, 0) != 0;
9191
{
9292
auto* ptr = table->GetPointer<const flatbuffers::String*>(28);
93-
if (ptr) outObject.string.assign(ptr->c_str());
93+
if (ptr) {
94+
outObject.string.assign(ptr->c_str(), ptr->size());
95+
} else {
96+
outObject.string.clear();
97+
}
9498
}
9599
{
96100
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>(30);
@@ -100,24 +104,31 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
100104
auto* itemPtr = ptr->Get(i);
101105
if (itemPtr) outObject.stringvector.emplace_back(itemPtr->c_str());
102106
}
107+
} else {
108+
outObject.stringvector.clear();
103109
}
104110
}
105111
outObject.byte = table->GetField<int8_t>(32, 0);
106112
outObject.ubyte = table->GetField<uint8_t>(34, 0);
107113
{
108114
auto* ptr = table->GetPointer<const flatbuffers::Vector<int8_t>*>(36);
109115
if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end());
116+
else {
117+
outObject.bytevector.clear();
118+
}
110119
}
111120
{
112121
auto* ptr = table->GetPointer<const flatbuffers::Vector<uint8_t>*>(38);
113122
if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end());
123+
else {
124+
outObject.ubytevector.clear();
125+
}
114126
}
115127
outObject.float32 = table->GetField<float>(40, 0.0f);
116128
outObject.float64 = table->GetField<double>(42, 0.0);
117129
outObject.float_ = table->GetField<float>(44, 0.0f);
118130
outObject.double_ = table->GetField<double>(46, 0.0);
119131
outObject.relId = table->GetField<obx_id>(48, 0);
120-
121132
}
122133

123134
const obx::Property<ns::Annotated, OBXPropertyType_Long> ns::Annotated_::identifier(1);
@@ -172,28 +183,47 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
172183
outObject.identifier = table->GetField<obx_id>(4, 0);
173184
{
174185
auto* ptr = table->GetPointer<const flatbuffers::String*>(6);
175-
if (ptr) outObject.fullName.assign(ptr->c_str());
186+
if (ptr) {
187+
outObject.fullName.assign(ptr->c_str(), ptr->size());
188+
} else {
189+
outObject.fullName.clear();
190+
}
176191
}
177192
outObject.time = table->GetField<int64_t>(8, 0);
178193
outObject.relId = table->GetField<obx_id>(10, 0);
179194
{
180195
auto* ptr = table->GetPointer<const flatbuffers::String*>(12);
181-
if (ptr) outObject.unique.assign(ptr->c_str());
196+
if (ptr) {
197+
outObject.unique.assign(ptr->c_str(), ptr->size());
198+
} else {
199+
outObject.unique.clear();
200+
}
182201
}
183202
{
184203
auto* ptr = table->GetPointer<const flatbuffers::String*>(14);
185-
if (ptr) outObject.uniqueValue.assign(ptr->c_str());
204+
if (ptr) {
205+
outObject.uniqueValue.assign(ptr->c_str(), ptr->size());
206+
} else {
207+
outObject.uniqueValue.clear();
208+
}
186209
}
187210
{
188211
auto* ptr = table->GetPointer<const flatbuffers::String*>(16);
189-
if (ptr) outObject.uniqueHash.assign(ptr->c_str());
212+
if (ptr) {
213+
outObject.uniqueHash.assign(ptr->c_str(), ptr->size());
214+
} else {
215+
outObject.uniqueHash.clear();
216+
}
190217
}
191218
{
192219
auto* ptr = table->GetPointer<const flatbuffers::String*>(18);
193-
if (ptr) outObject.uniqueHash64.assign(ptr->c_str());
220+
if (ptr) {
221+
outObject.uniqueHash64.assign(ptr->c_str(), ptr->size());
222+
} else {
223+
outObject.uniqueHash64.clear();
224+
}
194225
}
195226
outObject.uid = table->GetField<int32_t>(20, 0);
196-
197227
}
198228

199229
const obx::Property<ns::TSDate, OBXPropertyType_Long> ns::TSDate_::id(1);
@@ -226,7 +256,6 @@ void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSD
226256
assert(table);
227257
outObject.id = table->GetField<obx_id>(4, 0);
228258
outObject.timestamp = table->GetField<int64_t>(6, 0);
229-
230259
}
231260

232261
const obx::Property<ns::TSDateNano, OBXPropertyType_Long> ns::TSDateNano_::id(1);
@@ -259,6 +288,5 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:
259288
assert(table);
260289
outObject.id = table->GetField<obx_id>(4, 0);
261290
outObject.timestamp = table->GetField<int64_t>(6, 0);
262-
263291
}
264292

test/comparison/testdata/fbs/typeful/cpp11/schema.obx.cpp.expected

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
9090
outObject.bool_ = table->GetField<uint8_t>(26, 0) != 0;
9191
{
9292
auto* ptr = table->GetPointer<const flatbuffers::String*>(28);
93-
if (ptr) outObject.string.assign(ptr->c_str());
93+
if (ptr) {
94+
outObject.string.assign(ptr->c_str(), ptr->size());
95+
} else {
96+
outObject.string.clear();
97+
}
9498
}
9599
{
96100
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>(30);
@@ -100,24 +104,31 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
100104
auto* itemPtr = ptr->Get(i);
101105
if (itemPtr) outObject.stringvector.emplace_back(itemPtr->c_str());
102106
}
107+
} else {
108+
outObject.stringvector.clear();
103109
}
104110
}
105111
outObject.byte = table->GetField<int8_t>(32, 0);
106112
outObject.ubyte = table->GetField<uint8_t>(34, 0);
107113
{
108114
auto* ptr = table->GetPointer<const flatbuffers::Vector<int8_t>*>(36);
109115
if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end());
116+
else {
117+
outObject.bytevector.clear();
118+
}
110119
}
111120
{
112121
auto* ptr = table->GetPointer<const flatbuffers::Vector<uint8_t>*>(38);
113122
if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end());
123+
else {
124+
outObject.ubytevector.clear();
125+
}
114126
}
115127
outObject.float32 = table->GetField<float>(40, 0.0f);
116128
outObject.float64 = table->GetField<double>(42, 0.0);
117129
outObject.float_ = table->GetField<float>(44, 0.0f);
118130
outObject.double_ = table->GetField<double>(46, 0.0);
119131
outObject.relId = table->GetField<obx_id>(48, 0);
120-
121132
}
122133

123134
const obx::Property<ns::Annotated, OBXPropertyType_Long> ns::Annotated_::identifier(1);
@@ -172,28 +183,47 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
172183
outObject.identifier = table->GetField<obx_id>(4, 0);
173184
{
174185
auto* ptr = table->GetPointer<const flatbuffers::String*>(6);
175-
if (ptr) outObject.fullName.assign(ptr->c_str());
186+
if (ptr) {
187+
outObject.fullName.assign(ptr->c_str(), ptr->size());
188+
} else {
189+
outObject.fullName.clear();
190+
}
176191
}
177192
outObject.time = table->GetField<int64_t>(8, 0);
178193
outObject.relId = table->GetField<obx_id>(10, 0);
179194
{
180195
auto* ptr = table->GetPointer<const flatbuffers::String*>(12);
181-
if (ptr) outObject.unique.assign(ptr->c_str());
196+
if (ptr) {
197+
outObject.unique.assign(ptr->c_str(), ptr->size());
198+
} else {
199+
outObject.unique.clear();
200+
}
182201
}
183202
{
184203
auto* ptr = table->GetPointer<const flatbuffers::String*>(14);
185-
if (ptr) outObject.uniqueValue.assign(ptr->c_str());
204+
if (ptr) {
205+
outObject.uniqueValue.assign(ptr->c_str(), ptr->size());
206+
} else {
207+
outObject.uniqueValue.clear();
208+
}
186209
}
187210
{
188211
auto* ptr = table->GetPointer<const flatbuffers::String*>(16);
189-
if (ptr) outObject.uniqueHash.assign(ptr->c_str());
212+
if (ptr) {
213+
outObject.uniqueHash.assign(ptr->c_str(), ptr->size());
214+
} else {
215+
outObject.uniqueHash.clear();
216+
}
190217
}
191218
{
192219
auto* ptr = table->GetPointer<const flatbuffers::String*>(18);
193-
if (ptr) outObject.uniqueHash64.assign(ptr->c_str());
220+
if (ptr) {
221+
outObject.uniqueHash64.assign(ptr->c_str(), ptr->size());
222+
} else {
223+
outObject.uniqueHash64.clear();
224+
}
194225
}
195226
outObject.uid = table->GetField<int32_t>(20, 0);
196-
197227
}
198228

199229
const obx::Property<ns::TSDate, OBXPropertyType_Long> ns::TSDate_::id(1);
@@ -226,7 +256,6 @@ void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSD
226256
assert(table);
227257
outObject.id = table->GetField<obx_id>(4, 0);
228258
outObject.timestamp = table->GetField<int64_t>(6, 0);
229-
230259
}
231260

232261
const obx::Property<ns::TSDateNano, OBXPropertyType_Long> ns::TSDateNano_::id(1);
@@ -259,6 +288,5 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:
259288
assert(table);
260289
outObject.id = table->GetField<obx_id>(4, 0);
261290
outObject.timestamp = table->GetField<int64_t>(6, 0);
262-
263291
}
264292

0 commit comments

Comments
 (0)