Skip to content

Commit

Permalink
support the type int16 and uint16 option
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Apr 9, 2022
1 parent 4e1ab7c commit 63e3d4c
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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) }

Expand Down
6 changes: 6 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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) }

Expand Down
16 changes: 16 additions & 0 deletions opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions opt_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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) }

Expand Down
162 changes: 162 additions & 0 deletions opt_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} }

Expand All @@ -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} }

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 63e3d4c

Please sign in to comment.