Skip to content

Commit

Permalink
lxd/shared/validate: Add named results to ParseNetworkVLANRange and…
Browse files Browse the repository at this point in the history
… `ParseUint32Range`

Signed-off-by: Kadin Sayani <[email protected]>
  • Loading branch information
kadinsayani committed Oct 21, 2024
1 parent 5c1227c commit 1a9fc0e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions shared/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func IsUint32(value string) error {

// ParseUint32Range parses a uint32 range in the form "number" or "start-end".
// Returns the start number and the size of the range.
func ParseUint32Range(value string) (uint32, uint32, error) {
func ParseUint32Range(value string) (start uint32, rangeSize uint32, err error) {
rangeParts := strings.SplitN(value, "-", 2)
rangeLen := len(rangeParts)
if rangeLen != 1 && rangeLen != 2 {
Expand All @@ -101,7 +101,7 @@ func ParseUint32Range(value string) (uint32, uint32, error) {
return 0, 0, fmt.Errorf("Invalid number %q", value)
}

var rangeSize uint32 = 1
rangeSize = 1

if rangeLen == 2 {
endNum, err := strconv.ParseUint(rangeParts[1], 10, 32)
Expand All @@ -116,7 +116,9 @@ func ParseUint32Range(value string) (uint32, uint32, error) {
rangeSize += uint32(endNum) - uint32(startNum)
}

return uint32(startNum), rangeSize, nil
start = uint32(startNum)

return start, rangeSize, nil
}

// IsUint32Range validates whether the string is a uint32 range in the form "number" or "start-end".
Expand Down Expand Up @@ -674,8 +676,8 @@ func IsAbsFilePath(value string) error {

// ParseNetworkVLANRange parses a VLAN range in the form "number" or "start-end".
// Returns the start number and the number of items in the range.
func ParseNetworkVLANRange(vlan string) (int, int, error) {
err := IsNetworkVLAN(vlan)
func ParseNetworkVLANRange(vlan string) (vlanRangeStart int, rangeSize int, err error) {
err = IsNetworkVLAN(vlan)
if err == nil {
vlanRangeStart, err := strconv.Atoi(vlan)
if err != nil {
Expand All @@ -694,7 +696,7 @@ func ParseNetworkVLANRange(vlan string) (int, int, error) {
return -1, -1, fmt.Errorf("Invalid VLAN range boundary. start:%s, end:%s", vlanRange[0], vlanRange[1])
}

vlanRangeStart, err := strconv.Atoi(vlanRange[0])
vlanRangeStart, err = strconv.Atoi(vlanRange[0])
if err != nil {
return -1, -1, err
}
Expand All @@ -708,7 +710,9 @@ func ParseNetworkVLANRange(vlan string) (int, int, error) {
return -1, -1, fmt.Errorf("Invalid VLAN range boundary. start:%d is higher than end:%d", vlanRangeStart, vlanRangeEnd)
}

return vlanRangeStart, vlanRangeEnd - vlanRangeStart + 1, nil
rangeSize = vlanRangeEnd - vlanRangeStart + 1

return vlanRangeStart, rangeSize, nil
}

// IsHostname checks the string is valid DNS hostname.
Expand Down

0 comments on commit 1a9fc0e

Please sign in to comment.