Skip to content

Commit

Permalink
Support functions: Field/SliceInt/SliceInt64/SliceFloat64/SliceString
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Sep 6, 2022
1 parent 4d970a5 commit 21a699c
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 51 deletions.
15 changes: 14 additions & 1 deletion cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ var formatOutOfLimitInt = "%w, out of max limit value(%d)"
var formatOutOfLimitFloat = "%w, out of max limit value(%f)"
var formatExtend = "%v, %w"

// FieldE return the field value from map/struct, ignore the filed type
// Field return the field value from map/struct, with default value
func Field(v interface{}, field interface{}, def ...interface{}) interface{} {
if v, err := FieldE(v, field); err == nil {
return v
}

if len(def) > 0 {
return def[0]
}

return nil
}

// FieldE return the field value from map/struct, ignore the field type
func FieldE(val interface{}, field interface{}) (interface{}, error) {
if val == nil {
return nil, errUnsupportedTypeNil
Expand Down
33 changes: 33 additions & 0 deletions doc/content/en/type/others.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Others
weight: 80
---


{{< toc >}}

## Field
Reference method `FieldE`.

## FieldE
Return the field value from map/struct, `interface{}`.

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

// struct
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "A") // "Hello"
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "B") // 18
```


> 更多示例请看单元测试:`cvte_test.go`
33 changes: 13 additions & 20 deletions doc/content/en/type/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,6 @@ cvt.ColumnsE(map[int]TestStructD{1: {11}, 2: {22}}, "D1")
cvt.ColumnsE([]TestStructE{{D1: 1}, {D1: 2}}, "D1")
```

## FieldE
Return the field value from map/struct, `interface{}`.

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

// struct
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "A") // "Hello"
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "B") // 18
```

## KeysE
Return the keys of map, or fields of struct, `[]interface{}`.

Expand Down Expand Up @@ -83,7 +64,7 @@ cvt.KeysE(struct{


## Slice
Convert an interface to a `[]interface{}` type.
Reference method `SliceE`.

## SliceE
Convert an interface to a `[]interface{}` type.
Expand All @@ -104,6 +85,9 @@ cvt.SliceE(TestStruct{18,"jhon"}) // []interface{}{18, "jhon"}
```


## SliceInt
Reference method `SliceIntE`.

## SliceIntE
Convert an interface to a `[]int` type.

Expand All @@ -112,6 +96,9 @@ cvt.SliceIntE([]string{"1", "2", "3"}) // []int{1, 2, 3}
cvt.SliceIntE(map[int]string{2: "222", 1: "111"}) // []int{111, 222}
```

## SliceInt64
Reference method `SliceInt64E`.

## SliceInt64E
Convert an interface to a `[]int64` type.

Expand All @@ -121,6 +108,9 @@ cvt.SliceInt64E(map[int]string{2: "222", 1: "111"}) // []int64{111, 222}
```


## SliceFloat64
Reference method `SliceFloat64E`.

## SliceFloat64E
Convert an interface to a `[]float64` type.

Expand All @@ -129,6 +119,9 @@ cvt.SliceFloat64E([]string{"1", "2", "3"}) // []float64{1, 2, 3}
cvt.SliceFloat64E(map[int]string{2: "222", 1: "111"}) // []float64{111, 222}
```

## SliceString
Reference method `SliceStringE`.

## SliceStringE
Convert an interface to a `[]string` type.

Expand Down
6 changes: 6 additions & 0 deletions doc/content/en/usage/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ weight: -20

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


{{< hint type=warning >}}
**Document statement**\
`__()` and `__P()` are convenient method is based on `__E()`, so the method of use and the type of support please reference ` __E () ` document.
{{< /hint >}}

<!--more-->

{{< toc >}}
Expand Down
12 changes: 12 additions & 0 deletions doc/content/zh-cn/develop/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: 测试
weight: 20
---

## 命名约定

- `TestXXX_HasDefault`:测试带有默认值的方法
- `TestXXX_BaseLine`:测试没有带默认值的无错误方法
- `TestXXXE`:测试返回错误信息的方法
- `TestXXXP`:测试返回数据指针的方法

34 changes: 34 additions & 0 deletions doc/content/zh-cn/type/others.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Others
weight: 80
---


{{< toc >}}


## Field
参考 `FieldE` 方法。

## FieldE
`map``struct` 的字段值,返回 `interface{}`

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

// struct
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "A") // "Hello"
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "B") // 18
```


> 更多示例请看单元测试:`cvte_test.go`
33 changes: 13 additions & 20 deletions doc/content/zh-cn/type/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ cvt.ColumnsE(map[int]TestStructD{1: {11}, 2: {22}}, "D1")
cvt.ColumnsE([]TestStructE{{D1: 1}, {D1: 2}}, "D1")
```

## FieldE
`map``struct` 的字段值,返回 `interface{}`

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

// struct
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "A") // "Hello"
cvt.FieldE(struct{
A string
B int
}{"Hello", 18}, "B") // 18
```

## KeysE
`map` 的键名,或结构体的字段名,返回 `[]interface{}`

Expand Down Expand Up @@ -86,7 +67,7 @@ cvt.KeysE(struct{


## Slice
`SliceE` 方法的默认值版本
参考 `SliceE` 方法。

## SliceE
转换成 `[]interface{}`
Expand All @@ -107,6 +88,9 @@ cvt.SliceE(TestStruct{18,"jhon"}) // []interface{}{18, "jhon"}
```


## SliceInt
参考 `SliceIntE` 方法。

## SliceIntE
转换成 `[]int`

Expand All @@ -115,6 +99,9 @@ cvt.SliceIntE([]string{"1", "2", "3"}) // []int{1, 2, 3}
cvt.SliceIntE(map[int]string{2: "222", 1: "111"}) // []int{111, 222}
```

## SliceInt64
参考 `SliceInt64E` 方法。

## SliceInt64E
转换成 `[]int64`

Expand All @@ -124,6 +111,9 @@ cvt.SliceInt64E(map[int]string{2: "222", 1: "111"}) // []int64{111, 222}
```


## SliceFloat64
参考 `SliceFloat64E` 方法。

## SliceFloat64E
转换成 `[]float64`

Expand All @@ -132,6 +122,9 @@ cvt.SliceFloat64E([]string{"1", "2", "3"}) // []float64{1, 2, 3}
cvt.SliceFloat64E(map[int]string{2: "222", 1: "111"}) // []float64{111, 222}
```

## SliceString
参考 `SliceStringE` 方法。

## SliceStringE
转换成 `[]string`

Expand Down
7 changes: 7 additions & 0 deletions doc/content/zh-cn/usage/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ weight: -20
简单、安全、高效的转换任意数据类型的 Go 语言工具包,支持自定义类型、提取结构体字段和值。


{{< hint type=warning >}}
**文档说明**\
`__()``__P()` 均是以其对应的 `__E()` 为基础的便捷方法,故其使用方法及支持的类型请参考 `__E()` 的文档,大部分此类便利方法均不再详细阐述。
{{< /hint >}}


<!--more-->

{{< toc >}}
Expand Down Expand Up @@ -75,6 +81,7 @@ cvt.Float("hello", 12.34) // 12.34
cvt.BoolP("true") // (*bool)(0x14000126180)(true)
```


### 更多示例

> 上千个单元测试用例,覆盖率近100%,所有示例可通过单元测试了解:`*_test.go`
Expand Down
Loading

1 comment on commit 21a699c

@vercel
Copy link

@vercel vercel bot commented on 21a699c Sep 6, 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.