From 63e3d4c43bac523c1a3b9c752c6247dcf3d63606 Mon Sep 17 00:00:00 2001 From: xgfone Date: Sat, 9 Apr 2022 14:07:09 +0800 Subject: [PATCH] support the type int16 and uint16 option --- config.go | 6 ++ global.go | 6 ++ opt.go | 16 +++++ opt_group.go | 6 ++ opt_proxy.go | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 2 + 6 files changed, 198 insertions(+) diff --git a/config.go b/config.go index ec9d65f..d74655f 100644 --- a/config.go +++ b/config.go @@ -505,6 +505,9 @@ func (c *Config) GetBool(name string) bool { return c.Must(name).(bool) } // GetInt returns the value of the option named name as int. func (c *Config) GetInt(name string) int { return c.Must(name).(int) } +// GetInt16 returns the value of the option named name as int16. +func (c *Config) GetInt16(name string) int16 { return c.Must(name).(int16) } + // GetInt32 returns the value of the option named name as int32. func (c *Config) GetInt32(name string) int32 { return c.Must(name).(int32) } @@ -514,6 +517,9 @@ func (c *Config) GetInt64(name string) int64 { return c.Must(name).(int64) } // GetUint returns the value of the option named name as uint. func (c *Config) GetUint(name string) uint { return c.Must(name).(uint) } +// GetUint16 returns the value of the option named name as uint16. +func (c *Config) GetUint16(name string) uint16 { return c.Must(name).(uint16) } + // GetUint32 returns the value of the option named name as uint32. func (c *Config) GetUint32(name string) uint32 { return c.Must(name).(uint32) } diff --git a/global.go b/global.go index b7b1576..6f9d1b3 100644 --- a/global.go +++ b/global.go @@ -64,6 +64,9 @@ func GetBool(name string) bool { return Conf.GetBool(name) } // GetInt is equal to Conf.GetInt(name). func GetInt(name string) int { return Conf.GetInt(name) } +// GetInt16 is equal to Conf.GetInt16(name). +func GetInt16(name string) int16 { return Conf.GetInt16(name) } + // GetInt32 is equal to Conf.GetInt32(name). func GetInt32(name string) int32 { return Conf.GetInt32(name) } @@ -73,6 +76,9 @@ func GetInt64(name string) int64 { return Conf.GetInt64(name) } // GetUint is equal to Conf.GetUint(name). func GetUint(name string) uint { return Conf.GetUint(name) } +// GetUint16 is equal to Conf.GetUint16(name). +func GetUint16(name string) uint16 { return Conf.GetUint16(name) } + // GetUint32 is equal to Conf.GetUint32(name). func GetUint32(name string) uint32 { return Conf.GetUint32(name) } diff --git a/opt.go b/opt.go index 6766a73..362568f 100644 --- a/opt.go +++ b/opt.go @@ -195,6 +195,14 @@ func IntOpt(name string, help string) Opt { }) } +// Int16Opt is the same NewOpt, but uses ToInt16 to parse the value as int16. +func Int16Opt(name string, help string) Opt { + return NewOpt(name, help, int16(0), + func(v interface{}) (interface{}, error) { + return ToInt16(v) + }) +} + // Int32Opt is the same NewOpt, but uses ToInt32 to parse the value as int32. func Int32Opt(name string, help string) Opt { return NewOpt(name, help, int32(0), @@ -219,6 +227,14 @@ func UintOpt(name string, help string) Opt { }) } +// Uint16Opt is the same NewOpt, but uses ToUint16 to parse the value as uint16. +func Uint16Opt(name string, help string) Opt { + return NewOpt(name, help, uint16(0), + func(v interface{}) (interface{}, error) { + return ToUint16(v) + }) +} + // Uint32Opt is the same NewOpt, but uses ToUint32 to parse the value as uint32. func Uint32Opt(name string, help string) Opt { return NewOpt(name, help, uint32(0), diff --git a/opt_group.go b/opt_group.go index 606749c..077aebd 100644 --- a/opt_group.go +++ b/opt_group.go @@ -117,6 +117,9 @@ func (g *OptGroup) GetBool(name string) bool { return g.Must(name).(bool) } // GetInt returns the value of the option named name as int. func (g *OptGroup) GetInt(name string) int { return g.Must(name).(int) } +// GetInt16 returns the value of the option named name as int16. +func (g *OptGroup) GetInt16(name string) int16 { return g.Must(name).(int16) } + // GetInt32 returns the value of the option named name as int32. func (g *OptGroup) GetInt32(name string) int32 { return g.Must(name).(int32) } @@ -126,6 +129,9 @@ func (g *OptGroup) GetInt64(name string) int64 { return g.Must(name).(int64) } // GetUint returns the value of the option named name as uint. func (g *OptGroup) GetUint(name string) uint { return g.Must(name).(uint) } +// GetUint16 returns the value of the option named name as uint16. +func (g *OptGroup) GetUint16(name string) uint16 { return g.Must(name).(uint16) } + // GetUint32 returns the value of the option named name as uint32. func (g *OptGroup) GetUint32(name string) uint32 { return g.Must(name).(uint32) } diff --git a/opt_proxy.go b/opt_proxy.go index ac7e57f..6f0dca7 100644 --- a/opt_proxy.go +++ b/opt_proxy.go @@ -100,6 +100,9 @@ func (o OptProxy) ToBool() *OptProxyBool { return &OptProxyBool{o} } // ToInt converts option from OptProxy to OptProxyInt. func (o OptProxy) ToInt() *OptProxyInt { return &OptProxyInt{o} } +// ToInt16 converts option from OptProxy to OptProxyInt16. +func (o OptProxy) ToInt16() *OptProxyInt16 { return &OptProxyInt16{o} } + // ToInt32 converts option from OptProxy to OptProxyInt32. func (o OptProxy) ToInt32() *OptProxyInt32 { return &OptProxyInt32{o} } @@ -109,6 +112,9 @@ func (o OptProxy) ToInt64() *OptProxyInt64 { return &OptProxyInt64{o} } // ToUint converts option from OptProxy to OptProxyUint. func (o OptProxy) ToUint() *OptProxyUint { return &OptProxyUint{o} } +// ToUint16 converts option from OptProxy to OptProxyUint16. +func (o OptProxy) ToUint16() *OptProxyUint16 { return &OptProxyUint16{o} } + // ToUint32 converts option from OptProxy to OptProxyUint32. func (o OptProxy) ToUint32() *OptProxyUint32 { return &OptProxyUint32{o} } @@ -160,6 +166,11 @@ func (g *OptGroup) NewInt(name string, _default int, help string) *OptProxyInt { return g.config.NewInt(g.prefix+name, _default, help) } +// NewInt16 creates and registers a int16 option, then returns its proxy. +func (g *OptGroup) NewInt16(name string, _default int16, help string) *OptProxyInt16 { + return g.config.NewInt16(g.prefix+name, _default, help) +} + // NewInt32 creates and registers a int32 option, then returns its proxy. func (g *OptGroup) NewInt32(name string, _default int32, help string) *OptProxyInt32 { return g.config.NewInt32(g.prefix+name, _default, help) @@ -175,6 +186,11 @@ func (g *OptGroup) NewUint(name string, _default uint, help string) *OptProxyUin return g.config.NewUint(g.prefix+name, _default, help) } +// NewUint16 creates and registers a uint16 option, then returns its proxy. +func (g *OptGroup) NewUint16(name string, _default uint16, help string) *OptProxyUint16 { + return g.config.NewUint16(g.prefix+name, _default, help) +} + // NewUint32 creates and registers a uint32 option, then returns its proxy. func (g *OptGroup) NewUint32(name string, _default uint32, help string) *OptProxyUint32 { return g.config.NewUint32(g.prefix+name, _default, help) @@ -378,6 +394,79 @@ func (o *OptProxyInt) Parser(parser Parser) *OptProxyInt { //////////////////////////////////////////////////////////////////////////// +// OptProxyInt16 is a proxy for the int16 option registered into Config, +// which can be used to modify the attributions of the option and +// update its value directly. +type OptProxyInt16 struct{ OptProxy } + +// NewInt16 is equal to Conf.NewInt16(name, _default, help). +func NewInt16(name string, _default int16, help string) *OptProxyInt16 { + return Conf.NewInt16(name, _default, help) +} + +// NewInt16 creates and registers a int16 option, then returns its proxy. +func (c *Config) NewInt16(name string, _default int16, help string) *OptProxyInt16 { + return &OptProxyInt16{c.NewOptProxy(Int16Opt(name, help).D(_default))} +} + +// Name returns the name of the option. +func (o *OptProxyInt16) Name() string { return o.OptProxy.Name() } + +// Opt returns the registered and proxied option. +func (o *OptProxyInt16) Opt() Opt { return o.option.opt } + +// Get returns the value of the option. +func (o *OptProxyInt16) Get() int16 { return o.OptProxy.Get().(int16) } + +// Set sets the value of the option to value. +func (o *OptProxyInt16) Set(value interface{}) (err error) { + return o.OptProxy.Set(value) +} + +// OnUpdate resets the update callback of the option and returns itself. +func (o *OptProxyInt16) OnUpdate(f func(old, new interface{})) *OptProxyInt16 { + o.OptProxy.OnUpdate(f) + return o +} + +// IsCli resets the cli flag of the option and returns itself. +func (o *OptProxyInt16) IsCli(cli bool) *OptProxyInt16 { + o.OptProxy.IsCli(cli) + return o +} + +// Aliases appends the aliases of the option and returns itself. +func (o *OptProxyInt16) Aliases(aliases ...string) *OptProxyInt16 { + o.OptProxy.Aliases(aliases...) + return o +} + +// Short resets the short name of the option and returns itself. +func (o *OptProxyInt16) Short(short string) *OptProxyInt16 { + o.OptProxy.Short(short) + return o +} + +// Validators appends the validators of the option and returns itself. +func (o *OptProxyInt16) Validators(validators ...Validator) *OptProxyInt16 { + o.OptProxy.Validators(validators...) + return o +} + +// Default resets the default value of the option and returns itself. +func (o *OptProxyInt16) Default(_default interface{}) *OptProxyInt16 { + o.OptProxy.Default(_default) + return o +} + +// Parser resets the parser of the option and returns itself. +func (o *OptProxyInt16) Parser(parser Parser) *OptProxyInt16 { + o.OptProxy.Parser(parser) + return o +} + +//////////////////////////////////////////////////////////////////////////// + // OptProxyInt32 is a proxy for the int32 option registered into Config, // which can be used to modify the attributions of the option and // update its value directly. @@ -597,6 +686,79 @@ func (o *OptProxyUint) Parser(parser Parser) *OptProxyUint { //////////////////////////////////////////////////////////////////////////// +// OptProxyUint16 is a proxy for the uint16 option registered into Config, +// which can be used to modify the attributions of the option and +// update its value directly. +type OptProxyUint16 struct{ OptProxy } + +// NewUint16 is equal to Conf.NewUint16(name, _default, help). +func NewUint16(name string, _default uint16, help string) *OptProxyUint16 { + return Conf.NewUint16(name, _default, help) +} + +// NewUint16 creates and registers a uint16 option, then returns its proxy. +func (c *Config) NewUint16(name string, _default uint16, help string) *OptProxyUint16 { + return &OptProxyUint16{c.NewOptProxy(Uint16Opt(name, help).D(_default))} +} + +// Name returns the name of the option. +func (o *OptProxyUint16) Name() string { return o.OptProxy.Name() } + +// Opt returns the registered and proxied option. +func (o *OptProxyUint16) Opt() Opt { return o.option.opt } + +// Get returns the value of the option. +func (o *OptProxyUint16) Get() uint16 { return o.OptProxy.Get().(uint16) } + +// Set sets the value of the option to value. +func (o *OptProxyUint16) Set(value interface{}) (err error) { + return o.OptProxy.Set(value) +} + +// OnUpdate resets the update callback of the option and returns itself. +func (o *OptProxyUint16) OnUpdate(f func(old, new interface{})) *OptProxyUint16 { + o.OptProxy.OnUpdate(f) + return o +} + +// IsCli resets the cli flag of the option and returns itself. +func (o *OptProxyUint16) IsCli(cli bool) *OptProxyUint16 { + o.OptProxy.IsCli(cli) + return o +} + +// Aliases appends the aliases of the option and returns itself. +func (o *OptProxyUint16) Aliases(aliases ...string) *OptProxyUint16 { + o.OptProxy.Aliases(aliases...) + return o +} + +// Short resets the short name of the option and returns itself. +func (o *OptProxyUint16) Short(short string) *OptProxyUint16 { + o.OptProxy.Short(short) + return o +} + +// Validators appends the validators of the option and returns itself. +func (o *OptProxyUint16) Validators(validators ...Validator) *OptProxyUint16 { + o.OptProxy.Validators(validators...) + return o +} + +// Default resets the default value of the option and returns itself. +func (o *OptProxyUint16) Default(_default interface{}) *OptProxyUint16 { + o.OptProxy.Default(_default) + return o +} + +// Parser resets the parser of the option and returns itself. +func (o *OptProxyUint16) Parser(parser Parser) *OptProxyUint16 { + o.OptProxy.Parser(parser) + return o +} + +//////////////////////////////////////////////////////////////////////////// + // OptProxyUint32 is a proxy for the uint32 option registered into Config, // which can be used to modify the attributions of the option and // update its value directly. diff --git a/utils.go b/utils.go index 2693de6..713ec47 100644 --- a/utils.go +++ b/utils.go @@ -29,9 +29,11 @@ import ( var ( ToBool = cast.ToBool // func(interface{}) (bool, error) ToInt = cast.ToInt // func(interface{}) (int, error) + ToInt16 = cast.ToInt16 // func(interface{}) (int16, error) ToInt32 = cast.ToInt32 // func(interface{}) (int32, error) ToInt64 = cast.ToInt64 // func(interface{}) (int64, error) ToUint = cast.ToUint // func(interface{}) (uint, error) + ToUint16 = cast.ToUint16 // func(interface{}) (uint16, error) ToUint32 = cast.ToUint32 // func(interface{}) (uint32, error) ToUint64 = cast.ToUint64 // func(interface{}) (uint64, error) ToFloat64 = cast.ToFloat64 // func(interface{}) (float64, error)