Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from Showmax/fix_url
Browse files Browse the repository at this point in the history
Add parsing for URL structure
  • Loading branch information
jan-dubsky authored Dec 15, 2020
2 parents 046a6f5 + 117eb3d commit ee8163d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion env.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ func defaultParsers() map[reflect.Type]parseFunc {
reflect.TypeOf(string("")): parseString,
reflect.TypeOf(&regexp.Regexp{}): parseRegex,
reflect.TypeOf(time.Duration(0)): parseDuration,
reflect.TypeOf(&url.URL{}): parseURL,
reflect.TypeOf(url.URL{}): parseURL,
reflect.TypeOf(&url.URL{}): parseURLPtr,
reflect.TypeOf(&tt.Template{}): parseTextTemplate,
}
}
Expand Down Expand Up @@ -421,6 +422,15 @@ func parseDuration(s string) (interface{}, error) {
}

func parseURL(s string) (interface{}, error) {
url, err := url.Parse(s)
if err != nil {
return nil, err
}

return *url, nil
}

func parseURLPtr(s string) (interface{}, error) {
return url.Parse(s)
}

Expand Down
7 changes: 7 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package env
import (
"fmt"
"math"
"net/url"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -30,6 +31,8 @@ type config struct {
IntSlice *[]int `env:"INT_SLICE"`
String string `env:"STRING"`
StringSlice []string `env:"STRING_SLICE"`
URLValue url.URL `env:"URL_VALUE"`
URLPtr *url.URL `env:"URL_PTR"`
badConfig int //nolint:structcheck,unused
}

Expand Down Expand Up @@ -62,6 +65,8 @@ var (
"INT_SLICE": `1,2,"3"`,
"STRING": "STRING",
"STRING_SLICE": `"comma separated",values`,
"URL_VALUE": "https://example.org",
"URL_PTR": "https://example.org",
}
invalidVars = environment{
"BOOL": "flase",
Expand All @@ -81,6 +86,8 @@ var (
IntSlice: &[]int{1, 2, 3},
String: "STRING",
StringSlice: []string{"comma separated", "values"},
URLValue: url.URL{Scheme: "https", Host: "example.org"},
URLPtr: &url.URL{Scheme: "https", Host: "example.org"},
}
)

Expand Down

0 comments on commit ee8163d

Please sign in to comment.