Skip to content

Commit df0039c

Browse files
committed
refactor: consolidate bouncer validity parsing into single block
Address review feedback from Gemini and Copilot: - Consolidate the validity check into one if/else-if/else block - Check "revoked" first (new CrowdSec >= v1.7.x), fall back to "valid" (old versions) - Remove redundant double-parsing of both fields - Clearer control flow, no duplicate JSON lookups Fixes #47
1 parent 2861f92 commit df0039c

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

internal/api/handlers/health_diagnostics.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ func parseBouncersJSON(bouncerOutput string, computeStatus bool) ([]models.Bounc
3131
if ipAddr, err := jsonparser.GetString(value, "ip_address"); err == nil {
3232
bouncer.IPAddress = ipAddr
3333
}
34-
if valid, err := jsonparser.GetBoolean(value, "valid"); err == nil {
35-
bouncer.Valid = valid
36-
}
3734
if lastPull, err := jsonparser.GetString(value, "last_pull"); err == nil {
3835
for _, layout := range []string{time.RFC3339Nano, time.RFC3339} {
3936
if t, err := time.Parse(layout, lastPull); err == nil {
@@ -49,20 +46,17 @@ func parseBouncersJSON(bouncerOutput string, computeStatus bool) ([]models.Bounc
4946
bouncer.Version = version
5047
}
5148

52-
// CrowdSec >= v1.7.x replaced "valid: true" with "revoked: false".
53-
// Read the revoked field; if present, derive Valid from it.
49+
// Determine bouncer validity in a single block for clarity and efficiency.
50+
// CrowdSec >= v1.7.x uses "revoked", older versions use "valid".
5451
if revoked, err := jsonparser.GetBoolean(value, "revoked"); err == nil {
5552
bouncer.Revoked = revoked
5653
bouncer.Valid = !revoked
57-
}
58-
// If neither "valid" nor "revoked" was found (edge case),
59-
// treat the bouncer as valid — it exists in the list, so it was not deleted.
60-
if !bouncer.Valid {
61-
if _, validErr := jsonparser.GetBoolean(value, "valid"); validErr != nil {
62-
if _, revokedErr := jsonparser.GetBoolean(value, "revoked"); revokedErr != nil {
63-
bouncer.Valid = true
64-
}
65-
}
54+
} else if valid, err := jsonparser.GetBoolean(value, "valid"); err == nil {
55+
bouncer.Valid = valid
56+
} else {
57+
// Neither field present — the bouncer exists in the list,
58+
// so it was not deleted. Treat as valid.
59+
bouncer.Valid = true
6660
}
6761

6862
if computeStatus {

0 commit comments

Comments
 (0)