Skip to content

Commit

Permalink
Add numeric string check function + IsMap function. Extend tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
demdxx committed Sep 23, 2023
1 parent 0727649 commit 69c62cd
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
needs: lint
strategy:
matrix:
go-version: [1.18.x, 1.19.x]
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand All @@ -53,7 +53,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go-version: [1.18.x, 1.19.x]
go-version: [1.18.x, 1.21.x]
steps:
- uses: actions/setup-go@v3
with:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ gocast.Number[float32]("2.12") // 2.12
gocast.Cast[float64]("2.") // 2.0

val, err := gocast.TryCast[int]("123.2") // 123, <nil>

res := gocast.Map[string, any](struct{ID int64}{ID: 1}) // map[string]any{"ID": 1}
```

```go
Expand Down Expand Up @@ -178,4 +180,3 @@ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2 changes: 1 addition & 1 deletion cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func ReflectTryToTypeContext(ctx context.Context, srcVal reflect.Value, t reflec
return TryNumber[float64](v.Interface())
case reflect.Slice, reflect.Array:
slice := reflect.New(t)
if err = TryAnySliceContext(ctx, slice.Interface(), v.Interface(), tags...); err == nil {
if err = TryToAnySliceContext(ctx, slice.Interface(), v.Interface(), tags...); err == nil {
return slice.Elem().Interface(), nil
}
case reflect.Map:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module github.com/demdxx/gocast/v2

go 1.18

require github.com/stretchr/testify v1.8.1
require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 4 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab h1:1S7USr8/C0Sgk4egxq4zZ07zYt2Xh1IiFp8hUMXH/us=
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
56 changes: 14 additions & 42 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,77 +139,49 @@ func ToUint64ByReflect(v reflect.Value) uint64 {
// ToUint64 from any other basic types
//
// Deprecated: Use Number[uint64](v) or Uint64 instead
func ToUint64(v any) uint64 {
return Number[uint64](v)
}
func ToUint64(v any) uint64 { return Number[uint64](v) }

// ToUint32 from any other basic types
//
// Deprecated: Use Number[uint32](v) or Uint32 instead
func ToUint32(v any) uint32 {
return uint32(ToUint64(v))
}
func ToUint32(v any) uint32 { return uint32(ToUint64(v)) }

// ToUint32 from any other basic types
//
// Deprecated: Use Number[uint16](v) or Uint16 instead
func ToUint16(v any) uint16 {
return uint16(ToUint64(v))
}
func ToUint16(v any) uint16 { return uint16(ToUint64(v)) }

// ToUint from any other basic types
//
// Deprecated: Use Number[uint](v) or Uint instead
func ToUint(v any) uint {
return uint(ToUint64(v))
}
func ToUint(v any) uint { return uint(ToUint64(v)) }

// Int from any other basic type
func Int(v any) int {
return Number[int](v)
}
func Int(v any) int { return Number[int](v) }

// Int8 from any other basic type
func Int8(v any) int8 {
return Number[int8](v)
}
func Int8(v any) int8 { return Number[int8](v) }

// Int16 from any other basic type
func Int16(v any) int16 {
return Number[int16](v)
}
func Int16(v any) int16 { return Number[int16](v) }

// Int32 from any other basic type
func Int32(v any) int32 {
return Number[int32](v)
}
func Int32(v any) int32 { return Number[int32](v) }

// Int64 from any other basic type
func Int64(v any) int64 {
return Number[int64](v)
}
func Int64(v any) int64 { return Number[int64](v) }

// Uint from any other basic type
func Uint(v any) uint {
return Number[uint](v)
}
func Uint(v any) uint { return Number[uint](v) }

// Uint8 from any other basic type
func Uint8(v any) uint8 {
return Number[uint8](v)
}
func Uint8(v any) uint8 { return Number[uint8](v) }

// Uint16 from any other basic type
func Uint16(v any) uint16 {
return Number[uint16](v)
}
func Uint16(v any) uint16 { return Number[uint16](v) }

// Uint32 from any other basic type
func Uint32(v any) uint32 {
return Number[uint32](v)
}
func Uint32(v any) uint32 { return Number[uint32](v) }

// Uint64 from any other basic type
func Uint64(v any) uint64 {
return Number[uint64](v)
}
func Uint64(v any) uint64 { return Number[uint64](v) }
2 changes: 1 addition & 1 deletion length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestLen(t *testing.T) {
assert.Equal(t, 4, Len([]float64{1, 2, 3, 4}))
assert.Equal(t, 4, Len(&[]float64{1, 2, 3, 4}))
assert.Equal(t, 3, Len([]byte("123")))
assert.Equal(t, 0, Len(([]int)(nil)))
assert.Equal(t, 0, Len([]int(nil)))

assert.Equal(t, 0, Len((map[string]any)(nil)))
assert.Equal(t, 1, Len(map[string]any{"a": 1}))
Expand Down
10 changes: 10 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ func ToStringMap(src any, recursive bool, tags ...string) (map[string]string, er
return TryMapFrom[string, string](src, recursive, tags...)
}

// IsMap checks if the input value is a Map/Object type
func IsMap(v any) bool {
switch v.(type) {
case map[any]any, map[string]any, map[string]string, map[string]int, map[int]int:
return true
default:
return reflect.ValueOf(v).Kind() == reflect.Map
}
}

///////////////////////////////////////////////////////////////////////////////
/// MARK: Helpers
///////////////////////////////////////////////////////////////////////////////
Expand Down
42 changes: 42 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gocast

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -48,4 +49,45 @@ func TestMap(t *testing.T) {
err = ToMap(&target4, map[string]any{"Foo": 1}, true)
assert.NoError(t, err)
assert.Equal(t, true, reflect.DeepEqual(map[string]int{"Foo": 1}, target4))

var target5 = map[string]float64{}
err = TryMapCopy(target5, struct{ Foo float64 }{Foo: 1.0}, true)
assert.NoError(t, err)
assert.Equal(t, true, reflect.DeepEqual(map[string]float64{"Foo": 1.0}, target5))
err = TryMapCopy(target5, map[string]any{"Foo": 1.0}, true)
assert.NoError(t, err)
assert.Equal(t, true, reflect.DeepEqual(map[string]float64{"Foo": 1.0}, target5))

target6 := MapContext[string, float32](context.TODO(), struct{ Foo float32 }{Foo: 1.0})
assert.Equal(t, true, reflect.DeepEqual(map[string]float32{"Foo": 1.0}, target6))

target7 := MapRecursiveContext[string, map[string]any](context.TODO(), struct{ Foo struct{ Bar string } }{Foo: struct{ Bar string }{Bar: "boo"}})
assert.Equal(t, true, reflect.DeepEqual(map[string]map[string]any{"Foo": {"Bar": "boo"}}, target7))
}

func TestIsMap(t *testing.T) {
tests := []struct {
src any
trg bool
}{
{src: map[string]string{"1": "2"}, trg: true},
{src: map[string]int{"1": 2}, trg: true},
{src: map[string]float64{"1": 2.0}, trg: true},
{src: map[string]float32{"1": 2.0}, trg: true},
{src: map[any]uint{"1": 2}, trg: true},
{src: map[any]uint8{"1": 2}, trg: true},
{src: map[any]uint16{"1": 2}, trg: true},
{src: 1, trg: false},
{src: "1", trg: false},
{src: []string{"1"}, trg: false},
{src: []int{1}, trg: false},
{src: []float64{1.0}, trg: false},
{src: []float32{1.0}, trg: false},
{src: []uint{1}, trg: false},
{src: []uint8{1}, trg: false},
}

for _, test := range tests {
assert.Equal(t, test.trg, IsMap(test.src))
}
}
58 changes: 58 additions & 0 deletions number.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,61 @@ func Number[R Numeric](v any) R {
res, _ := TryNumber[R](v)
return res
}

// IsNumeric returns true if input is a numeric
func IsNumericStr(s string) bool {
if len(s) > 0 && (s[0] == '-' || s[0] == '+') {
s = s[1:]
}
if len(s) == 0 || s[0] == '-' || s[0] == '+' {
return false
}
switch {
case strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X"):
return IsStrContainsOf(s[2:], "0123456789abcdefABCDEF")
case strings.HasPrefix(s, "0o") || strings.HasPrefix(s, "0O") || (s[0] == '0' && len(s) > 1 && s[1] >= '0' && s[1] <= '7'):
return IsStrContainsOf(s[2:], "01234567")
case strings.HasPrefix(s, "0b") || strings.HasPrefix(s, "0B"):
return IsStrContainsOf(s[2:], "01")
}
return IsNumeric10Str(s)
}

// IsNumeric10Str returns true if input string is a numeric in base 10
func IsNumeric10Str(s string) bool {
dot := false
esimbol := -1
isStart := false
for i, c := range s {
if c < '0' || c > '9' {
if c == '.' && !dot && esimbol == -1 {
dot = true
continue
}
if i == 0 && (c == '-' || c == '+') {
continue
}
if (c == 'e' || c == 'E') && isStart && esimbol == -1 {
esimbol = i
continue
}
if esimbol != -1 && (c == '-' || c == '+') && esimbol == i-1 {
continue
}
return false
} else {
isStart = true
}
}
return isStart && esimbol != len(s)-1
}

// IsNumericOnlyStr returns true if input string is a numeric only
func IsNumericOnlyStr(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}
24 changes: 24 additions & 0 deletions number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ func TestNumber(t *testing.T) {
assert.Equal(t, int(0), v)
assert.Error(t, err)
}

func TestIsNumeric(t *testing.T) {
trueTests := []string{
"101", "+.1", "101.0", "2.35E-01", "2.35e-01", "-2e-01", "+2e-01", "1e10", "1e+10",
"0x0aB1", "0b0101", "0B0101", "0o0123", "0777",
}
falseTests := []string{
"1x10", "-+2e-01", "2Ee-01", "3..14", "3.1.4", "-", ".", "E", "e", "x",
"0x0aB1x", "0b01b", "0o0123o", "0777x", "0x", "0b", "0o", "0x0aB1x", "0b01b", "0o0123o", "0777x",
"0x01YT", "0b0101012", "0o01238", "0778",
}

for _, v := range trueTests {
assert.True(t, IsNumericStr(v), v)
}

for _, v := range falseTests {
assert.False(t, IsNumericStr(v), v)
}

assert.True(t, IsNumericOnlyStr("0123456789"))
assert.False(t, IsNumericOnlyStr("0123456789abcdefABCDEF"))
assert.False(t, IsNumericOnlyStr("0.1"))
}
Loading

0 comments on commit 69c62cd

Please sign in to comment.