{"record":{"id":"7b0fcf59e906bbea","repo":"siyuan-note/siyuan","slug":"duplicated-attribute-view-custom-color-index-d","errorCode":null,"errorMessage":"duplicated attribute view custom color index [%d]","messagePattern":"duplicated attribute view custom color index \\[(.+?)\\]","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/av/color.go","lineNumber":110,"sourceCode":"\n// NormalizeAttributeViewCustomColors 校验并规范化数据库自定义颜色。\nfunc NormalizeAttributeViewCustomColors(colors []*AttributeViewCustomColor, strict bool) (ret []*AttributeViewCustomColor, err error) {\n\tif strict && MaxCustomColors < len(colors) {\n\t\treturn nil, fmt.Errorf(\"attribute view custom colors count exceeds the %d item limit\", MaxCustomColors)\n\t}\n\n\tindexes := map[int]struct{}{}\n\tfor _, color := range colors {\n\t\tnormalized, normalizeErr := normalizeAttributeViewCustomColor(color)\n\t\tif nil != normalizeErr {\n\t\t\tif strict {\n\t\t\t\treturn nil, normalizeErr\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\tif _, ok := indexes[normalized.Index]; ok {\n\t\t\tif strict {\n\t\t\t\treturn nil, fmt.Errorf(\"duplicated attribute view custom color index [%d]\", normalized.Index)\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\tindexes[normalized.Index] = struct{}{}\n\t\tret = append(ret, normalized)\n\t}\n\n\tsort.Slice(ret, func(i, j int) bool {\n\t\treturn ret[i].Index < ret[j].Index\n\t})\n\tif nil == ret {\n\t\tret = []*AttributeViewCustomColor{}\n\t}\n\treturn\n}\n\nfunc normalizeAttributeViewCustomColor(color *AttributeViewCustomColor) (ret *AttributeViewCustomColor, err error) {\n\tif nil == color {","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/av/color.go#L92-L128","documentation":"NormalizeAttributeViewCustomColors requires each custom color to have a unique Index. In strict mode, encountering a second color with an already-seen index aborts with this error; in non-strict mode the duplicate is silently skipped. Indexes identify colors in the palette order, so duplicates are ambiguous.","triggerScenarios":"Calling NormalizeAttributeViewCustomColors (strict=true) with a list where two AttributeViewCustomColor entries share the same Index, e.g. via setAttrViewCustomColors with hand-built colors or a decoded historical palette containing duplicated indexes.","commonSituations":"Merging palettes from two workspaces without reassigning indexes; programmatic generation of colors using a non-unique counter; manual JSON edits copying an entry but forgetting to bump its index.","solutions":["Assign each color a unique Index within [CustomColorMinIndex, CustomColorMaxIndex] before saving","Deduplicate entries by Index, keeping the newest one","Renumber indexes sequentially after any merge or deletion"],"exampleFix":"// before\n{\"index\":1,\"light\":{...}}, {\"index\":1,\"light\":{...}}\n// after\n{\"index\":1,\"light\":{...}}, {\"index\":2,\"light\":{...}}","handlingStrategy":"validation","validationCode":"seen := map[int]bool{}\nfor _, c := range colors {\n    if seen[c.Index] { return fmt.Errorf(\"duplicate index %d\", c.Index) }\n    seen[c.Index] = true\n}","typeGuard":null,"tryCatchPattern":"if _, err := av.NormalizeAttributeViewCustomColors(colors, true); err != nil {\n    if strings.Contains(err.Error(), \"duplicated\") { renumber(colors) }\n}","preventionTips":["Generate indexes from a counter or list position, never from user input","After merging palettes, renumber all indexes sequentially","Deduplicate by Index before saving"],"tags":["attribute-view","duplicate","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}