@@ -309,3 +309,110 @@ func (c *CustomTextUnmarshaler) UnmarshalText(text []byte) error {
309309func ptr [T any ](t T ) * T {
310310 return & t
311311}
312+
313+ func TestEmptySliceMapParsing (t * testing.T ) {
314+ type Config struct {
315+ Strings []string `env:"STRINGS"`
316+ IntMap map [string ]int `env:"INT_MAP"`
317+ }
318+
319+ le := func (key string ) (string , bool ) {
320+ switch key {
321+ case "STRINGS" :
322+ return "" , true
323+ case "INT_MAP" :
324+ return "" , true
325+ }
326+ return "" , false
327+ }
328+
329+ var cfg Config
330+ err := envconfig .Read (& cfg , le )
331+ if err != nil {
332+ t .Errorf ("Unexpected error: %v" , err )
333+ }
334+
335+ if cfg .Strings != nil {
336+ t .Errorf ("Expected nil slice, got %v" , cfg .Strings )
337+ }
338+ if cfg .IntMap != nil {
339+ t .Errorf ("Expected nil map, got %v" , cfg .IntMap )
340+ }
341+ }
342+
343+ func TestNestedStructPrefixHandling (t * testing.T ) {
344+ type SubConfig struct {
345+ Port int `env:"PORT"`
346+ }
347+
348+ type Config struct {
349+ Sub1 SubConfig `envPrefix:"APP1"`
350+ Sub2 SubConfig `envPrefix:"APP2"`
351+ }
352+
353+ le := func (key string ) (string , bool ) {
354+ switch key {
355+ case "APP1_PORT" :
356+ return "8080" , true
357+ case "APP2_PORT" :
358+ return "9090" , true
359+ }
360+ return "" , false
361+ }
362+
363+ var cfg Config
364+ err := envconfig .Read (& cfg , le )
365+ if err != nil {
366+ t .Errorf ("Unexpected error: %v" , err )
367+ }
368+
369+ if cfg .Sub1 .Port != 8080 || cfg .Sub2 .Port != 9090 {
370+ t .Errorf ("Incorrect port values: Sub1=%d, Sub2=%d" , cfg .Sub1 .Port , cfg .Sub2 .Port )
371+ }
372+ }
373+
374+ func TestEmptyStringDefaultValue (t * testing.T ) {
375+ type Config struct {
376+ Value string `env:"VALUE" envDefault:"default"`
377+ }
378+
379+ le := func (key string ) (string , bool ) {
380+ if key == "VALUE" {
381+ return "" , true // Explicitly set to empty
382+ }
383+ return "" , false
384+ }
385+
386+ var cfg Config
387+ err := envconfig .Read (& cfg , le )
388+ if err != nil {
389+ t .Errorf ("Unexpected error: %v" , err )
390+ }
391+
392+ if cfg .Value != "" {
393+ t .Errorf ("Expected empty string, got %q" , cfg .Value )
394+ }
395+ }
396+
397+ func TestEmptySlice (t * testing.T ) {
398+ type Config struct {
399+ Value []string `env:"VALUE"`
400+ }
401+
402+ le := func (key string ) (string , bool ) {
403+ if key == "VALUE" {
404+ return ",,,," , true
405+ }
406+ return "" , false
407+ }
408+
409+ var cfg Config
410+ err := envconfig .Read (& cfg , le )
411+ if err != nil {
412+ t .Errorf ("Unexpected error: %v" , err )
413+ }
414+
415+ if len (cfg .Value ) != 5 {
416+ t .Errorf ("Expected empty string, got %q" , cfg .Value )
417+ }
418+ }
0 commit comments