Skip to content

Commit

Permalink
remove derived type support for time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Sep 10, 2022
1 parent 723c37b commit c646e19
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 28 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

> Simple, safe conversion of any type, including indirect/custom types.

## Documents
https://cvt.shockerli.net


## Install
> Go >= 1.13
```go
go get -u github.com/shockerli/cvt
Expand Down Expand Up @@ -66,11 +72,6 @@ cvt.Float("hello", 12.34) // 12.34
> 1000+ unit test cases, for more examples, see `*_test.go`

## Documents

https://cvt.shockerli.net


## License

This project is under the terms of the [MIT](LICENSE) license.
Expand Down
11 changes: 6 additions & 5 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

> 一个简单、安全、高效的转换任意数据类型的 Go 语言工具包,支持自定义类型、提取结构体字段和值

## 帮助文档
https://cvt.shockerli.net


## 安装
> Go >= 1.13
```go
go get -u github.com/shockerli/cvt
Expand Down Expand Up @@ -66,11 +72,6 @@ cvt.Float("hello", 12.34) // 12.34
> 上千个单元测试用例,覆盖率近100%,所有示例可通过单元测试了解:`*_test.go`

## 帮助文档

https://cvt.shockerli.net


## 开源协议

本项目基于 [MIT](LICENSE) 协议开放源代码。
Expand Down
10 changes: 0 additions & 10 deletions cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"reflect"
"sort"
"strings"
"time"
)

var errConvFail = errors.New("convert failed")
Expand Down Expand Up @@ -185,15 +184,6 @@ func Indirect(a interface{}) (val interface{}, rv reflect.Value) {
if rv.Type().Elem().Kind() == reflect.Uint8 {
val = rv.Bytes()
}
default:
// time.Time
// promise for support go >= 1.13
// rv.CanConvert(t) is added in go1.17
// rt.ConvertibleTo(t) is added in go1.1
if ct := reflect.TypeOf(time.Time{}); rv.Type().ConvertibleTo(ct) {
cv := rv.Convert(ct)
return cv.Interface(), cv
}
}

return
Expand Down
5 changes: 1 addition & 4 deletions cvte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type (
AliasTypeString string
AliasTypeBytes []byte
AliasTypeInterface interface{}
AliasTypeTime time.Time
)

var (
Expand Down Expand Up @@ -68,7 +67,7 @@ var (
aliasTypeBytesTrue AliasTypeBytes = []byte("true")
aliasTypeBytes8d15 AliasTypeBytes = []byte("8.15")

aliasTypeTime1 = AliasTypeTime(time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC))
time1 = time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC)
)

// custom type
Expand Down Expand Up @@ -256,8 +255,6 @@ func TestIndirect(t *testing.T) {
{&pointerIntNil, nil},
{pointerRunes, []rune("中国")},
{&pointerRunes, []rune("中国")},
{aliasTypeTime1, time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC)},
{&aliasTypeTime1, time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC)},
}

for i, tt := range tests {
Expand Down
8 changes: 7 additions & 1 deletion doc/content/en/type/others.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ cvt.FieldE(struct{
}{"Hello", 18}, "B") // 18
```

Combine with other methods:

> 更多示例请看单元测试:`cvte_test.go`
```go
cvt.Int(cvt.Field(map[int]interface{}{123: "112233"}, 123)) // 112233
```


> More case see unit: `cvte_test.go`
6 changes: 6 additions & 0 deletions doc/content/zh-cn/type/others.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ cvt.FieldE(struct{
}{"Hello", 18}, "B") // 18
```

`Field` 与其他方法结合使用:

```go
cvt.Int(cvt.Field(map[int]interface{}{123: "112233"}, 123)) // 112233
```


> 更多示例请看单元测试:`cvte_test.go`
6 changes: 3 additions & 3 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ func TestTimeE(t *testing.T) {
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, loc), false},
{int64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{uint(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, loc), false},
{uint(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{uint32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{time.Date(2009, 2, 13, 23, 31, 30, 0, loc), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{time1, time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{&time1, time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{TestTimeStringer{time.Date(2010, 3, 7, 0, 0, 0, 0, loc)}, time.Date(2010, 3, 7, 0, 0, 0, 0, loc), false},
{pointerIntNil, time.Time{}, false},
{aliasTypeStringTime1, time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false},
{&aliasTypeStringTime1, time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false},
{aliasTypeIntTime1, time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{&aliasTypeIntTime1, time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{aliasTypeTime1, time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{&aliasTypeTime1, time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{json.Number("1234567890"), time.Date(2009, 2, 13, 23, 31, 30, 0, loc), false},
{json.Number(aliasTypeStringTime1), time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false},

Expand Down

1 comment on commit c646e19

@vercel
Copy link

@vercel vercel bot commented on c646e19 Sep 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.