session: ModuleParam.Get() swap error return

session: ModuleParam.validate() swap error return

session: fix calling functions to adjust to swapped returns

wifi: fix calling functions to adjust to swapped returns
This commit is contained in:
Lars Lehtonen 2019-11-25 03:08:21 -08:00
commit c9cc78f9b3
No known key found for this signature in database
GPG key ID: 8137D474EBCB04F2
3 changed files with 16 additions and 18 deletions

View file

@ -172,7 +172,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
"Minimum WiFi signal strength in dBm.")
mod.AddObservableParam(minRSSI, func(v string) {
if err, v := minRSSI.Get(s); err != nil {
if v, err := minRSSI.Get(s); err != nil {
mod.Error("%v", err)
} else if mod.minRSSI = v.(int); mod.Started {
mod.Info("wifi.rssi.min set to %d", mod.minRSSI)
@ -231,7 +231,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
"Seconds of inactivity for an access points to be considered not in range anymore.")
mod.AddObservableParam(apTTL, func(v string) {
if err, v := apTTL.Get(s); err != nil {
if v, err := apTTL.Get(s); err != nil {
mod.Error("%v", err)
} else if mod.apTTL = v.(int); mod.Started {
mod.Info("wifi.ap.ttl set to %d", mod.apTTL)
@ -243,7 +243,7 @@ func NewWiFiModule(s *session.Session) *WiFiModule {
"Seconds of inactivity for a client station to be considered not in range or not connected to its access point anymore.")
mod.AddObservableParam(staTTL, func(v string) {
if err, v := staTTL.Get(s); err != nil {
if v, err := staTTL.Get(s); err != nil {
mod.Error("%v", err)
} else if mod.staTTL = v.(int); mod.Started {
mod.Info("wifi.sta.ttl set to %d", mod.staTTL)

View file

@ -171,7 +171,7 @@ func (m SessionModule) ListParam(name string) (err error, values []string) {
func (m SessionModule) StringParam(name string) (error, string) {
if p, found := m.params[name]; found {
if err, v := p.Get(m.Session); err != nil {
if v, err := p.Get(m.Session); err != nil {
return err, ""
} else {
return nil, v.(string)
@ -191,7 +191,7 @@ func (m SessionModule) IPParam(name string) (error, net.IP) {
func (m SessionModule) IntParam(name string) (error, int) {
if p, found := m.params[name]; found {
if err, v := p.Get(m.Session); err != nil {
if v, err := p.Get(m.Session); err != nil {
return err, 0
} else {
return nil, v.(int)
@ -204,7 +204,7 @@ func (m SessionModule) IntParam(name string) (error, int) {
func (m SessionModule) DecParam(name string) (error, float64) {
if p, found := m.params[name]; found {
if err, v := p.Get(m.Session); err != nil {
if v, err := p.Get(m.Session); err != nil {
return err, 0
} else {
return nil, v.(float64)
@ -216,7 +216,7 @@ func (m SessionModule) DecParam(name string) (error, float64) {
}
func (m SessionModule) BoolParam(name string) (error, bool) {
if err, v := m.params[name].Get(m.Session); err != nil {
if v, err := m.params[name].Get(m.Session); err != nil {
return err, false
} else {
return nil, v.(bool)

View file

@ -62,34 +62,32 @@ func NewDecimalParameter(name string, def_value string, desc string) *ModulePara
return NewModuleParameter(name, def_value, FLOAT, "^[\\d]+(\\.\\d+)?$", desc)
}
func (p ModuleParam) validate(value string) (error, interface{}) {
func (p ModuleParam) validate(value string) (interface{}, error) {
if p.Validator != nil {
if !p.Validator.MatchString(value) {
return fmt.Errorf("Parameter %s not valid: '%s' does not match rule '%s'.", tui.Bold(p.Name), value, p.Validator.String()), nil
return nil, fmt.Errorf("Parameter %s not valid: '%s' does not match rule '%s'.", tui.Bold(p.Name), value, p.Validator.String())
}
}
switch p.Type {
case STRING:
return nil, value
return value, nil
case BOOL:
lvalue := strings.ToLower(value)
if lvalue == "true" {
return nil, true
return true, nil
} else if lvalue == "false" {
return nil, false
return false, nil
} else {
return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
}
case INT:
i, err := strconv.Atoi(value)
return err, i
return strconv.Atoi(value)
case FLOAT:
i, err := strconv.ParseFloat(value, 64)
return err, i
return strconv.ParseFloat(value, 64)
}
return fmt.Errorf("Unhandled module parameter type %d.", p.Type), nil
return nil, fmt.Errorf("Unhandled module parameter type %d.", p.Type)
}
const ParamIfaceName = "<interface name>"
@ -122,7 +120,7 @@ func (p ModuleParam) getUnlocked(s *Session) string {
return p.parse(s, v)
}
func (p ModuleParam) Get(s *Session) (error, interface{}) {
func (p ModuleParam) Get(s *Session) (interface{}, error) {
_, v := s.Env.Get(p.Name)
v = p.parse(s, v)
return p.validate(v)