structs

package
v0.0.0-...-d31700d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 5 Imported by: 0

README

structs

功能

  • 递归遍历结构体的字段
  • 结构体的标签的查找,支持多行文本的标签。reflect.StructTag不支持跨多行文本的标签。
  • 打印结构体,不打印零值字段

functions

  • recursively traverse fields of struct.
  • lookup struct tag, support tag with multiline text. reflect.StructTag does not support tag with multiline text.
  • print struct, but do not include zero value fields.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrSyntax = errors.New("invalid syntax")

Functions

func GetTag

func GetTag(tag, key string) string
Example
fmt.Println(GetTag(`a:"av"`, `a`))
Output:

av

func LookupTag

func LookupTag(tag, key string) (value string, ok bool)
Example (False)
fmt.Println(LookupTag(``, ``))
fmt.Println(LookupTag(` `, ``))
fmt.Println(LookupTag(`a`, ``))
fmt.Println(LookupTag(`a:""`, ``))
fmt.Println(LookupTag(`a:"av"`, `b`))
Output:

 false
 false
 false
 false
 false
Example (True)
fmt.Println(LookupTag(`a:""`, `a`))
fmt.Println(LookupTag(`a:"av"`, `a`))
fmt.Println(LookupTag(`a:"av" b:"b\" v"`, `b`))
fmt.Println(LookupTag(`a:"av"
		b:"b v" `, `b`))
fmt.Println(LookupTag(`名称:"值
\"def"`, `名称`))
Output:

 true
av true
b" v true
b v true
值
"def true

func ParseTag

func ParseTag(tag string) (result map[string]string)
Example (True)
fmt.Println(ParseTag(`a:""`))
fmt.Println(ParseTag(`a:"av"`))
fmt.Println(ParseTag(`a:"av" b:"b\" v"`))
fmt.Println(ParseTag(`a:"av"
		b:"b v" `))
fmt.Println(ParseTag(`名称:"值
\"def"`))
Output:

map[a:]
map[a:av]
map[a:av b:b" v]
map[a:av b:b v]
map[名称:值
"def]

func Println

func Println(structs ...interface{})

Println print a struct without zero value fields.

Example
type c struct {
	C int
}
type D struct {
	D int
}
var s = struct {
	A int
	B string
	C c
	D
	E c
}{
	A: 1, B: "", C: c{C: 3}, D: D{D: 4},
}
Println(s)
Output:

{A:1 C:{C:3} D:{D:4}}
Example (NilStringer)
t := time.Date(2019, 1, 2, 10, 11, 12, 0, time.UTC)

Println(struct{ Time *time.Time }{})
Println(struct{ Time *time.Time }{&t})
Output:

{}
{Time:2019-01-02 10:11:12 +0000 UTC}

func ShallowCopy

func ShallowCopy(src interface{}) interface{}

ShallowCopy shallow copy a struct

Example
type T struct{ A, B, c int }
src := T{99, 88, 97}
copy := ShallowCopy(src).(T)

fmt.Println(copy, &src == &copy)
Output:

{99 88 0} false

func ShallowCopyV

func ShallowCopyV(src reflect.Value) reflect.Value

ShallowCopyV shallow copy a struct value

func Sprint

func Sprint(strct interface{}) string

func Traverse

func Traverse(
	val reflect.Value,
	convertNilPtr bool,
	skip func(val reflect.Value, field reflect.StructField) bool,
	fn func(val reflect.Value, field reflect.StructField) bool,
) bool

Traverse traverses a reflect.Value convertNilPtr: convert anonymous nil struct pointer to non-nil or not. nil pointer to anonymous unexported struct fields are not traversed by Traverse,

Example
value := getTestValue()
Traverse(reflect.ValueOf(value), true, nil, func(v reflect.Value, f reflect.StructField) bool {
	fmt.Println(f.Name)
	switch v.Kind() {
	case reflect.Int:
		v.SetInt(8)
	case reflect.String:
		v.SetString(f.Name)
	case reflect.Bool:
		v.SetBool(true)
		return false
	}
	return true
})
fmt.Println(regexp.MustCompile("0x[0-9a-f]+").ReplaceAllLiteralString(
	fmt.Sprintf("%+v", value),
	fmt.Sprintf("%+v", reflect.ValueOf(value).Elem().FieldByName("T5")),
))
Output:

T1
T2
T3
T4
T5
Stop
&{T1:T1 T2:{T2:T2} t3:{T3:T3} T4:8 T5:&{T5:T5} t6:<nil> Stop:true AfterStop: notExported:0}

func TraverseType

func TraverseType(
	typ reflect.Type,
	skip func(field reflect.StructField) bool,
	fn func(field reflect.StructField),
	index ...int,
)

TraverseType traverses a reflect.Type but pointer to anonymous unexported struct fields are always traversed by TraverseType.

Example
TraverseType(reflect.TypeOf(getTestValue()), nil, func(f reflect.StructField) {
	fmt.Println(f.Name, f.Index)
})
Output:

T1 [0]
T2 [1 0]
T3 [2 0]
T4 [3]
T5 [4 0]
T6 [5 0]
Stop [6]
AfterStop [7]

func Unquote

func Unquote(s string) (string, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL