From 5c88fae97bc0ea56e68b77888bcf1af1a6f4fe2b Mon Sep 17 00:00:00 2001 From: greenrobot Team <13865709+greenrobot-team@users.noreply.github.com> Date: Tue, 18 Jan 2022 15:46:19 +0100 Subject: [PATCH] Exclude flex properties when generating C/C++ binding code. --- internal/generator/binding/field.go | 10 +++++++++ internal/generator/c/cgenerator.go | 22 ++++++++++++++++++- .../fbs/typeful/c/objectbox-model.h.expected | 3 ++- .../typeful/cpp/objectbox-model.h.expected | 3 ++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/generator/binding/field.go b/internal/generator/binding/field.go index 0fb17df..a28c5e5 100644 --- a/internal/generator/binding/field.go +++ b/internal/generator/binding/field.go @@ -35,6 +35,7 @@ type Field struct { Name string Optional string IsSkipped bool + IsFlex bool } func CreateField(prop *model.Property) *Field { @@ -110,6 +111,15 @@ func (field *Field) ProcessAnnotations(a map[string]*Annotation) error { field.ModelProperty.AddFlag(model.PropertyFlagIdCompanion) } + if a["flex"] != nil { + if field.ModelProperty.Type != model.PropertyTypeByteVector { + return errors.New("flex must be used with type [byte]") + } + // Overwrite recognized type. + field.ModelProperty.Type = model.PropertyTypeFlex + field.IsFlex = true + } + if a["unique"] != nil { field.ModelProperty.AddFlag(model.PropertyFlagUnique) diff --git a/internal/generator/c/cgenerator.go b/internal/generator/c/cgenerator.go index 5eaa1a7..f9b9c43 100644 --- a/internal/generator/c/cgenerator.go +++ b/internal/generator/c/cgenerator.go @@ -124,13 +124,33 @@ func (gen *CGenerator) generateBindingFile(bindingFile, headerFile string, m *mo var fileIdentifier = strings.ToLower(filepath.Base(bindingFile)) fileIdentifier = replaceSpecialChars.Replace(fileIdentifier) + // Remove flex properties only for binding code generation: + // create a (shallow) copy of the ModelInfo struct, + // create copies of each Entity and replace the list + // of properties with a filtered one. + storedModelCopy := *m + storedModelCopy.Entities = nil + for _, e := range m.Entities { + entityCopy := *e + storedModelCopy.Entities = append(storedModelCopy.Entities, &entityCopy) + } + for _, e := range storedModelCopy.Entities { + var filteredProperties []*model.Property + for _, p := range e.Properties { + if !p.Meta.(*fbsField).IsFlex { + filteredProperties = append(filteredProperties, p) + } + } + e.Properties = filteredProperties + } + var tplArguments = struct { Model *model.ModelInfo GeneratorVersion int FileIdentifier string HeaderFile string Optional string - }{m, generator.VersionId, fileIdentifier, filepath.Base(headerFile), gen.Optional} + }{&storedModelCopy, generator.VersionId, fileIdentifier, filepath.Base(headerFile), gen.Optional} var tpl *template.Template diff --git a/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected b/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected index f379d04..a9fc8ca 100644 --- a/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected +++ b/test/comparison/testdata/fbs/typeful/c/objectbox-model.h.expected @@ -85,9 +85,10 @@ static inline OBX_model* create_obx_model() { obx_model_property(model, "uid", OBXPropertyType_Int, 9, 6972732843819909978); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_UNIQUE)); obx_model_property_index_id(model, 8, 5558237345453186302); + obx_model_property(model, "flexValue", OBXPropertyType_Flex, 10, 7845762441295307478); obx_model_relation(model, 1, 7845762441295307478, 1, 8717895732742165505); obx_model_relation(model, 2, 771642788862502430, 1, 8717895732742165505); - obx_model_entity_last_property_id(model, 9, 6972732843819909978); + obx_model_entity_last_property_id(model, 10, 7845762441295307478); obx_model_entity(model, "TSDate", 3, 6050128673802995827); obx_model_property(model, "id", OBXPropertyType_Long, 1, 8514850266767180993); diff --git a/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected b/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected index f379d04..a9fc8ca 100644 --- a/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected +++ b/test/comparison/testdata/fbs/typeful/cpp/objectbox-model.h.expected @@ -85,9 +85,10 @@ static inline OBX_model* create_obx_model() { obx_model_property(model, "uid", OBXPropertyType_Int, 9, 6972732843819909978); obx_model_property_flags(model, (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_UNIQUE)); obx_model_property_index_id(model, 8, 5558237345453186302); + obx_model_property(model, "flexValue", OBXPropertyType_Flex, 10, 7845762441295307478); obx_model_relation(model, 1, 7845762441295307478, 1, 8717895732742165505); obx_model_relation(model, 2, 771642788862502430, 1, 8717895732742165505); - obx_model_entity_last_property_id(model, 9, 6972732843819909978); + obx_model_entity_last_property_id(model, 10, 7845762441295307478); obx_model_entity(model, "TSDate", 3, 6050128673802995827); obx_model_property(model, "id", OBXPropertyType_Long, 1, 8514850266767180993);