refactor: Update CustomProfileAttributesSelectOption constructor to prioritize ID parameter

This commit is contained in:
Julien Tant (aider)
2025-02-12 14:56:15 -07:00
parent dbd1728a14
commit bbb7f3e261
2 changed files with 21 additions and 5 deletions
@@ -60,9 +60,20 @@ type CustomProfileAttributesSelectOption struct {
}
func NewCustomProfileAttributesSelectOptionFromMap(m map[string]any) CustomProfileAttributesSelectOption {
id := ""
name := ""
color := ""
if v, ok := m["ID"]; ok {
if vStr, ok := v.(string); ok {
id = vStr
}
} else if v, ok := m["id"]; ok {
if vStr, ok := v.(string); ok {
id = vStr
}
}
if v, ok := m["Name"]; ok {
if vStr, ok := v.(string); ok {
name = vStr
@@ -83,12 +94,17 @@ func NewCustomProfileAttributesSelectOptionFromMap(m map[string]any) CustomProfi
}
}
return NewCustomProfileAttributesSelectOption(name, color)
return NewCustomProfileAttributesSelectOption(id, name, color)
}
func NewCustomProfileAttributesSelectOption(name, color string) CustomProfileAttributesSelectOption {
func NewCustomProfileAttributesSelectOption(id, name, color string) CustomProfileAttributesSelectOption {
optionID := id
if optionID == "" {
optionID = NewId()
}
return CustomProfileAttributesSelectOption{
ID: NewId(),
ID: optionID,
Name: strings.TrimSpace(name),
Color: strings.TrimSpace(color),
}
@@ -11,7 +11,7 @@ import (
func TestNewCustomProfileAttributeSelectOption(t *testing.T) {
t.Run("creates valid option with generated ID", func(t *testing.T) {
option := NewCustomProfileAttributesSelectOption("Test Option", "#FF0000")
option := NewCustomProfileAttributesSelectOption("", "Test Option", "#FF0000")
assert.NotEmpty(t, option.ID)
assert.True(t, IsValidId(option.ID))
@@ -20,7 +20,7 @@ func TestNewCustomProfileAttributeSelectOption(t *testing.T) {
})
t.Run("trims spaces from name and color", func(t *testing.T) {
option := NewCustomProfileAttributesSelectOption(" Test Option ", " #FF0000 ")
option := NewCustomProfileAttributesSelectOption("", " Test Option ", " #FF0000 ")
assert.Equal(t, "Test Option", option.Name)
assert.Equal(t, "#FF0000", option.Color)