Use StringMap instead of the map[string]any on property fields

This commit is contained in:
Miguel de la Cruz" (aider) 2025-01-08 17:51:46 +01:00 committed by Miguel de la Cruz
parent 2fb8664d71
commit e2735ab0f8
3 changed files with 30 additions and 22 deletions

View File

@ -27,12 +27,16 @@ var propertyFieldColumns = []string{
"DeleteAt",
}
func propertyFieldToInsertMap(field *model.PropertyField) (map[string]any, error) {
func (s *SqlPropertyFieldStore) propertyFieldToInsertMap(field *model.PropertyField) (map[string]any, error) {
attrsJSON, err := json.Marshal(field.Attrs)
if err != nil {
return nil, errors.Wrap(err, "property_field_to_insert_map_marshal_attrs")
}
if s.IsBinaryParamEnabled() {
attrsJSON = AppendBinaryFlag(attrsJSON)
}
return map[string]any{
"ID": field.ID,
"GroupID": field.GroupID,
@ -47,12 +51,16 @@ func propertyFieldToInsertMap(field *model.PropertyField) (map[string]any, error
}, nil
}
func propertyFieldToUpdateMap(field *model.PropertyField) (map[string]any, error) {
func (s *SqlPropertyFieldStore) propertyFieldToUpdateMap(field *model.PropertyField) (map[string]any, error) {
attrsJSON, err := json.Marshal(field.Attrs)
if err != nil {
return nil, errors.Wrap(err, "property_field_to_update_map_marshal_attrs")
}
if s.IsBinaryParamEnabled() {
attrsJSON = AppendBinaryFlag(attrsJSON)
}
return map[string]any{
"Name": field.Name,
"Type": field.Type,
@ -129,7 +137,7 @@ func (s *SqlPropertyFieldStore) Create(field *model.PropertyField) (*model.Prope
return nil, errors.Wrap(err, "property_field_create_isvalid")
}
insertMap, err := propertyFieldToInsertMap(field)
insertMap, err := s.propertyFieldToInsertMap(field)
if err != nil {
return nil, err
}
@ -271,7 +279,7 @@ func (s *SqlPropertyFieldStore) Update(fields []*model.PropertyField) (_ []*mode
return nil, errors.Wrap(err, "property_field_update_isvalid")
}
updateMap, err := propertyFieldToUpdateMap(field)
updateMap, err := s.propertyFieldToUpdateMap(field)
if err != nil {
return nil, err
}

View File

@ -48,8 +48,8 @@ func testCreatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
GroupID: model.NewId(),
Name: "My new property field",
Type: model.PropertyFieldTypeText,
Attrs: map[string]any{
"locked": true,
Attrs: model.StringMap{
"locked": "true",
"special": "value",
},
}
@ -83,8 +83,8 @@ func testGetPropertyField(t *testing.T, _ request.CTX, ss store.Store) {
GroupID: model.NewId(),
Name: "My new property field",
Type: model.PropertyFieldTypeText,
Attrs: map[string]any{
"locked": true,
Attrs: model.StringMap{
"locked": "true",
"special": "value",
},
}
@ -95,7 +95,7 @@ func testGetPropertyField(t *testing.T, _ request.CTX, ss store.Store) {
field, err := ss.PropertyField().Get(newField.ID)
require.NoError(t, err)
require.Equal(t, newField.ID, field.ID)
require.True(t, field.Attrs["locked"].(bool))
require.Equal(t, "true", field.Attrs["locked"])
require.Equal(t, "value", field.Attrs["special"])
})
}
@ -177,8 +177,8 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
GroupID: model.NewId(),
Name: "First field",
Type: model.PropertyFieldTypeText,
Attrs: map[string]any{
"locked": true,
Attrs: model.StringMap{
"locked": "true",
"special": "value",
},
}
@ -187,8 +187,8 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
GroupID: model.NewId(),
Name: "Second field",
Type: model.PropertyFieldTypeSelect,
Attrs: map[string]any{
"options": []string{"a", "b"},
Attrs: model.StringMap{
"options": "a,b",
},
}
@ -201,14 +201,14 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
field1.Name = "Updated first"
field1.Type = model.PropertyFieldTypeSelect
field1.Attrs = map[string]any{
"locked": false,
field1.Attrs = model.StringMap{
"locked": "false",
"new_field": "new_value",
}
field2.Name = "Updated second"
field2.Attrs = map[string]any{
"options": []string{"x", "y", "z"},
field2.Attrs = model.StringMap{
"options": "x,y,z",
}
_, err := ss.PropertyField().Update([]*model.PropertyField{field1, field2})
@ -219,7 +219,7 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
require.NoError(t, err)
require.Equal(t, "Updated first", updated1.Name)
require.Equal(t, model.PropertyFieldTypeSelect, updated1.Type)
require.False(t, updated1.Attrs["locked"].(bool))
require.Equal(t, "false", updated1.Attrs["locked"])
require.NotContains(t, updated1.Attrs, "special")
require.Equal(t, "new_value", updated1.Attrs["new_field"])
require.Greater(t, updated1.UpdateAt, updated1.CreateAt)
@ -229,7 +229,7 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
require.NoError(t, err)
require.Equal(t, "Updated second", updated2.Name)
require.Equal(t, model.PropertyFieldTypeSelect, updated2.Type)
require.ElementsMatch(t, []string{"x", "y", "z"}, updated2.Attrs["options"])
require.Equal(t, "x,y,z", updated2.Attrs["options"])
require.Greater(t, updated2.UpdateAt, updated2.CreateAt)
})
@ -240,7 +240,7 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
GroupID: groupID,
Name: "Field 1",
Type: model.PropertyFieldTypeText,
Attrs: map[string]any{
Attrs: model.StringMap{
"key": "value",
},
}
@ -249,7 +249,7 @@ func testUpdatePropertyField(t *testing.T, _ request.CTX, ss store.Store) {
GroupID: groupID,
Name: "Field 2",
Type: model.PropertyFieldTypeText,
Attrs: map[string]any{
Attrs: model.StringMap{
"key": "value",
},
}

View File

@ -21,7 +21,7 @@ type PropertyField struct {
GroupID string `json:"group_id"`
Name string `json:"name"`
Type PropertyFieldType `json:"type"`
Attrs map[string]any `json:"attrs"`
Attrs StringMap `json:"attrs"`
TargetID string `json:"target_id"`
TargetType string `json:"target_type"`
CreateAt int64 `json:"create_at"`