diff options
Diffstat (limited to 'vendor/github.com/alexflint/go-scalar')
| -rw-r--r-- | vendor/github.com/alexflint/go-scalar/.gitignore | 24 | ||||
| -rw-r--r-- | vendor/github.com/alexflint/go-scalar/.travis.yml | 9 | ||||
| -rw-r--r-- | vendor/github.com/alexflint/go-scalar/LICENSE | 24 | ||||
| -rw-r--r-- | vendor/github.com/alexflint/go-scalar/README.md | 28 | ||||
| -rw-r--r-- | vendor/github.com/alexflint/go-scalar/scalar.go | 154 |
5 files changed, 239 insertions, 0 deletions
diff --git a/vendor/github.com/alexflint/go-scalar/.gitignore b/vendor/github.com/alexflint/go-scalar/.gitignore new file mode 100644 index 0000000..daf913b --- /dev/null +++ b/vendor/github.com/alexflint/go-scalar/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/alexflint/go-scalar/.travis.yml b/vendor/github.com/alexflint/go-scalar/.travis.yml new file mode 100644 index 0000000..d4e2c01 --- /dev/null +++ b/vendor/github.com/alexflint/go-scalar/.travis.yml @@ -0,0 +1,9 @@ +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 diff --git a/vendor/github.com/alexflint/go-scalar/LICENSE b/vendor/github.com/alexflint/go-scalar/LICENSE new file mode 100644 index 0000000..a50c494 --- /dev/null +++ b/vendor/github.com/alexflint/go-scalar/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2015, Alex Flint +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/vendor/github.com/alexflint/go-scalar/README.md b/vendor/github.com/alexflint/go-scalar/README.md new file mode 100644 index 0000000..e6f510e --- /dev/null +++ b/vendor/github.com/alexflint/go-scalar/README.md @@ -0,0 +1,28 @@ +[](https://godoc.org/github.com/alexflint/go-scalar) +[](https://travis-ci.org/alexflint/go-scalar) +[](https://coveralls.io/github/alexflint/go-scalar?branch=master) +[](https://goreportcard.com/badge/github.com/alexflint/go-scalar) + +## Scalar parsing library + +Scalar is a library for parsing strings into arbitrary scalars (integers, +floats, strings, booleans, etc). It is helpful for tasks such as parsing +strings passed as environment variables or command line arguments. + +```shell +go get github.com/alexflint/go-scalar +``` + +The main API works as follows: + +```go +var value int +err := scalar.Parse(&value, "123") +``` + +There is also a variant that takes a `reflect.Value`: + +```go +var value int +err := scalar.ParseValue(reflect.ValueOf(&value), "123") +``` diff --git a/vendor/github.com/alexflint/go-scalar/scalar.go b/vendor/github.com/alexflint/go-scalar/scalar.go new file mode 100644 index 0000000..663f143 --- /dev/null +++ b/vendor/github.com/alexflint/go-scalar/scalar.go @@ -0,0 +1,154 @@ +// Package scalar parses strings into values of scalar type. + +package scalar + +import ( + "encoding" + "errors" + "fmt" + "net" + "net/mail" + "reflect" + "strconv" + "time" +) + +// The reflected form of some special types +var ( + textUnmarshalerType = reflect.TypeOf([]encoding.TextUnmarshaler{}).Elem() + durationType = reflect.TypeOf(time.Duration(0)) + mailAddressType = reflect.TypeOf(mail.Address{}) + ipType = reflect.TypeOf(net.IP{}) + macType = reflect.TypeOf(net.HardwareAddr{}) +) + +var ( + errNotSettable = errors.New("value is not settable") + errPtrNotSettable = errors.New("value is a nil pointer and is not settable") +) + +// Parse assigns a value to v by parsing s. +func Parse(dest interface{}, s string) error { + return ParseValue(reflect.ValueOf(dest), s) +} + +// ParseValue assigns a value to v by parsing s. +func ParseValue(v reflect.Value, s string) error { + // If we have a nil pointer then allocate a new object + if v.Kind() == reflect.Ptr && v.IsNil() { + if !v.CanSet() { + return errPtrNotSettable + } + + v.Set(reflect.New(v.Type().Elem())) + } + + // If it implements encoding.TextUnmarshaler then use that + if scalar, ok := v.Interface().(encoding.TextUnmarshaler); ok { + return scalar.UnmarshalText([]byte(s)) + } + + // If we have a pointer then dereference it + if v.Kind() == reflect.Ptr { + v = v.Elem() + } + + if !v.CanSet() { + return errNotSettable + } + + // Switch on concrete type + switch scalar := v.Interface(); scalar.(type) { + case time.Duration: + duration, err := time.ParseDuration(s) + if err != nil { + return err + } + v.Set(reflect.ValueOf(duration)) + return nil + case mail.Address: + addr, err := mail.ParseAddress(s) + if err != nil { + return err + } + v.Set(reflect.ValueOf(*addr)) + return nil + case net.IP: + ip := net.ParseIP(s) + if ip == nil { + return fmt.Errorf(`invalid IP address: "%s"`, s) + } + v.Set(reflect.ValueOf(ip)) + return nil + case net.HardwareAddr: + ip, err := net.ParseMAC(s) + if err != nil { + return err + } + v.Set(reflect.ValueOf(ip)) + return nil + } + + // Switch on kind so that we can handle derived types + switch v.Kind() { + case reflect.String: + v.SetString(s) + case reflect.Bool: + x, err := strconv.ParseBool(s) + if err != nil { + return err + } + v.SetBool(x) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + x, err := strconv.ParseInt(s, 10, 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()) + if err != nil { + return err + } + v.SetUint(x) + case reflect.Float32, reflect.Float64: + x, err := strconv.ParseFloat(s, v.Type().Bits()) + if err != nil { + return err + } + v.SetFloat(x) + default: + return fmt.Errorf("cannot parse into %v", v.Type()) + } + return nil +} + +// CanParse returns true if the type can be parsed from a string. +func CanParse(t reflect.Type) bool { + // If it implements encoding.TextUnmarshaler then use that + if t.Implements(textUnmarshalerType) { + return true + } + + // If we have a pointer then dereference it + if t.Kind() == reflect.Ptr { + t = t.Elem() + } + + // Check for other special types + switch t { + case durationType, mailAddressType, ipType, macType: + return true + } + + // Fall back to checking the kind + switch t.Kind() { + case reflect.Bool: + return true + case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, + reflect.Float32, reflect.Float64: + return true + } + return false +} |
