This commit is contained in:
2026-08-16 17:16:47 +02:00
parent 532d439c41
commit d161b28a10
5 changed files with 170 additions and 21 deletions
+64
View File
@@ -79,6 +79,8 @@ func categoryUniqueIDFromAny(v any) string {
for _, k := range []string{
"unique_id", "category_unique_id", "categoryUniqueId",
"#text", "text", "id", "value", "code",
// Last: display name only (resolved to unique_id later via taxonomy).
"name",
} {
if s := categoryUniqueIDFromAny(t[k]); s != "" {
return s
@@ -163,8 +165,70 @@ func applyCategoryFromMapped(out *StepResult, maps ...map[string]any) {
out.FieldSources["category"] = "mapped"
}
// resolveCompanyCategoryUniqueID maps a feed/vector/prior category token onto a
// company taxonomy unique_id. Accepts an already-valid unique_id, or a display
// name (case-insensitive) from namesByUID. When valid is non-empty, the result
// must be in that set. Returns "" when unresolvable.
func resolveCompanyCategoryUniqueID(raw string, namesByUID map[string]string, valid map[string]struct{}) string {
raw = strings.TrimSpace(raw)
if raw == "" || strings.EqualFold(raw, "none") {
return ""
}
if len(valid) > 0 {
if _, ok := valid[raw]; ok {
return raw
}
} else if _, ok := namesByUID[raw]; ok {
return raw
}
if len(namesByUID) == 0 {
return ""
}
for uid, name := range namesByUID {
uid = strings.TrimSpace(uid)
if uid == "" {
continue
}
if !strings.EqualFold(strings.TrimSpace(name), raw) {
continue
}
if len(valid) == 0 {
return uid
}
if _, ok := valid[uid]; ok {
return uid
}
}
return ""
}
// coerceCategoryToCompanyUniqueID rewrites out.Category from a display name (or
// other alias) onto the taxonomy unique_id before filterCategoryIfInvalid.
// Without this, feed/vector category names are wiped even when they match categories.name.
func coerceCategoryToCompanyUniqueID(out *StepResult, namesByUID map[string]string, valid map[string]struct{}) {
if out == nil {
return
}
cat := strings.TrimSpace(out.Category)
if cat == "" {
return
}
resolved := resolveCompanyCategoryUniqueID(cat, namesByUID, valid)
if resolved == "" || resolved == cat {
return
}
out.Category = SanitizeText(resolved)
if out.FieldSources == nil {
out.FieldSources = map[string]any{}
}
if _, ok := out.FieldSources["category"]; !ok {
out.FieldSources["category"] = "name_coerced"
}
}
// filterCategoryIfInvalid clears Category when the company has a known unique_id set
// and the value is not in that set. Empty valid set means "skip validation" (tests / no taxonomy).
// Call coerceCategoryToCompanyUniqueID first so display names are not dropped.
func filterCategoryIfInvalid(out *StepResult, valid map[string]struct{}) {
if out == nil || len(valid) == 0 {
return