diff --git a/README.md b/README.md index 13222e6..5d9d48e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/README_ZH.md b/README_ZH.md index f888d00..4364382 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -9,7 +9,13 @@ > 一个简单、安全、高效的转换任意数据类型的 Go 语言工具包,支持自定义类型、提取结构体字段和值 + +## 帮助文档 +https://cvt.shockerli.net + + ## 安装 +> Go >= 1.13 ```go go get -u github.com/shockerli/cvt @@ -66,11 +72,6 @@ cvt.Float("hello", 12.34) // 12.34 > 上千个单元测试用例,覆盖率近100%,所有示例可通过单元测试了解:`*_test.go` -## 帮助文档 - -https://cvt.shockerli.net - - ## 开源协议 本项目基于 [MIT](LICENSE) 协议开放源代码。 diff --git a/cvte.go b/cvte.go index e48a8e4..54e2a8b 100644 --- a/cvte.go +++ b/cvte.go @@ -6,7 +6,6 @@ import ( "reflect" "sort" "strings" - "time" ) var errConvFail = errors.New("convert failed") @@ -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 diff --git a/cvte_test.go b/cvte_test.go index 1025bf5..6900542 100644 --- a/cvte_test.go +++ b/cvte_test.go @@ -33,7 +33,6 @@ type ( AliasTypeString string AliasTypeBytes []byte AliasTypeInterface interface{} - AliasTypeTime time.Time ) var ( @@ -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 @@ -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 { diff --git a/doc/content/en/type/others.md b/doc/content/en/type/others.md index 4ac92ac..f651721 100644 --- a/doc/content/en/type/others.md +++ b/doc/content/en/type/others.md @@ -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` diff --git a/doc/content/zh-cn/type/others.md b/doc/content/zh-cn/type/others.md index 348c08b..308256a 100644 --- a/doc/content/zh-cn/type/others.md +++ b/doc/content/zh-cn/type/others.md @@ -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` diff --git a/time_test.go b/time_test.go index e3c7d92..665688b 100644 --- a/time_test.go +++ b/time_test.go @@ -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},