Skip to content

Commit

Permalink
Exclude flex properties when generating C/C++ binding code.
Browse files Browse the repository at this point in the history
  • Loading branch information
greenrobot-team committed Jan 18, 2022
1 parent bce720a commit 5c88fae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions internal/generator/binding/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Field struct {
Name string
Optional string
IsSkipped bool
IsFlex bool
}

func CreateField(prop *model.Property) *Field {
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 21 additions & 1 deletion internal/generator/c/cgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5c88fae

Please sign in to comment.