Compare commits
14 Commits
deal-with-
...
main
Author | SHA1 | Date |
---|---|---|
Jeff Carr | cd28260234 | |
Jeff Carr | 6080327a64 | |
Alex Flint | 489daf05a6 | |
Alex Flint | 706bcb4ea6 | |
Alex Flint | de2dbbd181 | |
Alex Flint | bc4a9b4370 | |
Alex Flint | e7a360897d | |
Daniele Sluijters | 08e5e4d956 | |
Alex Flint | b3bcd8035e | |
Alex Flint | 8ca9e7daec | |
Alex Flint | fd60e96695 | |
Alex Flint | 9e23a0e262 | |
Alex Flint | f33108a28e | |
Alex Flint | 7fa3c9e833 |
|
@ -15,12 +15,12 @@ jobs:
|
|||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
go: ['1.13', '1.14', '1.15', '1.16']
|
||||
go: ['1.17', '1.18', '1.19']
|
||||
|
||||
steps:
|
||||
- id: go
|
||||
name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
|
||||
|
|
|
@ -22,3 +22,5 @@ _testmain.go
|
|||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
go.*
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
language: go
|
||||
go:
|
||||
- tip
|
||||
before_install:
|
||||
- go get github.com/axw/gocov/gocov
|
||||
- go get github.com/mattn/goveralls
|
||||
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
|
||||
script:
|
||||
- $HOME/gopath/bin/goveralls -service=travis-ci
|
|
@ -0,0 +1,10 @@
|
|||
all:
|
||||
@echo
|
||||
@echo
|
||||
|
||||
test:
|
||||
|
||||
redomod:
|
||||
rm -f go.*
|
||||
GO111MODULE= go mod init
|
||||
GO111MODULE= go mod tidy
|
9
go.mod
9
go.mod
|
@ -1,9 +0,0 @@
|
|||
module github.com/alexflint/go-scalar
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.2.2
|
||||
)
|
6
go.sum
6
go.sum
|
@ -1,6 +0,0 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
15
scalar.go
15
scalar.go
|
@ -8,6 +8,7 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
|
@ -19,6 +20,7 @@ var (
|
|||
durationType = reflect.TypeOf(time.Duration(0))
|
||||
mailAddressType = reflect.TypeOf(mail.Address{})
|
||||
macType = reflect.TypeOf(net.HardwareAddr{})
|
||||
urlType = reflect.TypeOf(url.URL{})
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -86,6 +88,13 @@ func ParseValue(v reflect.Value, s string) error {
|
|||
}
|
||||
v.Set(reflect.ValueOf(ip))
|
||||
return nil
|
||||
case url.URL:
|
||||
url, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.Set(reflect.ValueOf(*url))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Switch on kind so that we can handle derived types
|
||||
|
@ -99,13 +108,13 @@ func ParseValue(v reflect.Value, s string) error {
|
|||
}
|
||||
v.SetBool(x)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
x, err := strconv.ParseInt(s, 10, v.Type().Bits())
|
||||
x, err := strconv.ParseInt(s, 0, v.Type().Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.SetInt(x)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
x, err := strconv.ParseUint(s, 10, v.Type().Bits())
|
||||
x, err := strconv.ParseUint(s, 0, v.Type().Bits())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -136,7 +145,7 @@ func CanParse(t reflect.Type) bool {
|
|||
|
||||
// Check for other special types
|
||||
switch t {
|
||||
case durationType, mailAddressType, macType:
|
||||
case durationType, mailAddressType, macType, urlType:
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
package scalar
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type textUnmarshaler struct {
|
||||
val int
|
||||
}
|
||||
|
||||
func (f *textUnmarshaler) UnmarshalText(b []byte) error {
|
||||
f.val = len(b)
|
||||
return nil
|
||||
}
|
||||
|
||||
func assertParse(t *testing.T, expected interface{}, str string) {
|
||||
v := reflect.New(reflect.TypeOf(expected)).Elem()
|
||||
err := ParseValue(v, str)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, expected, v.Interface())
|
||||
}
|
||||
|
||||
ptr := reflect.New(reflect.PtrTo(reflect.TypeOf(expected))).Elem()
|
||||
err = ParseValue(ptr, str)
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, expected, ptr.Elem().Interface())
|
||||
}
|
||||
|
||||
assert.True(t, CanParse(v.Type()))
|
||||
assert.True(t, CanParse(ptr.Type()))
|
||||
}
|
||||
|
||||
func TestParseValue(t *testing.T) {
|
||||
// strings
|
||||
assertParse(t, "abc", "abc")
|
||||
|
||||
// booleans
|
||||
assertParse(t, true, "true")
|
||||
assertParse(t, false, "false")
|
||||
|
||||
// integers
|
||||
assertParse(t, int(123), "123")
|
||||
assertParse(t, int8(123), "123")
|
||||
assertParse(t, int16(123), "123")
|
||||
assertParse(t, int32(123), "123")
|
||||
assertParse(t, int64(123), "123")
|
||||
|
||||
// unsigned integers
|
||||
assertParse(t, uint(123), "123")
|
||||
assertParse(t, byte(123), "123")
|
||||
assertParse(t, uint8(123), "123")
|
||||
assertParse(t, uint16(123), "123")
|
||||
assertParse(t, uint32(123), "123")
|
||||
assertParse(t, uint64(123), "123")
|
||||
assertParse(t, uintptr(123), "123")
|
||||
assertParse(t, rune(123), "123")
|
||||
|
||||
// floats
|
||||
assertParse(t, float32(123), "123")
|
||||
assertParse(t, float64(123), "123")
|
||||
|
||||
// durations
|
||||
assertParse(t, 3*time.Hour+15*time.Minute, "3h15m")
|
||||
|
||||
// IP addresses
|
||||
assertParse(t, net.IPv4(1, 2, 3, 4), "1.2.3.4")
|
||||
|
||||
// MAC addresses
|
||||
assertParse(t, net.HardwareAddr("\x01\x23\x45\x67\x89\xab"), "01:23:45:67:89:ab")
|
||||
|
||||
// MAC addresses
|
||||
assertParse(t, net.HardwareAddr("\x01\x23\x45\x67\x89\xab"), "01:23:45:67:89:ab")
|
||||
|
||||
// custom text unmarshaler
|
||||
assertParse(t, textUnmarshaler{3}, "abc")
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
var v int
|
||||
err := Parse(&v, "123")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 123, v)
|
||||
}
|
Loading…
Reference in New Issue