Replace if/else by switch

This commit is contained in:
Edznux 2018-10-28 18:01:55 +01:00
parent b119f815d9
commit 25a2f48a02
3 changed files with 22 additions and 17 deletions

View file

@ -64,9 +64,10 @@ func (p ModuleParam) Validate(value string) (error, interface{}) {
}
}
if p.Type == STRING {
switch p.Type {
case STRING:
return nil, value
} else if p.Type == BOOL {
case BOOL:
lvalue := strings.ToLower(value)
if lvalue == "true" {
return nil, true
@ -75,7 +76,7 @@ func (p ModuleParam) Validate(value string) (error, interface{}) {
} else {
return fmt.Errorf("Can't typecast '%s' to boolean.", value), nil
}
} else if p.Type == INT {
case INT:
i, err := strconv.Atoi(value)
return err, i
}
@ -90,17 +91,21 @@ const ParamRandomMAC = "<random mac>"
func (p ModuleParam) Get(s *Session) (error, interface{}) {
_, v := s.Env.Get(p.Name)
if v == ParamIfaceName {
switch v {
case ParamIfaceName:
v = s.Interface.Name()
} else if v == ParamIfaceAddress {
case ParamIfaceAddress:
v = s.Interface.IpAddress
} else if v == ParamSubnet {
case ParamSubnet:
v = s.Interface.CIDR()
} else if v == ParamRandomMAC {
case ParamRandomMAC:
hw := make([]byte, 6)
rand.Read(hw)
v = net.HardwareAddr(hw).String()
default:
return fmt.Errorf("Uknown value for v"), nil
}
return p.Validate(v)
}