1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: add docment for new or fixed function

This commit is contained in:
dudaodong
2025-10-31 13:37:41 +08:00
parent f407e51b24
commit cbdc3971dd
11 changed files with 434 additions and 48 deletions

View File

@@ -1125,6 +1125,10 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<big>FindValuesBy</big>** : returns a slice of values from the map that satisfy the given predicate function. - **<big>FindValuesBy</big>** : returns a slice of values from the map that satisfy the given predicate function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#FindValuesBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#FindValuesBy)]
[[play](https://go.dev/play/p/bvNwNBZDm6v)] [[play](https://go.dev/play/p/bvNwNBZDm6v)]
- **<big>ToMarkdownTable</big>** : Convert a map slice data to a Markdown table string. It supports custom header display names and column display order.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToMarkdownTable)]
[[play](https://go.dev/play/p/todo)]
<h3 id="mathutil"> 13. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="mathutil"> 13. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -1819,30 +1823,50 @@ import "github.com/duke-git/lancet/v2/structs"
- **<big>New</big>** : creates a `Struct` instance. - **<big>New</big>** : creates a `Struct` instance.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#New)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#New)]
[[play](https://go.dev/play/p/O29l8kk-Z17)]
- **<big>ToMap</big>** : converts a valid struct to a map. - **<big>ToMap</big>** : converts a valid struct to a map.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#ToMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#ToMap)]
[[play](https://go.dev/play/p/qQbLySBgerZ)]
- **<big>Fields</big>** : get all fields of a given struct, that the fields are abstract struct field. - **<big>Fields</big>** : get all fields of a given struct, that the fields are abstract struct field.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Fields)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Fields)]
[[play](https://go.dev/play/p/w3Kk_CyVY7D)]
- **<big>Field</big>** : get an abstract field of a struct by given field name
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Field)]
[[play](https://go.dev/play/p/KocZMSYarza)]
- **<big>IsStruct</big>** : check if the struct is valid. - **<big>IsStruct</big>** : check if the struct is valid.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsStruct)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsStruct)]
[[play](https://go.dev/play/p/bU2FSdkbK1C)]
- **<big>Tag</big>** : get a `Tag` of the `Field`, `Tag` is a abstract struct field tag. - **<big>Tag</big>** : get a `Tag` of the `Field`, `Tag` is a abstract struct field tag.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Tag)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Tag)]
[[play](https://go.dev/play/p/DVrx5HvvUJr)]
- **<big>Name</big>** : get the field name. - **<big>Name</big>** : get the field name.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Name)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Name)]
[[play](https://go.dev/play/p/zfIGlqsatee)]
- **<big>Value</big>** : get the `Field` underlying value. - **<big>Value</big>** : get the `Field` underlying value.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Value)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Value)]
[[play](https://go.dev/play/p/qufYEU2o4Oi)]
- **<big>Kind</big>** : get the field's kind. - **<big>Kind</big>** : get the field's kind.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Kind)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Kind)]
[[play](https://go.dev/play/p/wg4NlcUNG5o)]
- **<big>IsEmbedded</big>** : check if the field is an embedded field. - **<big>IsEmbedded</big>** : check if the field is an embedded field.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsEmbedded)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsEmbedded)]
[[play](https://go.dev/play/p/wV2PrbYm3Ec)]
- **<big>IsExported</big>** : check if the field is exported. - **<big>IsExported</big>** : check if the field is exported.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsExported)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsExported)]
[[play](https://go.dev/play/p/csK4AXYaNbJ)]
- **<big>IsZero</big>** : check if the field is zero value. - **<big>IsZero</big>** : check if the field is zero value.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsZero)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsZero)]
[[play](https://go.dev/play/p/RzqpGISf87r)]
- **<big>IsSlice</big>** : check if the field is a slice. - **<big>IsSlice</big>** : check if the field is a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsSlice)]
[[play](https://go.dev/play/p/MKz4CgBIUrU)]
- **<big>IsTargetType</big>** : check if the field is target type. - **<big>IsTargetType</big>** : check if the field is target type.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsTargetType)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsTargetType)]
[[play](https://go.dev/play/p/Ig75P-agN39)]
- **<big>TypeName</big>** : Return struct type name.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#TypeName)]
[[play](https://go.dev/play/p/todo)]
<h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
@@ -2301,6 +2325,12 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsChinaUnionPay</big>** : check if a give string is a valid china union pay number or not. - **<big>IsChinaUnionPay</big>** : check if a give string is a valid china union pay number or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChinaUnionPay)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChinaUnionPay)]
[[play](https://go.dev/play/p/yafpdxLiymu)] [[play](https://go.dev/play/p/yafpdxLiymu)]
- **<big>IsPassport</big>** : Passport validation(using regex).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsPassport)]
[[play](https://go.dev/play/p/todo)]
- **<big>IsChineseHMPassport</big>** : Mainland travel permit for Hong Kong, Macao validation (using regex).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChineseHMPassport)]
[[play](https://go.dev/play/p/todo)]
<h3 id="xerror"> 25. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="xerror"> 25. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>

View File

@@ -1135,6 +1135,9 @@ import "github.com/duke-git/lancet/v2/maputil"
- **<big>FindValuesBy</big>** : 返回一个切片,包含满足给定谓词判断函数的 map 中的值。 - **<big>FindValuesBy</big>** : 返回一个切片,包含满足给定谓词判断函数的 map 中的值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#FindValuesBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#FindValuesBy)]
[[play](https://go.dev/play/p/bvNwNBZDm6v)] [[play](https://go.dev/play/p/bvNwNBZDm6v)]
- **<big>ToMarkdownTable</big>** : 将一个 map 切片数据转换为 Markdown 表格字符串。支持自定义表头显示名称和列的显示顺序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToMarkdownTable)]
[[play](https://go.dev/play/p/todo)]
<h3 id="mathutil"> 14. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="mathutil"> 14. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -1825,32 +1828,50 @@ import "github.com/duke-git/lancet/v2/structs"
- **<big>New</big>** : `Struct`结构体的构造函数。 - **<big>New</big>** : `Struct`结构体的构造函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#New)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#New)]
[[play](https://go.dev/play/p/O29l8kk-Z17)]
- **<big>ToMap</big>** : 将一个合法的 struct 对象转换为 map[string]any。 - **<big>ToMap</big>** : 将一个合法的 struct 对象转换为 map[string]any。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#ToMap)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#ToMap)]
[[play](https://go.dev/play/p/qQbLySBgerZ)]
- **<big>Fields</big>** : 获取一个 struct 对象的属性列表。 - **<big>Fields</big>** : 获取一个 struct 对象的属性列表。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)]
[[play](https://go.dev/play/p/w3Kk_CyVY7D)]
- **<big>Field</big>** : 根据属性名获取一个 struct 对象的属性。 - **<big>Field</big>** : 根据属性名获取一个 struct 对象的属性。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)]
[[play](https://go.dev/play/p/KocZMSYarza)]
- **<big>IsStruct</big>** : 判断是否为一个合法的 struct 对象。 - **<big>IsStruct</big>** : 判断是否为一个合法的 struct 对象。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsStruct)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsStruct)]
[[play](https://go.dev/play/p/bU2FSdkbK1C)]
- **<big>Tag</big>** : 获取`Field``Tag`,默认的 tag key 是 json。 - **<big>Tag</big>** : 获取`Field``Tag`,默认的 tag key 是 json。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Tag)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Tag)]
[[play](https://go.dev/play/p/DVrx5HvvUJr)]
- **<big>Name</big>** : 获取属性名。 - **<big>Name</big>** : 获取属性名。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Name)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Name)]
[[play](https://go.dev/play/p/zfIGlqsatee)]
- **<big>Value</big>** : 获取`Field`属性的值。 - **<big>Value</big>** : 获取`Field`属性的值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Value)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Value)]
[[play](https://go.dev/play/p/qufYEU2o4Oi)]
- **<big>Kind</big>** : 获取属性 Kind。 - **<big>Kind</big>** : 获取属性 Kind。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Kind)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Kind)]
[[play](https://go.dev/play/p/wg4NlcUNG5o)]
- **<big>IsEmbedded</big>** : 判断属性是否为嵌入。 - **<big>IsEmbedded</big>** : 判断属性是否为嵌入。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsEmbedded)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsEmbedded)]
[[play](https://go.dev/play/p/wV2PrbYm3Ec)]
- **<big>IsExported</big>** : 判断属性是否导出。 - **<big>IsExported</big>** : 判断属性是否导出。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsExported)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsExported)]
[[play](https://go.dev/play/p/csK4AXYaNbJ)]
- **<big>IsZero</big>** : 判断属性是否为零值。 - **<big>IsZero</big>** : 判断属性是否为零值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsZero)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsZero)]
[[play](https://go.dev/play/p/RzqpGISf87r)]
- **<big>IsSlice</big>** : 判断属性是否是切片。 - **<big>IsSlice</big>** : 判断属性是否是切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsSlice)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsSlice)]
[[play](https://go.dev/play/p/MKz4CgBIUrU)]
- **<big>IsTargetType</big>** : 判断属性是否是目标类型。 - **<big>IsTargetType</big>** : 判断属性是否是目标类型。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsTargetType)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsTargetType)]
[[play](https://go.dev/play/p/Ig75P-agN39)]
- **<big>TypeName</big>** : 获取结构体类型名。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#TypeName)]
[[play](https://go.dev/play/p/todo)]
<h3 id="strutil"> 22. strutil 包含字符串处理的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="strutil"> 22. strutil 包含字符串处理的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
@@ -2310,7 +2331,12 @@ import "github.com/duke-git/lancet/v2/validator"
- **<big>IsChinaUnionPay</big>** : 检查字符串是否是有效的中国银联卡号。 - **<big>IsChinaUnionPay</big>** : 检查字符串是否是有效的中国银联卡号。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChinaUnionPay)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChinaUnionPay)]
[[play](https://go.dev/play/p/yafpdxLiymu)] [[play](https://go.dev/play/p/yafpdxLiymu)]
- **<big>IsPassport</big>** : 判断护照(正则判断)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsPassport)]
[[play](https://go.dev/play/p/todo)]
- **<big>IsChineseHMPassport</big>** : 判断港澳台通行证(正则判断)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChineseHMPassport)]
[[play](https://go.dev/play/p/todo)]
<h3 id="xerror"> 26. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="xerror"> 26. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go

View File

@@ -2357,7 +2357,7 @@ func main() {
编辑 编辑
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
``` ```
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
```go ```go

View File

@@ -31,6 +31,7 @@ import (
- [IsStruct](#IsStruct) - [IsStruct](#IsStruct)
- [Tag](#Tag) - [Tag](#Tag)
- [Name](#Name) - [Name](#Name)
- [TypeName](#TypeName)
- [Value](#Value) - [Value](#Value)
- [Kind](#Kind) - [Kind](#Kind)
- [IsEmbedded](#IsEmbedded) - [IsEmbedded](#IsEmbedded)
@@ -53,7 +54,7 @@ import (
func New(value any, tagName ...string) *Struct func New(value any, tagName ...string) *Struct
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/O29l8kk-Z17)</span></b>
```go ```go
package main package main
@@ -68,7 +69,11 @@ func main() {
} }
p1 := &People{Name: "11"} p1 := &People{Name: "11"}
s := structs.New(p1) s := structs.New(p1)
// to do something
fmt.Println(s.ToMap())
// Output:
// map[name:11] <nil>
} }
``` ```
@@ -88,7 +93,7 @@ func (s *Struct) ToMap() (map[string]any, error)
func ToMap(v any) (map[string]any, error) func ToMap(v any) (map[string]any, error)
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/qQbLySBgerZ)</span></b>
```go ```go
package main package main
@@ -129,7 +134,7 @@ func main() {
func (s *Struct) Fields() []*Field func (s *Struct) Fields() []*Field
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/w3Kk_CyVY7D)</span></b>
```go ```go
package main package main
@@ -161,10 +166,10 @@ func main() {
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func (s *Struct) Field(name string) *Field func (s *Struct) Field(name string) (*Field, bool)
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/KocZMSYarza)</span></b>
```go ```go
package main package main
@@ -180,12 +185,14 @@ func main() {
} }
p1 := &People{Name: "11"} p1 := &People{Name: "11"}
s := structs.New(p1) s := structs.New(p1)
f := s.Field("Name") f, found := s.Field("Name")
fmt.Println(f.Value()) fmt.Println(f.Value())
fmt.Println(found)
// Output: // Output:
// 11 // 11
// true
} }
``` ```
@@ -199,7 +206,7 @@ func main() {
func (s *Struct) IsStruct() bool func (s *Struct) IsStruct() bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/bU2FSdkbK1C)</span></b>
```go ```go
package main package main
@@ -233,7 +240,7 @@ func main() {
func (f *Field) Tag() *Tag func (f *Field) Tag() *Tag
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/DVrx5HvvUJr)</span></b>
```go ```go
package main package main
@@ -270,7 +277,7 @@ func main() {
func (f *Field) Value() any func (f *Field) Value() any
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/qufYEU2o4Oi)</span></b>
```go ```go
package main package main
@@ -306,7 +313,7 @@ func main() {
func (f *Field) IsEmbedded() bool func (f *Field) IsEmbedded() bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/wV2PrbYm3Ec)</span></b>
```go ```go
package main package main
@@ -351,7 +358,7 @@ func main() {
func (f *Field) IsExported() bool func (f *Field) IsExported() bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/csK4AXYaNbJ)</span></b>
```go ```go
package main package main
@@ -390,7 +397,7 @@ func main() {
func (f *Field) IsZero() bool func (f *Field) IsZero() bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/RzqpGISf87r)</span></b>
```go ```go
package main package main
@@ -429,7 +436,7 @@ func main() {
func (f *Field) Name() string func (f *Field) Name() string
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/zfIGlqsatee)</span></b>
```go ```go
package main package main
@@ -468,7 +475,7 @@ func main() {
func (f *Field) Kind() reflect.Kind func (f *Field) Kind() reflect.Kind
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/wg4NlcUNG5o)</span></b>
```go ```go
package main package main
@@ -497,6 +504,42 @@ func main() {
} }
``` ```
### <span id="TypeName">TypeName</span>
<p>获取结构体类型名。</p>
<b>函数签名:</b>
```go
func (s *Struct) TypeName() string
```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p := &Parent{Age: 11}
s := structs.New(p1)
fmt.Println(s.TypeName())
// Output:
// Parent
}
```
### <span id="IsSlice">IsSlice</span> ### <span id="IsSlice">IsSlice</span>
<p>判断属性是否是切片</p> <p>判断属性是否是切片</p>
@@ -507,7 +550,7 @@ func main() {
func (f *Field) IsSlice() bool func (f *Field) IsSlice() bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/MKz4CgBIUrU)</span></b>
```go ```go
package main package main
@@ -544,7 +587,7 @@ func main() {
func (f *Field) IsTargetType(targetType reflect.Kind) bool func (f *Field) IsTargetType(targetType reflect.Kind) bool
``` ```
<b>示例:</b> <b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Ig75P-agN39)</span></b>
```go ```go
package main package main

View File

@@ -65,6 +65,8 @@ import (
- [IsAmericanExpress](#IsAmericanExpress) - [IsAmericanExpress](#IsAmericanExpress)
- [IsUnionPay](#IsUnionPay) - [IsUnionPay](#IsUnionPay)
- [IsChinaUnionPay](#IsChinaUnionPay) - [IsChinaUnionPay](#IsChinaUnionPay)
- [IsPassport](#IsPassport)
- [IsChineseHMPassport](#IsChineseHMPassport)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -844,20 +846,20 @@ import (
func main() { func main() {
result1 := validator.IsAlphaNumeric("ABC") result1 := validator.IsAlphaNumeric("ABC")
result2 := validator.IsAlphaNumeric("123") result2 := validator.IsAlphaNumeric("123")
result3 := validator.IsAlphaNumeric("abc123") result3 := validator.IsAlphaNumeric("abc123")
result4 := validator.IsAlphaNumeric("abc123@#$") result4 := validator.IsAlphaNumeric("abc123@#$")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
// Output: // Output:
// true // true
// true // true
// true // true
// false // false
} }
``` ```
@@ -1567,3 +1569,80 @@ func main() {
// false // false
} }
``` ```
### <span id="IsPassport">IsPassport</span>
<p>判断护照(正则判断)。</p>
<b>函数签名:</b>
```go
func IsPassport(passport, country string) bool
```
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsPassport("P123456789", "CN")
result2 := validator.IsPassport("123456789", "US")
result3 := validator.IsPassport("AB1234567", "RU")
result4 := validator.IsPassport("123456789", "CN")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// true
// false
}
```
### <span id="IsChineseHMPassport">IsChineseHMPassport</span>
<p>判断港澳台通行证(正则判断)。</p>
<b>函数签名:</b>
```go
func IsChineseHMPassport(hmPassport string) bool
```
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsChineseHMPassport("C12345678")
result2 := validator.IsChineseHMPassport("C00000000")
result3 := validator.IsChineseHMPassport("M12345678")
result4 := validator.IsChineseHMPassport("c12345678")
result5 := validator.IsChineseHMPassport("C1234567")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
```

View File

@@ -79,6 +79,8 @@ import (
- [SortByKey](#SortByKey) - [SortByKey](#SortByKey)
- [GetOrDefault](#GetOrDefault) - [GetOrDefault](#GetOrDefault)
- [FindValuesBy](#FindValuesBy) - [FindValuesBy](#FindValuesBy)
- [ToMarkdownTable](#ToMarkdownTable)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -2363,3 +2365,69 @@ func main() {
// [b d] // [b d]
} }
``` ```
### <span id="ToMarkdownTable">ToMarkdownTable</span>
<p>Convert a map slice data to a Markdown table string. It supports custom header display names and column display order.</p>
<b>Signature:</b>
```go
编辑
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
// basic usage: automatically extract column names from data as table headers
data := []map[string]interface{}{
{"name": "Alice", "age": 25, "salary": 50000},
{"name": "Bob", "age": 30, "salary": 60000},
}
result := maputil.ToMarkdownTable(data, nil, nil)
fmt.Println(result)
// output:
// |name|age|salary|
// |---|---|---|
// |Alice|25|50000|
// |Bob|30|60000|
// custom header name
headerMap := map[string]string{
"name": "n",
"age": "a",
"salary": "s",
}
result = maputil.ToMarkdownTable(data, headerMap, nil)
fmt.Println(result)
// ouput:
// |m|a|s|
// |---|---|---|
// |Alice|25|50000|
// |Bob|30|60000|
// custom column display order
columnOrder := []string{"salary", "name"}
result = maputil.ToMarkdownTable(data, nil, columnOrder)
fmt.Println(result)
// 输出:
// |salary|name|
// |---|---|
// |50000|Alice|
// |60000|Bob|
}
```

View File

@@ -31,6 +31,7 @@ import (
- [IsStruct](#IsStruct) - [IsStruct](#IsStruct)
- [Tag](#Tag) - [Tag](#Tag)
- [Name](#Name) - [Name](#Name)
- [TypeName](#TypeName)
- [Value](#Value) - [Value](#Value)
- [Kind](#Kind) - [Kind](#Kind)
- [IsEmbedded](#IsEmbedded) - [IsEmbedded](#IsEmbedded)
@@ -53,12 +54,13 @@ import (
func New(value any, tagName ...string) *Struct func New(value any, tagName ...string) *Struct
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/O29l8kk-Z17)</span></b>
```go ```go
package main package main
import ( import (
"fmt"
"github.com/duke-git/lancet/v2/structs" "github.com/duke-git/lancet/v2/structs"
) )
@@ -68,7 +70,11 @@ func main() {
} }
p1 := &People{Name: "11"} p1 := &People{Name: "11"}
s := structs.New(p1) s := structs.New(p1)
// to do something
fmt.Println(s.ToMap())
// Output:
// map[name:11] <nil>
} }
``` ```
@@ -88,7 +94,7 @@ func (s *Struct) ToMap() (map[string]any, error)
func ToMap(v any) (map[string]any, error) func ToMap(v any) (map[string]any, error)
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/qQbLySBgerZ)</span></b>
```go ```go
package main package main
@@ -130,7 +136,7 @@ func main() {
func (s *Struct) Fields() []*Field func (s *Struct) Fields() []*Field
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/w3Kk_CyVY7D)</span></b>
```go ```go
package main package main
@@ -162,10 +168,10 @@ func main() {
<b>Signature:</b> <b>Signature:</b>
```go ```go
func (s *Struct) Field(name string) *Field func (s *Struct) Field(name string) (*Field, bool)
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/KocZMSYarza)</span></b>
```go ```go
package main package main
@@ -181,12 +187,14 @@ func main() {
} }
p1 := &People{Name: "11"} p1 := &People{Name: "11"}
s := structs.New(p1) s := structs.New(p1)
f := s.Field("Name") f, found := s.Field("Name")
fmt.Println(f.Value()) fmt.Println(f.Value())
fmt.Println(found)
// Output: // Output:
// 11 // 11
// true
} }
``` ```
@@ -200,7 +208,7 @@ func main() {
func (s *Struct) IsStruct() bool func (s *Struct) IsStruct() bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/bU2FSdkbK1C)</span></b>
```go ```go
package main package main
@@ -234,7 +242,7 @@ func main() {
func (f *Field) Tag() *Tag func (f *Field) Tag() *Tag
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/DVrx5HvvUJr)</span></b>
```go ```go
package main package main
@@ -271,7 +279,7 @@ func main() {
func (f *Field) Value() any func (f *Field) Value() any
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/qufYEU2o4Oi)</span></b>
```go ```go
package main package main
@@ -307,7 +315,7 @@ func main() {
func (f *Field) IsEmbedded() bool func (f *Field) IsEmbedded() bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/wV2PrbYm3Ec)</span></b>
```go ```go
package main package main
@@ -352,7 +360,7 @@ func main() {
func (f *Field) IsExported() bool func (f *Field) IsExported() bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/csK4AXYaNbJ)</span></b>
```go ```go
package main package main
@@ -391,7 +399,7 @@ func main() {
func (f *Field) IsZero() bool func (f *Field) IsZero() bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/RzqpGISf87r)</span></b>
```go ```go
package main package main
@@ -430,7 +438,7 @@ func main() {
func (f *Field) Name() string func (f *Field) Name() string
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zfIGlqsatee)</span></b>
```go ```go
package main package main
@@ -469,7 +477,7 @@ func main() {
func (f *Field) Kind() reflect.Kind func (f *Field) Kind() reflect.Kind
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/wg4NlcUNG5o)</span></b>
```go ```go
package main package main
@@ -498,6 +506,42 @@ func main() {
} }
``` ```
### <span id="TypeName">TypeName</span>
<p>Return struct type name.</p>
<b>Signature:</b>
```go
func (s *Struct) TypeName() string
```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p := &Parent{Age: 11}
s := structs.New(p1)
fmt.Println(s.TypeName())
// Output:
// Parent
}
```
### <span id="IsSlice">IsSlice</span> ### <span id="IsSlice">IsSlice</span>
<p>Check if the field is a slice</p> <p>Check if the field is a slice</p>
@@ -508,7 +552,7 @@ func main() {
func (f *Field) IsSlice() bool func (f *Field) IsSlice() bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/MKz4CgBIUrU)</span></b>
```go ```go
package main package main
@@ -545,7 +589,7 @@ func main() {
func (f *Field) IsTargetType(targetType reflect.Kind) bool func (f *Field) IsTargetType(targetType reflect.Kind) bool
``` ```
<b>Example:</b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Ig75P-agN39)</span></b>
```go ```go
package main package main

View File

@@ -65,6 +65,8 @@ import (
- [IsAmericanExpress](#IsAmericanExpress) - [IsAmericanExpress](#IsAmericanExpress)
- [IsUnionPay](#IsUnionPay) - [IsUnionPay](#IsUnionPay)
- [IsChinaUnionPay](#IsChinaUnionPay) - [IsChinaUnionPay](#IsChinaUnionPay)
- [IsPassport](#IsPassport)
- [IsChineseHMPassport](#IsChineseHMPassport)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1569,3 +1571,80 @@ func main() {
// false // false
} }
``` ```
### <span id="IsPassport">IsPassport</span>
<p>Passport validation(using regex).</p>
<b>Signature:</b>
```go
func IsPassport(passport, country string) bool
```
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsPassport("P123456789", "CN")
result2 := validator.IsPassport("123456789", "US")
result3 := validator.IsPassport("AB1234567", "RU")
result4 := validator.IsPassport("123456789", "CN")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// true
// false
}
```
### <span id="IsChineseHMPassport">IsChineseHMPassport</span>
<p>Mainland travel permit for Hong Kong, Macao validation (using regex). </p>
<b>Signature:</b>
```go
func IsChineseHMPassport(hmPassport string) bool
```
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/todo)</span></b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsChineseHMPassport("C12345678")
result2 := validator.IsChineseHMPassport("C00000000")
result3 := validator.IsChineseHMPassport("M12345678")
result4 := validator.IsChineseHMPassport("c12345678")
result5 := validator.IsChineseHMPassport("C1234567")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
```

View File

@@ -683,6 +683,7 @@ func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V)
} }
// ToMarkdownTable converts a slice of maps to a Markdown table. // ToMarkdownTable converts a slice of maps to a Markdown table.
// Play: ttps://go.dev/play/p/todo
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string { func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string {
if len(data) == 0 { if len(data) == 0 {
return "| |\n|---|\n" return "| |\n|---|\n"

View File

@@ -26,26 +26,31 @@ func newField(v reflect.Value, f reflect.StructField, tagName string) *Field {
} }
// Tag returns the value that the key in the tag string. // Tag returns the value that the key in the tag string.
// Play: https://go.dev/play/p/DVrx5HvvUJr
func (f *Field) Tag() *Tag { func (f *Field) Tag() *Tag {
return f.tag return f.tag
} }
// Value returns the underlying value of the field. // Value returns the underlying value of the field.
// Play: https://go.dev/play/p/qufYEU2o4Oi
func (f *Field) Value() any { func (f *Field) Value() any {
return f.rvalue.Interface() return f.rvalue.Interface()
} }
// IsEmbedded returns true if the given field is an embedded field. // IsEmbedded returns true if the given field is an embedded field.
// Play: https://go.dev/play/p/wV2PrbYm3Ec
func (f *Field) IsEmbedded() bool { func (f *Field) IsEmbedded() bool {
return len(f.field.Index) > 1 return len(f.field.Index) > 1
} }
// IsExported returns true if the given field is exported. // IsExported returns true if the given field is exported.
// Play: https://go.dev/play/p/csK4AXYaNbJ
func (f *Field) IsExported() bool { func (f *Field) IsExported() bool {
return f.field.IsExported() return f.field.IsExported()
} }
// IsZero returns true if the given field is zero value. // IsZero returns true if the given field is zero value.
// Play: https://go.dev/play/p/RzqpGISf87r
func (f *Field) IsZero() bool { func (f *Field) IsZero() bool {
z := reflect.Zero(f.rvalue.Type()).Interface() z := reflect.Zero(f.rvalue.Type()).Interface()
v := f.Value() v := f.Value()
@@ -63,22 +68,26 @@ func (f *Field) IsNil() bool {
} }
// Name returns the name of the given field // Name returns the name of the given field
// Play: https://go.dev/play/p/zfIGlqsatee
func (f *Field) Name() string { func (f *Field) Name() string {
return f.field.Name return f.field.Name
} }
// Kind returns the field's kind // Kind returns the field's kind
// Play: https://go.dev/play/p/wg4NlcUNG5o
func (f *Field) Kind() reflect.Kind { func (f *Field) Kind() reflect.Kind {
return f.rvalue.Kind() return f.rvalue.Kind()
} }
// IsSlice check if a struct field type is slice or not // IsSlice check if a struct field type is slice or not
// Play: https://go.dev/play/p/MKz4CgBIUrU
func (f *Field) IsSlice() bool { func (f *Field) IsSlice() bool {
k := f.rvalue.Kind() k := f.rvalue.Kind()
return k == reflect.Slice return k == reflect.Slice
} }
// IsTargetType check if a struct field type is target type or not // IsTargetType check if a struct field type is target type or not
// Play: https://go.dev/play/p/Ig75P-agN39
func (f *Field) IsTargetType(targetType reflect.Kind) bool { func (f *Field) IsTargetType(targetType reflect.Kind) bool {
return f.rvalue.Kind() == targetType return f.rvalue.Kind() == targetType
} }

View File

@@ -20,6 +20,7 @@ type Struct struct {
} }
// New returns a new *Struct // New returns a new *Struct
// Play: https://go.dev/play/p/O29l8kk-Z17
func New(value any, tagName ...string) *Struct { func New(value any, tagName ...string) *Struct {
value = pointer.ExtractPointer(value) value = pointer.ExtractPointer(value)
v := reflect.ValueOf(value) v := reflect.ValueOf(value)
@@ -60,6 +61,7 @@ func New(value any, tagName ...string) *Struct {
// Name string `json:"myName"` // Name string `json:"myName"`
// //
// ToMap convert the exported fields of a struct to map. // ToMap convert the exported fields of a struct to map.
// Play: https://go.dev/play/p/qQbLySBgerZ
func (s *Struct) ToMap() (map[string]any, error) { func (s *Struct) ToMap() (map[string]any, error) {
if !s.IsStruct() { if !s.IsStruct() {
return nil, fmt.Errorf("invalid struct %v", s) return nil, fmt.Errorf("invalid struct %v", s)
@@ -87,6 +89,7 @@ func (s *Struct) ToMap() (map[string]any, error) {
} }
// Fields returns all the struct fields within a slice // Fields returns all the struct fields within a slice
// Play: https://go.dev/play/p/w3Kk_CyVY7D
func (s *Struct) Fields() []*Field { func (s *Struct) Fields() []*Field {
fieldNum := s.rvalue.NumField() fieldNum := s.rvalue.NumField()
fields := make([]*Field, 0, fieldNum) fields := make([]*Field, 0, fieldNum)
@@ -100,6 +103,7 @@ func (s *Struct) Fields() []*Field {
} }
// Field returns a Field if the given field name was found // Field returns a Field if the given field name was found
// Play: https://go.dev/play/p/KocZMSYarza
func (s *Struct) Field(name string) (*Field, bool) { func (s *Struct) Field(name string) (*Field, bool) {
f, ok := s.rtype.FieldByName(name) f, ok := s.rtype.FieldByName(name)
if !ok { if !ok {
@@ -109,6 +113,7 @@ func (s *Struct) Field(name string) (*Field, bool) {
} }
// IsStruct returns true if the given rvalue is a struct // IsStruct returns true if the given rvalue is a struct
// Play: https://go.dev/play/p/bU2FSdkbK1C
func (s *Struct) IsStruct() bool { func (s *Struct) IsStruct() bool {
k := s.rvalue.Kind() k := s.rvalue.Kind()
if k == reflect.Invalid { if k == reflect.Invalid {
@@ -119,11 +124,13 @@ func (s *Struct) IsStruct() bool {
// ToMap convert struct to map, only convert exported struct field // ToMap convert struct to map, only convert exported struct field
// map key is specified same as struct field tag `json` value. // map key is specified same as struct field tag `json` value.
// Play: https://go.dev/play/p/qQbLySBgerZ
func ToMap(v any) (map[string]any, error) { func ToMap(v any) (map[string]any, error) {
return New(v).ToMap() return New(v).ToMap()
} }
// TypeName return struct type name // TypeName return struct type name
// Play: https://go.dev/play/p/todo
func (s *Struct) TypeName() string { func (s *Struct) TypeName() string {
return s.rtype.Name() return s.rtype.Name()
} }