From 557a6a2fb72b0b1ffc57875f299e7842351886f6 Mon Sep 17 00:00:00 2001 From: yveshield Date: Tue, 16 Aug 2022 14:24:43 +0800 Subject: [PATCH 1/9] Add more integer to boolean conversions Signed-off-by: Yveshield --- cast_test.go | 28 ++++++++++++++++++++++++++++ caste.go | 29 +++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/cast_test.go b/cast_test.go index dc8ba14..016f8c2 100644 --- a/cast_test.go +++ b/cast_test.go @@ -796,6 +796,18 @@ func TestToBoolE(t *testing.T) { iserr bool }{ {0, false, false}, + {int64(0), false, false}, + {int32(0), false, false}, + {int16(0), false, false}, + {int8(0), false, false}, + {uint(0), false, false}, + {uint64(0), false, false}, + {uint32(0), false, false}, + {uint16(0), false, false}, + {uint8(0), false, false}, + {float64(0), false, false}, + {float32(0), false, false}, + {time.Duration(0), false, false}, {jf, false, false}, {nil, false, false}, {"false", false, false}, @@ -811,10 +823,26 @@ func TestToBoolE(t *testing.T) { {"t", true, false}, {"T", true, false}, {1, true, false}, + {int64(1), true, false}, + {int32(1), true, false}, + {int16(1), true, false}, + {int8(1), true, false}, + {uint(1), true, false}, + {uint64(1), true, false}, + {uint32(1), true, false}, + {uint16(1), true, false}, + {uint8(1), true, false}, + {float64(1), true, false}, + {float32(1), true, false}, + {time.Duration(1), true, false}, {jt, true, false}, {je, true, false}, {true, true, false}, {-1, true, false}, + {int64(-1), true, false}, + {int32(-1), true, false}, + {int16(-1), true, false}, + {int8(-1), true, false}, // errors {"test", false, true}, diff --git a/caste.go b/caste.go index 514d759..bccf27a 100644 --- a/caste.go +++ b/caste.go @@ -98,10 +98,31 @@ func ToBoolE(i interface{}) (bool, error) { case nil: return false, nil case int: - if i.(int) != 0 { - return true, nil - } - return false, nil + return b != 0, nil + case int64: + return b != 0, nil + case int32: + return b != 0, nil + case int16: + return b != 0, nil + case int8: + return b != 0, nil + case uint: + return b != 0, nil + case uint64: + return b != 0, nil + case uint32: + return b != 0, nil + case uint16: + return b != 0, nil + case uint8: + return b != 0, nil + case float64: + return b != 0, nil + case float32: + return b != 0, nil + case time.Duration: + return b != 0, nil case string: return strconv.ParseBool(i.(string)) case json.Number: From fa1a3528d1cbf0e9403c053d008eb5f208f935be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E8=80=85?= Date: Fri, 16 Dec 2022 09:09:04 +0800 Subject: [PATCH 2/9] Correction of hugo URL error Correction of hugo URL error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 120a573..4cb671e 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ interface into a bool, etc. Cast does this intelligently when an obvious conversion is possible. It doesn’t make any attempts to guess what you meant, for example you can only convert a string to an int when it is a string representation of an int such as “8”. Cast was developed for use in -[Hugo](http://hugo.spf13.com), a website engine which uses YAML, TOML or JSON +[Hugo](https://gohugo.io), a website engine which uses YAML, TOML or JSON for meta data. ## Why use Cast? From 160eccf352f68a06342e3c7963f56910b4f82944 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sun, 5 Feb 2023 21:17:13 +0100 Subject: [PATCH 3/9] ci: general ci improvements Signed-off-by: Mark Sagi-Kazar --- .github/.editorconfig | 2 ++ .github/workflows/ci.yml | 33 +++++++++++++++++++++++++++++++++ .github/workflows/go.yml | 28 ---------------------------- README.md | 7 +++---- 4 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 .github/.editorconfig create mode 100644 .github/workflows/ci.yml delete mode 100644 .github/workflows/go.yml diff --git a/.github/.editorconfig b/.github/.editorconfig new file mode 100644 index 0000000..0902c6a --- /dev/null +++ b/.github/.editorconfig @@ -0,0 +1,2 @@ +[{*.yml,*.yaml}] +indent_size = 2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1817575 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,33 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + # Fail fast is disabled because there are Go version specific features and tests + # that should be able to fail independently. + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + go-version: ['1.16', '1.17', '1.18'] + + steps: + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go }} + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -race -v ./... diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index c4f0f68..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Go - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - -jobs: - build: - strategy: - matrix: - go-version: [1.16.x, 1.17.x, 1.18.x] - os: [ubuntu-latest, windows-latest] - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go-version }} - - - name: Build - run: go build -v ./... - - - name: Test - run: go test -race -v ./... diff --git a/README.md b/README.md index 120a573..c68b910 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -cast -==== +# cast + [![GoDoc](https://godoc.org/github.com/spf13/cast?status.svg)](https://godoc.org/github.com/spf13/cast) -[![Build Status](https://github.com/spf13/cast/actions/workflows/go.yml/badge.svg)](https://github.com/spf13/cast/actions/workflows/go.yml) +[![Build Status](https://github.com/spf13/cast/actions/workflows/ci.yml/badge.svg)](https://github.com/spf13/cast/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast) Easy and safe casting from one type to another in Go @@ -72,4 +72,3 @@ the code for a complete set. var eight interface{} = 8 cast.ToInt(eight) // 8 cast.ToInt(nil) // 0 - From c9c46a17924c84b0cacac50d0523ed70e41e0a41 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sun, 5 Feb 2023 21:17:45 +0100 Subject: [PATCH 4/9] ci: add Go 1.19 and 1.20 to the build matrix Signed-off-by: Mark Sagi-Kazar --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1817575..d80fdc2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest] - go-version: ['1.16', '1.17', '1.18'] + go-version: ['1.16', '1.17', '1.18', '1.19', '1.20'] steps: - name: Set up Go From b7b9986f2c2e97d7aee51342d4efe6ff7cb1a59e Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sun, 5 Feb 2023 21:18:28 +0100 Subject: [PATCH 5/9] ci: add dependabot config Signed-off-by: Mark Sagi-Kazar --- .github/dependabot.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/dependabot.yaml diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml new file mode 100644 index 0000000..5153b81 --- /dev/null +++ b/.github/dependabot.yaml @@ -0,0 +1,16 @@ +version: 2 + +updates: + - package-ecosystem: gomod + directory: / + labels: + - dependencies + schedule: + interval: daily + + - package-ecosystem: github-actions + directory: / + labels: + - dependencies + schedule: + interval: daily From cb4b5fb39691151d77c436ad606dfc7fbb9c56a4 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sun, 5 Feb 2023 21:21:24 +0100 Subject: [PATCH 6/9] ci: add a name to the build Signed-off-by: Mark Sagi-Kazar --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d80fdc2..e3501b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,6 +8,7 @@ on: jobs: build: + name: Build runs-on: ${{ matrix.os }} strategy: # Fail fast is disabled because there are Go version specific features and tests From 111af0b83989e6796f75cf356ae4febda185b6d1 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Sun, 5 Feb 2023 21:23:57 +0100 Subject: [PATCH 7/9] chore: improve badges in readme Signed-off-by: Mark Sagi-Kazar --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c68b910..50cd825 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # cast -[![GoDoc](https://godoc.org/github.com/spf13/cast?status.svg)](https://godoc.org/github.com/spf13/cast) [![Build Status](https://github.com/spf13/cast/actions/workflows/ci.yml/badge.svg)](https://github.com/spf13/cast/actions/workflows/ci.yml) +[![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/spf13/cast)](https://pkg.go.dev/mod/github.com/spf13/cast) +![Go Version](https://img.shields.io/badge/go%20version-%3E=1.16-61CFDD.svg?style=flat-square) [![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast) Easy and safe casting from one type to another in Go From 7ef7a1881934b25b73c11590fd9d3228aea33561 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 5 Feb 2023 20:29:41 +0000 Subject: [PATCH 8/9] Bump github.com/frankban/quicktest from 1.14.3 to 1.14.4 Bumps [github.com/frankban/quicktest](https://github.com/frankban/quicktest) from 1.14.3 to 1.14.4. - [Release notes](https://github.com/frankban/quicktest/releases) - [Commits](https://github.com/frankban/quicktest/compare/v1.14.3...v1.14.4) --- updated-dependencies: - dependency-name: github.com/frankban/quicktest dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 9 ++++----- go.sum | 24 +++++++++--------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 255e99f..231f764 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,11 @@ module github.com/spf13/cast go 1.18 -require github.com/frankban/quicktest v1.14.3 +require github.com/frankban/quicktest v1.14.4 require ( - github.com/google/go-cmp v0.5.7 // indirect - github.com/kr/pretty v0.3.0 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect - github.com/rogpeppe/go-internal v1.6.1 // indirect - golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect + github.com/rogpeppe/go-internal v1.9.0 // indirect ) diff --git a/go.sum b/go.sum index 1dbb1c6..80fb7de 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,12 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= -github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= -github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= -github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= From bbed5559a59db54120dda79106f85f9ec0afb038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 15 May 2023 10:09:44 +0200 Subject: [PATCH 9/9] Pull the 2006-01-02 layout to the top when checking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current top 2 formats are, from some checks I've done, 2 dominant formats used in Hugo projects. Checking these first has a big effect: ```bash name old time/op new time/op delta CommonTimeLayouts-10 1.45µs ± 0% 0.33µs ± 1% -77.45% (p=0.029 n=4+4) name old alloc/op new alloc/op delta CommonTimeLayouts-10 1.55kB ± 0% 0.18kB ± 0% -88.14% (p=0.029 n=4+4) name old allocs/op new allocs/op delta CommonTimeLayouts-10 38.0 ± 0% 6.0 ± 0% -84.21% (p=0.029 n=4+4) ``` See https://github.com/gohugoio/hugo/issues/10942 --- cast_test.go | 11 +++++++++++ caste.go | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cast_test.go b/cast_test.go index 016f8c2..6bea164 100644 --- a/cast_test.go +++ b/cast_test.go @@ -897,6 +897,17 @@ func BenchmarkTrimZeroDecimal(b *testing.B) { } } +func BenchmarkCommonTimeLayouts(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, commonLayout := range []string{"2019-04-29", "2017-05-30T00:00:00Z"} { + _, err := StringToDateInDefaultLocation(commonLayout, time.UTC) + if err != nil { + b.Fatal(err) + } + } + } +} + func TestIndirectPointers(t *testing.T) { c := qt.New(t) diff --git a/caste.go b/caste.go index bccf27a..d49bbf8 100644 --- a/caste.go +++ b/caste.go @@ -1406,6 +1406,8 @@ func (f timeFormat) hasTimezone() bool { var ( timeFormats = []timeFormat{ + // Keep common formats at the top. + {"2006-01-02", timeFormatNoTimezone}, {time.RFC3339, timeFormatNumericTimezone}, {"2006-01-02T15:04:05", timeFormatNoTimezone}, // iso8601 without timezone {time.RFC1123Z, timeFormatNumericTimezone}, @@ -1421,7 +1423,6 @@ var ( {time.UnixDate, timeFormatNamedTimezone}, {time.RubyDate, timeFormatNumericTimezone}, {"2006-01-02 15:04:05Z07:00", timeFormatNumericTimezone}, - {"2006-01-02", timeFormatNoTimezone}, {"02 Jan 2006", timeFormatNoTimezone}, {"2006-01-02 15:04:05 -07:00", timeFormatNumericTimezone}, {"2006-01-02 15:04:05 -0700", timeFormatNumericTimezone},