Skip to content

Commit 0a5d2ba

Browse files
authored
chore: treat whitespace TOPICS as empty (#553)
1 parent a2b53fc commit 0a5d2ba

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

internal/config/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ func (c *Config) parseEnvVariables(osInterface OSInterface) error {
277277
return env.Parse(c)
278278
}
279279

280+
func (c *Config) normalizeTopics() {
281+
if len(c.Topics) == 0 {
282+
return
283+
}
284+
285+
// If topics only contains whitespace entries, treat as empty
286+
// This handles cases like TOPICS=" " or TOPICS=" , "
287+
hasNonWhitespace := false
288+
for _, topic := range c.Topics {
289+
if strings.TrimSpace(topic) != "" {
290+
hasNonWhitespace = true
291+
break
292+
}
293+
}
294+
295+
if !hasNonWhitespace {
296+
c.Topics = []string{}
297+
}
298+
}
299+
280300
// GetService returns ServiceType with error checking
281301
func (c *Config) GetService() (ServiceType, error) {
282302
return ServiceTypeFromString(c.Service)
@@ -309,6 +329,9 @@ func ParseWithoutValidation(flags Flags, osInterface OSInterface) (*Config, erro
309329
return nil, err
310330
}
311331

332+
// Normalize topics: trim whitespace and filter out empty strings
333+
config.normalizeTopics()
334+
312335
return &config, nil
313336
}
314337

internal/config/config_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,82 @@ func TestConfigFilePath(t *testing.T) {
444444
})
445445
}
446446
}
447+
448+
func TestTopicsNormalization(t *testing.T) {
449+
tests := []struct {
450+
name string
451+
topicsEnv string
452+
expectedCount int
453+
expected []string
454+
description string
455+
}{
456+
{
457+
name: "normal topics",
458+
topicsEnv: "topic1,topic2,topic3",
459+
expectedCount: 3,
460+
expected: []string{"topic1", "topic2", "topic3"},
461+
description: "should parse normal comma-separated topics as-is",
462+
},
463+
{
464+
name: "topics with leading/trailing spaces",
465+
topicsEnv: " topic1 , topic2 , topic3 ",
466+
expectedCount: 3,
467+
expected: []string{" topic1 ", " topic2 ", " topic3 "},
468+
description: "should preserve topics with spaces (not trimmed)",
469+
},
470+
{
471+
name: "empty string only",
472+
topicsEnv: " ",
473+
expectedCount: 0,
474+
expected: []string{},
475+
description: "should treat whitespace-only as empty",
476+
},
477+
{
478+
name: "multiple spaces",
479+
topicsEnv: " ",
480+
expectedCount: 0,
481+
expected: []string{},
482+
description: "should treat multiple spaces as empty",
483+
},
484+
{
485+
name: "empty strings between commas",
486+
topicsEnv: " , , ",
487+
expectedCount: 0,
488+
expected: []string{},
489+
description: "should treat whitespace-only entries as empty",
490+
},
491+
{
492+
name: "mixed valid topics and whitespace",
493+
topicsEnv: "topic1, ,topic2",
494+
expectedCount: 3,
495+
expected: []string{"topic1", " ", "topic2"},
496+
description: "should preserve valid topics even with whitespace entries",
497+
},
498+
{
499+
name: "wildcard with spaces",
500+
topicsEnv: " * ",
501+
expectedCount: 1,
502+
expected: []string{" * "},
503+
description: "should preserve wildcard with spaces",
504+
},
505+
}
506+
507+
for _, tt := range tests {
508+
t.Run(tt.name, func(t *testing.T) {
509+
mockOS := &mockOS{
510+
envVars: map[string]string{
511+
"TOPICS": tt.topicsEnv,
512+
"API_KEY": "test",
513+
"API_JWT_SECRET": "test",
514+
"AES_ENCRYPTION_SECRET": "test",
515+
"POSTGRES_URL": "postgres://test",
516+
},
517+
}
518+
519+
cfg, err := config.ParseWithoutValidation(config.Flags{}, mockOS)
520+
assert.NoError(t, err)
521+
assert.Equal(t, tt.expectedCount, len(cfg.Topics), tt.description)
522+
assert.Equal(t, tt.expected, cfg.Topics, tt.description)
523+
})
524+
}
525+
}

0 commit comments

Comments
 (0)