package store import "strings" const ( DohPolicyPrimaryOnly = "primary_only" DohPolicyFailover = "failover" DohPolicyUnion = "union" ) // NormalizeDohResolverPolicy returns a supported resolver policy name. func NormalizeDohResolverPolicy(policy string) string { switch strings.TrimSpace(policy) { case DohPolicyFailover, DohPolicyUnion: return strings.TrimSpace(policy) default: return DohPolicyPrimaryOnly } } // NormalizeDohProfileIDList deduplicates profile ids preserving order. func NormalizeDohProfileIDList(ids []string) []string { if len(ids) == 0 { return nil } seen := make(map[string]struct{}, len(ids)) out := make([]string, 0, len(ids)) for _, id := range ids { id = strings.TrimSpace(id) if id == "" { continue } if _, ok := seen[id]; ok { continue } seen[id] = struct{}{} out = append(out, id) } if len(out) == 0 { return nil } return out } // EffectiveDohProfileIDs returns ordered DoH profile ids for a module. func (m *Module) EffectiveDohProfileIDs() []string { if m == nil { return nil } if ids := NormalizeDohProfileIDList(m.DohProfileIDs); len(ids) > 0 { return ids } if m.DohProfileID != nil { if id := strings.TrimSpace(*m.DohProfileID); id != "" { return []string{id} } } return nil } // SyncLegacyDohProfileID keeps deprecated doh_profile_id aligned with the first profile. func (m *Module) SyncLegacyDohProfileID() { if m == nil { return } ids := NormalizeDohProfileIDList(m.DohProfileIDs) if len(ids) == 0 { m.DohProfileID = nil return } first := ids[0] m.DohProfileID = &first } // NormalizeModuleDoh fills doh_profile_ids, policy and legacy id from module input. func NormalizeModuleDoh(m *Module) { if m == nil { return } ids := NormalizeDohProfileIDList(m.DohProfileIDs) if len(ids) == 0 && m.DohProfileID != nil { if id := strings.TrimSpace(*m.DohProfileID); id != "" { ids = []string{id} } } m.DohProfileIDs = ids m.DohResolverPolicy = NormalizeDohResolverPolicy(m.DohResolverPolicy) m.SyncLegacyDohProfileID() } // ApplyModuleDohPatch merges DoH-related fields from patch into mod. func ApplyModuleDohPatch(mod *Module, patch *ModulePatch) { if mod == nil || patch == nil { return } if patch.DohProfileIDs != nil { mod.DohProfileIDs = NormalizeDohProfileIDList(*patch.DohProfileIDs) } else if patch.DohProfileID != nil { v := strings.TrimSpace(*patch.DohProfileID) if v == "" { mod.DohProfileIDs = nil } else { mod.DohProfileIDs = []string{v} } } if patch.DohResolverPolicy != nil { mod.DohResolverPolicy = NormalizeDohResolverPolicy(*patch.DohResolverPolicy) } mod.SyncLegacyDohProfileID() }