1 Star 0 Fork 0

mosache / go-zero

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
fieldoptions.go 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
kevwan 提交于 2020-07-26 17:09 . initial import
package mapping
import "fmt"
const notSymbol = '!'
type (
// use context and OptionalDep option to determine the value of Optional
// nothing to do with context.Context
fieldOptionsWithContext struct {
FromString bool
Optional bool
Options []string
Default string
Range *numberRange
}
fieldOptions struct {
fieldOptionsWithContext
OptionalDep string
}
numberRange struct {
left float64
leftInclude bool
right float64
rightInclude bool
}
)
func (o *fieldOptionsWithContext) fromString() bool {
return o != nil && o.FromString
}
func (o *fieldOptionsWithContext) getDefault() (string, bool) {
if o == nil {
return "", false
} else {
return o.Default, len(o.Default) > 0
}
}
func (o *fieldOptionsWithContext) optional() bool {
return o != nil && o.Optional
}
func (o *fieldOptionsWithContext) options() []string {
if o == nil {
return nil
}
return o.Options
}
func (o *fieldOptions) optionalDep() string {
if o == nil {
return ""
} else {
return o.OptionalDep
}
}
func (o *fieldOptions) toOptionsWithContext(key string, m Valuer, fullName string) (
*fieldOptionsWithContext, error) {
var optional bool
if o.optional() {
dep := o.optionalDep()
if len(dep) == 0 {
optional = true
} else if dep[0] == notSymbol {
dep = dep[1:]
if len(dep) == 0 {
return nil, fmt.Errorf("wrong optional value for %q in %q", key, fullName)
}
_, baseOn := m.Value(dep)
_, selfOn := m.Value(key)
if baseOn == selfOn {
return nil, fmt.Errorf("set value for either %q or %q in %q", dep, key, fullName)
} else {
optional = baseOn
}
} else {
_, baseOn := m.Value(dep)
_, selfOn := m.Value(key)
if baseOn != selfOn {
return nil, fmt.Errorf("values for %q and %q should be both provided or both not in %q",
dep, key, fullName)
} else {
optional = !baseOn
}
}
}
if o.fieldOptionsWithContext.Optional == optional {
return &o.fieldOptionsWithContext, nil
} else {
return &fieldOptionsWithContext{
FromString: o.FromString,
Optional: optional,
Options: o.Options,
Default: o.Default,
}, nil
}
}
1
https://gitee.com/mosache/go-zero.git
git@gitee.com:mosache/go-zero.git
mosache
go-zero
go-zero
dfb45c801a6c

搜索帮助