Skip to content

Commit

Permalink
Add support for url.URL
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSLevy committed Jul 24, 2020
1 parent 9a33856 commit fb6e26d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import (
"encoding/json"
"flag"
"fmt"
"net/url"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -396,6 +397,9 @@ func (b bind) bind(fs FlagSet, v interface{}) (err error) {
_, isBinder := fieldI.(Binder)

_, isFlagValue := fieldI.(flag.Value)
_, isJSONRawMessage := fieldI.(*json.RawMessage)
_, isURL := fieldI.(*url.URL)
isFlagValue = isFlagValue || isJSONRawMessage || isURL

isStruct := fieldT.Kind() == reflect.Struct

Expand Down Expand Up @@ -530,6 +534,8 @@ func bindSTDFlag(fs STDFlagSet, tag flagTag, p interface{}) bool {
fs.Var(p, tag.Name, tag.Usage)
case *json.RawMessage:
fs.Var((*JSONRawMessage)(p), tag.Name, tag.Usage)
case *url.URL:
fs.Var((*URL)(p), tag.Name, tag.Usage)
case *bool:
val := *p
fs.BoolVar(p, tag.Name, val, tag.Usage)
Expand Down Expand Up @@ -580,6 +586,8 @@ func bindPFlag(fs PFlagSet, tag flagTag, p interface{}, typeName string) bool {
f = fs.VarPF(pp, tag.Name, tag.ShortName, tag.Usage)
case *json.RawMessage:
f = fs.VarPF((*JSONRawMessage)(p), tag.Name, tag.ShortName, tag.Usage)
case *url.URL:
f = fs.VarPF((*URL)(p), tag.Name, tag.ShortName, tag.Usage)
case *bool:
val := *p
fs.BoolVarP(p, tag.Name, tag.ShortName, val, tag.Usage)
Expand Down
21 changes: 21 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -202,6 +203,9 @@ type ValidTestFlags struct {

ExportedInterface interface{}

CustomURLPtr *url.URL
CustomURL url.URL

custom bool
}

Expand Down Expand Up @@ -287,6 +291,10 @@ var tests = []BindTest{
"-struct-b-bool",
"-nested-struct-a-bool",
"-embedded-struct-b-bool",
"-custom-url",
"http://example.com",
"-custom-url-ptr",
"http://example.com",
"-custom",
},
ExpF: &ValidTestFlags{
Expand Down Expand Up @@ -327,6 +335,11 @@ var tests = []BindTest{
NestedFlat: StructB{true},
StructA: StructA{true, false},
StructB: StructB{true},
CustomURL: func() url.URL {
u := mustParseURL("http://example.com")
return *u
}(),
CustomURLPtr: mustParseURL("http://example.com"),
custom: true,
},
}, {
Expand Down Expand Up @@ -436,3 +449,11 @@ var tests = []BindTest{
}{http.Client{Timeout: 5 * time.Second}},
},
}

func mustParseURL(rawurl string) *url.URL {
u, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
return u
}
20 changes: 20 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package flagbind

import "net/url"

type URL url.URL

func (u *URL) Set(text string) error {
_u, err := url.Parse(text)
if err != nil {
return err
}
*u = (URL)(*_u)
return nil
}

func (u URL) String() string {
return (*url.URL)(&u).String()
}

func (u URL) Type() string { return "URL" }

0 comments on commit fb6e26d

Please sign in to comment.