mirror of
https://github.com/mattermost/mattermost.git
synced 2026-09-24 16:05:00 +08:00
refactor: Update CustomProfileAttributesSelectOption constructor to prioritize ID parameter
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user