-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8e6d6a
commit 674867f
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package proxmox | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Vlan uint16 // 0-4095, 0 means no vlan | ||
|
||
const Vlan_Error_Invalid string = "vlan tag must be in the range 0-4095" | ||
|
||
func (config Vlan) String() string { | ||
return strconv.FormatInt(int64(config), 10) | ||
} | ||
|
||
func (config Vlan) Validate() error { | ||
if config > 4095 { | ||
return errors.New(Vlan_Error_Invalid) | ||
} | ||
return nil | ||
} | ||
|
||
type Vlans []Vlan | ||
|
||
func (config *Vlans) mapToApiUnsafe() string { | ||
// Use a map to track seen elements and remove duplicates. | ||
seen := make(map[Vlan]bool) | ||
result := make([]int, 0, len(*config)) | ||
// Iterate over the input slice and add unique elements to the result slice. | ||
for _, value := range *config { | ||
if _, ok := seen[value]; !ok { | ||
seen[value] = true | ||
result = append(result, int(value)) | ||
} | ||
} | ||
builder := strings.Builder{} | ||
for _, vlan := range result { | ||
builder.WriteString(";" + strconv.Itoa(vlan)) | ||
} | ||
return builder.String() | ||
} | ||
|
||
// TODO test | ||
func (config Vlans) Validate() error { | ||
for _, vlan := range config { | ||
if err := vlan.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package proxmox | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Vlan_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input Vlan | ||
output string | ||
}{ | ||
{name: `0`, | ||
input: 0, | ||
output: "0"}, | ||
{name: `4095`, | ||
input: 4095, | ||
output: "4095"}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, test.input.String()) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Vlan_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input Vlan | ||
output error | ||
}{ | ||
{name: `Valid Maximum`, | ||
input: 4095}, | ||
{name: `Valid Minimum`, | ||
input: 0}, | ||
{name: `Invalid`, | ||
input: 4096, | ||
output: errors.New(Vlan_Error_Invalid), | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, test.input.Validate()) | ||
}) | ||
} | ||
} | ||
|
||
func Test_Vlans_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input Vlans | ||
output error | ||
}{ | ||
{name: `Valid`, | ||
input: Vlans{0, 4095}}, | ||
{name: `Valid Empty`, | ||
input: Vlans{}}, | ||
{name: `Valid Duplicate`, | ||
input: Vlans{23, 78, 23, 90, 78}, | ||
}, | ||
{name: `Invalid`, | ||
input: Vlans{0, 4095, 4096}, | ||
output: errors.New(Vlan_Error_Invalid), | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Equal(t, test.output, test.input.Validate()) | ||
}) | ||
} | ||
} |