package engine import "fmt" // DB code encoders/decoders. // // These lowercase snake_case codes are the SINGLE SOURCE OF TRUTH for the SQL CHECK constraints in // db/migrations/0002_matching.sql. Keep two distinct representations per enum: // - String() — a human label for UI/explanations ("TS/SCI", "Full-Scope Poly", "Green"). // - Code() — the persisted DB value ("ts_sci", "full_scope", "green"). // Persisting String() instead of Code() was a real bug class (review INT-1/INT-2): "Green" and // "TS/SCI" both violate the CHECK. The persistence layer MUST use Code()/Parse*, never String(). // TestEnumCodesMatchSQLCheckSets locks these to the migration's CHECK sets. var clearanceLevelCodes = map[ClearanceLevel]string{ ClearanceUnknown: "unknown", ClearanceNone: "none", ClearanceConfidential: "confidential", ClearanceSecret: "secret", ClearanceTopSecret: "top_secret", ClearanceTSSCI: "ts_sci", } // Code returns the DB literal for the clearance level. func (c ClearanceLevel) Code() string { return clearanceLevelCodes[c] } // ParseClearanceLevel decodes a DB literal back to the enum, erroring on an unknown code. func ParseClearanceLevel(code string) (ClearanceLevel, error) { for k, v := range clearanceLevelCodes { if v == code { return k, nil } } return ClearanceUnknown, fmt.Errorf("engine: invalid clearance level code %q", code) } var polygraphCodes = map[Polygraph]string{ PolyNone: "none", PolyCI: "ci", PolyFullScope: "full_scope", } // Code returns the DB literal for the polygraph. func (p Polygraph) Code() string { return polygraphCodes[p] } // ParsePolygraph decodes a DB literal back to the enum, erroring on an unknown code. func ParsePolygraph(code string) (Polygraph, error) { for k, v := range polygraphCodes { if v == code { return k, nil } } return PolyNone, fmt.Errorf("engine: invalid polygraph code %q", code) } var clearanceStatusCodes = map[ClearanceStatus]string{ StatusUnknown: "unknown", StatusActive: "active", StatusExpired: "expired", StatusInactive: "inactive", } // String returns a human label for the clearance status (was missing — review INT-2). func (s ClearanceStatus) String() string { switch s { case StatusActive: return "Active" case StatusExpired: return "Expired" case StatusInactive: return "Inactive" default: return "Unknown" } } // Code returns the DB literal for the clearance status. func (s ClearanceStatus) Code() string { return clearanceStatusCodes[s] } // ParseClearanceStatus decodes a DB literal back to the enum, erroring on an unknown code. func ParseClearanceStatus(code string) (ClearanceStatus, error) { for k, v := range clearanceStatusCodes { if v == code { return k, nil } } return StatusUnknown, fmt.Errorf("engine: invalid clearance status code %q", code) } var bandCodes = map[Band]string{ BandRed: "red", BandYellow: "yellow", BandGreen: "green", } // Code returns the DB literal for the band. func (b Band) Code() string { return bandCodes[b] } // ParseBand decodes a DB literal back to the enum, erroring on an unknown code. func ParseBand(code string) (Band, error) { for k, v := range bandCodes { if v == code { return k, nil } } return BandRed, fmt.Errorf("engine: invalid band code %q", code) }