Skip to content

Commit

Permalink
refactor: some improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamid Reza Ranjbar committed Jun 2, 2024
1 parent 2db80cc commit 9695015
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
24 changes: 10 additions & 14 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"encoding"
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -327,10 +328,7 @@ func doParseField(refField reflect.Value, refTypeField reflect.StructField, proc
}

if isSliceOfStructs(refTypeField, opts) {
err := doParseSlice(refField, processField, optionsWithEnvPrefix(refTypeField, opts))
if len(err) > 0 {
return err[0]
}
return doParseSlice(refField, processField, optionsWithEnvPrefix(refTypeField, opts))
}

return nil
Expand Down Expand Up @@ -368,7 +366,7 @@ func isSliceOfStructs(refTypeField reflect.StructField, opts Options) bool {
return !ignore
}

func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options) []error {
func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options) error {
if !strings.HasSuffix(opts.Prefix, string(underscore)) {
opts.Prefix += string(underscore)
}
Expand All @@ -380,7 +378,6 @@ func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options)
}
}

var errors []error
if len(environments) > 0 {
counter := 0
for finished := false; !finished; {
Expand All @@ -405,31 +402,30 @@ func doParseSlice(ref reflect.Value, processField processFieldFn, opts Options)
initialized = ref.Len()
}
var capacity int
if capacity = counter; initialized > counter {
capacity = initialized
if capacity = initialized; counter > initialized {
capacity = counter
}
var errorList = make([]error, capacity)
result := reflect.MakeSlice(sliceType, capacity, capacity)
for i := 0; i < capacity; i++ {
item := result.Index(i)
if i < initialized {
item.Set(ref.Index(i))
}
err := doParse(item, processField, optionsWithSliceEnvPrefix(opts, i))
if err != nil {
errors = append(errors, err)
}
errorList[i] = doParse(item, processField, optionsWithSliceEnvPrefix(opts, i))
}

if reflect.Ptr == ref.Kind() {
resultPtr := reflect.New(sliceType)
resultPtr.Elem().Set(result)
result = resultPtr
}

ref.Set(result)

return errors.Join(errorList...)

Check failure on line 425 in env.go

View workflow job for this annotation

GitHub Actions / lint

undefined: errors.Join (typecheck)

Check failure on line 425 in env.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.18)

undefined: errors.Join
}

return errors
return nil
}

func setField(refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams) error {
Expand Down
2 changes: 1 addition & 1 deletion env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2085,7 +2085,7 @@ func TestIssue298(t *testing.T) {
}
type ComplexConfig struct {
Foo *[]Test `envPrefix:"FOO_"`
Bar []Test `envPrefix:"BAR_"`
Bar []Test `envPrefix:"BAR"`
Baz *Test
}

Expand Down

0 comments on commit 9695015

Please sign in to comment.