Skip to content

Commit

Permalink
feat: vlan type
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Sep 16, 2024
1 parent c8e6d6a commit 674867f
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
52 changes: 52 additions & 0 deletions proxmox/type_vlan(s).go
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 {

Check failure on line 26 in proxmox/type_vlan(s).go

View workflow job for this annotation

GitHub Actions / staticcheck

func (*Vlans).mapToApiUnsafe is unused (U1000)
// 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
}
75 changes: 75 additions & 0 deletions proxmox/type_vlan(s)_test.go
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())
})
}
}

0 comments on commit 674867f

Please sign in to comment.