mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
63 lines
916 B
Go
63 lines
916 B
Go
package structs
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/charlienet/go-mixed/json"
|
|
)
|
|
|
|
type optionFunc func(*option)
|
|
|
|
type option struct {
|
|
NameFunc func(string) string
|
|
IgnoreEmpty bool
|
|
DeepCopy bool
|
|
Omitempty bool
|
|
}
|
|
|
|
func IgnoreEmpty() optionFunc {
|
|
return func(o *option) {
|
|
o.IgnoreEmpty = true
|
|
}
|
|
}
|
|
|
|
func DeepCopy() optionFunc {
|
|
return func(o *option) {
|
|
o.DeepCopy = true
|
|
}
|
|
}
|
|
|
|
func Omitempty() optionFunc {
|
|
return func(o *option) {
|
|
o.Omitempty = true
|
|
}
|
|
}
|
|
|
|
func Lcfirst() optionFunc {
|
|
return func(o *option) {
|
|
o.NameFunc = json.Lcfirst
|
|
}
|
|
}
|
|
|
|
func Camel2Case() optionFunc {
|
|
return func(o *option) {
|
|
o.NameFunc = json.Camel2Case
|
|
}
|
|
}
|
|
|
|
func createOptions(opts []optionFunc) *option {
|
|
o := &option{
|
|
NameFunc: func(s string) string { return s },
|
|
}
|
|
|
|
for _, f := range opts {
|
|
f(o)
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func shouldIgnore(v reflect.Value, ignoreEmpty bool) bool {
|
|
return ignoreEmpty && v.IsZero()
|
|
}
|