Skip to content

Commit

Permalink
Merge branch '7-string-clear-if-set-to-null' into 'dev'
Browse files Browse the repository at this point in the history
Clear string if value ptr is null + optional fields: reset C++ field on update if not set

See merge request objectbox/objectbox-generator!7
  • Loading branch information
dan-obx committed Mar 4, 2024
2 parents 503210c + 5ba5627 commit e443abc
Show file tree
Hide file tree
Showing 7 changed files with 492 additions and 225 deletions.
84 changes: 62 additions & 22 deletions internal/generator/c/templates/binding-cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ var CppBindingTemplate = template.Must(template.New("binding-cpp").Funcs(funcMap
#include <cmath>
{{end -}}
{{range $entity := .Model.EntitiesWithMeta}}
{{- range $property := $entity.Properties}}
{{- range $property := $entity.Properties}}
const
{{- if $property.RelationTarget}} obx::RelationProperty<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$property.Meta.CppNameRelationTarget}}>
{{- else}} obx::Property<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, OBXPropertyType_{{PropTypeName $property.Type}}>
{{- end}} {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$property.Meta.CppName}}({{$property.Id.GetId}});
{{- end}}
{{- range $relation := $entity.Relations}}
{{- if $property.RelationTarget}} obx::RelationProperty<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, {{$property.Meta.CppNameRelationTarget}}>
{{- else}} obx::Property<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}, OBXPropertyType_{{PropTypeName $property.Type}}>
{{- end}} {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}_::{{$property.Meta.CppName}}({{$property.Id.GetId}});
{{- end}}
{{- range $relation := $entity.Relations}}
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}});
{{- end}}
{{- end}}
void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo::toFlatBuffer(flatbuffers::FlatBufferBuilder& fbb, const {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}& object) {
fbb.Clear();
Expand Down Expand Up @@ -86,38 +86,78 @@ std::unique_ptr<{{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}> {{$
void {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, {{$entity.Meta.CppNamespacePrefix}}{{$entity.Meta.CppName}}& outObject) {
const auto* table = flatbuffers::GetRoot<flatbuffers::Table>(data);
assert(table);
{{range $property := $entity.Properties}}
{{- if $property.Meta.Optional}}if (table->CheckField({{$property.FbvTableOffset}})) {{end}}
{{- if eq "std::vector<std::string>" $property.Meta.CppType}}{
{{- range $property := $entity.Properties}}
{{- if eq "std::string" $property.Meta.CppType}}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
if (ptr) {
outObject.{{$property.Meta.CppName}}
{{- if $property.Meta.Optional}}
{{- if IsOptionalPtr $.Optional -}}
.reset(new std::string(ptr->c_str(), ptr->size()));
{{- else -}}
.emplace(ptr->c_str(), ptr->size());
{{- end}}
{{- else -}}
.assign(ptr->c_str(), ptr->size());
{{- end}}
} else {
outObject.{{$property.Meta.CppName}}
{{- if $property.Meta.Optional -}}
.reset();
{{- else -}}
.clear();
{{- end}}
}
}
{{- else if eq "std::vector<std::string>" $property.Meta.CppType}}
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>({{$property.FbvTableOffset}});
if (ptr) {
{{- if $property.Meta.Optional}}
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}}
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}}
outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}reserve(ptr->size());
for (flatbuffers::uoffset_t i = 0; i < ptr->size(); i++) {
auto* itemPtr = ptr->Get(i);
if (itemPtr) outObject.{{$property.Meta.CppName}}{{$property.Meta.CppValOp}}emplace_back(itemPtr->c_str());
}
} else {
outObject.{{$property.Meta.CppName}}
{{- if $property.Meta.Optional -}}
.reset();
{{- else -}}
.clear();
{{- end}}
}
}{{else if eq "std::string" $property.Meta.CppType}}{
auto* ptr = table->GetPointer<const flatbuffers::String*>({{$property.FbvTableOffset}});
if (ptr) outObject.{{$property.Meta.CppName}}
{{- if $property.Meta.Optional}}{{template "field-value-assign-pre" $property.Meta}}ptr->c_str(){{template "field-value-assign-post" $property.Meta}}
{{- else}}.assign(ptr->c_str())
{{- end}};
}{{else if $property.Meta.FbIsVector}}{
}
{{- else if $property.Meta.FbIsVector}}
{
auto* ptr = table->GetPointer<const {{$property.Meta.FbOffsetType}}*>({{$property.FbvTableOffset}});
if (ptr) outObject.{{$property.Meta.CppName}}
{{- if IsOptionalPtr $property.Meta.Optional}}{{template "field-value-assign-pre" $property.Meta}}ptr->begin(), ptr->end(){{template "field-value-assign-post" $property.Meta}}
{{- else if $property.Meta.Optional}} = {{$property.Meta.CppType}}(ptr->begin(), ptr->end())
{{- else}}.assign(ptr->begin(), ptr->end())
{{- end}};
}{{- else}}outObject.{{$property.Meta.CppName}}
{{- template "field-value-assign-pre" $property.Meta -}}
else {
outObject.{{$property.Meta.CppName}}
{{- if $property.Meta.Optional -}}
.reset();
{{- else -}}
.clear();
{{- end}}
}
}
{{- else }}
{{ if $property.Meta.Optional -}}
if (table->CheckField({{$property.FbvTableOffset}})) {{end -}}
outObject.{{$property.Meta.CppName}}
{{- template "field-value-assign-pre" $property.Meta -}}
table->GetField<{{$property.Meta.CppFbType}}>({{- $property.FbvTableOffset}}, {{$property.Meta.FbDefaultValue}}){{if eq "bool" $property.Meta.CppType}} != 0{{end}}
{{- template "field-value-assign-post" $property.Meta}};
{{- template "field-value-assign-post" $property.Meta}};
{{- if $property.Meta.Optional}} else outObject.{{$property.Meta.CppName}}.reset();{{- end}}
{{- end }}
{{- end}}
{{end}}
}
{{end}}
`))
48 changes: 38 additions & 10 deletions test/comparison/testdata/fbs/typeful/cpp/schema.obx.cpp.expected
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
outObject.bool_ = table->GetField<uint8_t>(26, 0) != 0;
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(28);
if (ptr) outObject.string.assign(ptr->c_str());
if (ptr) {
outObject.string.assign(ptr->c_str(), ptr->size());
} else {
outObject.string.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>(30);
Expand All @@ -100,24 +104,31 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
auto* itemPtr = ptr->Get(i);
if (itemPtr) outObject.stringvector.emplace_back(itemPtr->c_str());
}
} else {
outObject.stringvector.clear();
}
}
outObject.byte = table->GetField<int8_t>(32, 0);
outObject.ubyte = table->GetField<uint8_t>(34, 0);
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<int8_t>*>(36);
if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end());
else {
outObject.bytevector.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<uint8_t>*>(38);
if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end());
else {
outObject.ubytevector.clear();
}
}
outObject.float32 = table->GetField<float>(40, 0.0f);
outObject.float64 = table->GetField<double>(42, 0.0);
outObject.float_ = table->GetField<float>(44, 0.0f);
outObject.double_ = table->GetField<double>(46, 0.0);
outObject.relId = table->GetField<obx_id>(48, 0);

}

const obx::Property<ns::Annotated, OBXPropertyType_Long> ns::Annotated_::identifier(1);
Expand Down Expand Up @@ -172,28 +183,47 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
outObject.identifier = table->GetField<obx_id>(4, 0);
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(6);
if (ptr) outObject.fullName.assign(ptr->c_str());
if (ptr) {
outObject.fullName.assign(ptr->c_str(), ptr->size());
} else {
outObject.fullName.clear();
}
}
outObject.time = table->GetField<int64_t>(8, 0);
outObject.relId = table->GetField<obx_id>(10, 0);
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(12);
if (ptr) outObject.unique.assign(ptr->c_str());
if (ptr) {
outObject.unique.assign(ptr->c_str(), ptr->size());
} else {
outObject.unique.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(14);
if (ptr) outObject.uniqueValue.assign(ptr->c_str());
if (ptr) {
outObject.uniqueValue.assign(ptr->c_str(), ptr->size());
} else {
outObject.uniqueValue.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(16);
if (ptr) outObject.uniqueHash.assign(ptr->c_str());
if (ptr) {
outObject.uniqueHash.assign(ptr->c_str(), ptr->size());
} else {
outObject.uniqueHash.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(18);
if (ptr) outObject.uniqueHash64.assign(ptr->c_str());
if (ptr) {
outObject.uniqueHash64.assign(ptr->c_str(), ptr->size());
} else {
outObject.uniqueHash64.clear();
}
}
outObject.uid = table->GetField<int32_t>(20, 0);

}

const obx::Property<ns::TSDate, OBXPropertyType_Long> ns::TSDate_::id(1);
Expand Down Expand Up @@ -226,7 +256,6 @@ void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSD
assert(table);
outObject.id = table->GetField<obx_id>(4, 0);
outObject.timestamp = table->GetField<int64_t>(6, 0);

}

const obx::Property<ns::TSDateNano, OBXPropertyType_Long> ns::TSDateNano_::id(1);
Expand Down Expand Up @@ -259,6 +288,5 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:
assert(table);
outObject.id = table->GetField<obx_id>(4, 0);
outObject.timestamp = table->GetField<int64_t>(6, 0);

}

48 changes: 38 additions & 10 deletions test/comparison/testdata/fbs/typeful/cpp11/schema.obx.cpp.expected
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
outObject.bool_ = table->GetField<uint8_t>(26, 0) != 0;
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(28);
if (ptr) outObject.string.assign(ptr->c_str());
if (ptr) {
outObject.string.assign(ptr->c_str(), ptr->size());
} else {
outObject.string.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>*>(30);
Expand All @@ -100,24 +104,31 @@ void Typeful::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, Typeful& o
auto* itemPtr = ptr->Get(i);
if (itemPtr) outObject.stringvector.emplace_back(itemPtr->c_str());
}
} else {
outObject.stringvector.clear();
}
}
outObject.byte = table->GetField<int8_t>(32, 0);
outObject.ubyte = table->GetField<uint8_t>(34, 0);
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<int8_t>*>(36);
if (ptr) outObject.bytevector.assign(ptr->begin(), ptr->end());
else {
outObject.bytevector.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::Vector<uint8_t>*>(38);
if (ptr) outObject.ubytevector.assign(ptr->begin(), ptr->end());
else {
outObject.ubytevector.clear();
}
}
outObject.float32 = table->GetField<float>(40, 0.0f);
outObject.float64 = table->GetField<double>(42, 0.0);
outObject.float_ = table->GetField<float>(44, 0.0f);
outObject.double_ = table->GetField<double>(46, 0.0);
outObject.relId = table->GetField<obx_id>(48, 0);

}

const obx::Property<ns::Annotated, OBXPropertyType_Long> ns::Annotated_::identifier(1);
Expand Down Expand Up @@ -172,28 +183,47 @@ void ns::Annotated::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::
outObject.identifier = table->GetField<obx_id>(4, 0);
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(6);
if (ptr) outObject.fullName.assign(ptr->c_str());
if (ptr) {
outObject.fullName.assign(ptr->c_str(), ptr->size());
} else {
outObject.fullName.clear();
}
}
outObject.time = table->GetField<int64_t>(8, 0);
outObject.relId = table->GetField<obx_id>(10, 0);
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(12);
if (ptr) outObject.unique.assign(ptr->c_str());
if (ptr) {
outObject.unique.assign(ptr->c_str(), ptr->size());
} else {
outObject.unique.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(14);
if (ptr) outObject.uniqueValue.assign(ptr->c_str());
if (ptr) {
outObject.uniqueValue.assign(ptr->c_str(), ptr->size());
} else {
outObject.uniqueValue.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(16);
if (ptr) outObject.uniqueHash.assign(ptr->c_str());
if (ptr) {
outObject.uniqueHash.assign(ptr->c_str(), ptr->size());
} else {
outObject.uniqueHash.clear();
}
}
{
auto* ptr = table->GetPointer<const flatbuffers::String*>(18);
if (ptr) outObject.uniqueHash64.assign(ptr->c_str());
if (ptr) {
outObject.uniqueHash64.assign(ptr->c_str(), ptr->size());
} else {
outObject.uniqueHash64.clear();
}
}
outObject.uid = table->GetField<int32_t>(20, 0);

}

const obx::Property<ns::TSDate, OBXPropertyType_Long> ns::TSDate_::id(1);
Expand Down Expand Up @@ -226,7 +256,6 @@ void ns::TSDate::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns::TSD
assert(table);
outObject.id = table->GetField<obx_id>(4, 0);
outObject.timestamp = table->GetField<int64_t>(6, 0);

}

const obx::Property<ns::TSDateNano, OBXPropertyType_Long> ns::TSDateNano_::id(1);
Expand Down Expand Up @@ -259,6 +288,5 @@ void ns::TSDateNano::_OBX_MetaInfo::fromFlatBuffer(const void* data, size_t, ns:
assert(table);
outObject.id = table->GetField<obx_id>(4, 0);
outObject.timestamp = table->GetField<int64_t>(6, 0);

}

Loading

0 comments on commit e443abc

Please sign in to comment.