Support url.URL.
This commit is contained in:
@@ -3,6 +3,7 @@ package kong
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -162,10 +163,11 @@ func (r *Registry) RegisterDefaults() *Registry {
|
|||||||
return nil
|
return nil
|
||||||
})).
|
})).
|
||||||
RegisterKind(reflect.Bool, boolMapper{}).
|
RegisterKind(reflect.Bool, boolMapper{}).
|
||||||
RegisterType(reflect.TypeOf(time.Time{}), timeDecoder()).
|
|
||||||
RegisterType(reflect.TypeOf(time.Duration(0)), durationDecoder()).
|
|
||||||
RegisterKind(reflect.Slice, sliceDecoder(r)).
|
RegisterKind(reflect.Slice, sliceDecoder(r)).
|
||||||
RegisterKind(reflect.Map, mapDecoder(r)).
|
RegisterKind(reflect.Map, mapDecoder(r)).
|
||||||
|
RegisterType(reflect.TypeOf(time.Time{}), timeDecoder()).
|
||||||
|
RegisterType(reflect.TypeOf(time.Duration(0)), durationDecoder()).
|
||||||
|
RegisterType(reflect.TypeOf(&url.URL{}), urlMapper()).
|
||||||
RegisterName("path", pathMapper(r)).
|
RegisterName("path", pathMapper(r)).
|
||||||
RegisterName("existingfile", existingFileMapper(r)).
|
RegisterName("existingfile", existingFileMapper(r)).
|
||||||
RegisterName("existingdir", existingDirMapper(r))
|
RegisterName("existingdir", existingDirMapper(r))
|
||||||
@@ -360,6 +362,17 @@ func existingDirMapper(r *Registry) MapperFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func urlMapper() MapperFunc {
|
||||||
|
return func(ctx *DecodeContext, target reflect.Value) error {
|
||||||
|
url, err := url.Parse(ctx.Scan.PopValue("url"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
target.Set(reflect.ValueOf(url))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SplitEscaped splits a string on a separator.
|
// SplitEscaped splits a string on a separator.
|
||||||
//
|
//
|
||||||
// It differs from strings.Split() in that the separator can exist in a field by escaping it with a \. eg.
|
// It differs from strings.Split() in that the separator can exist in a field by escaping it with a \. eg.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package kong
|
package kong
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -95,3 +96,15 @@ func TestMapWithNamedTypes(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, map[string]string{"FIRST": "5s", "SECOND": "10s"}, cli.TypedKey)
|
require.Equal(t, map[string]string{"FIRST": "5s", "SECOND": "10s"}, cli.TypedKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestURLMapper(t *testing.T) {
|
||||||
|
var cli struct {
|
||||||
|
URL *url.URL `arg:""`
|
||||||
|
}
|
||||||
|
p := mustNew(t, &cli)
|
||||||
|
_, err := p.Parse([]string{"http://w3.org"})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "http://w3.org", cli.URL.String())
|
||||||
|
_, err = p.Parse([]string{":foo"})
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user