Skip to content

Commit

Permalink
fix: prefix added to empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Nov 21, 2024
1 parent 7f796da commit 0694dab
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
20 changes: 16 additions & 4 deletions proxmox/config_qemu_pcie.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@ type PciDeviceID string
const PciDeviceID_Error_Invalid string = "device id" + qemuPci_Error_Maximum

func (id PciDeviceID) String() string {
return ensurePrefix("0x", strings.ToLower(string(id)))
if id == "" {
return ""
}
return ensurePrefix(hexPrefix, strings.ToLower(string(id)))
}

func (id PciDeviceID) Validate() error {
Expand All @@ -425,7 +428,10 @@ type PciSubDeviceID string
const PciSubDeviceID_Error_Invalid string = "sub device id" + qemuPci_Error_Maximum

func (id PciSubDeviceID) String() string {
return ensurePrefix("0x", strings.ToLower(string(id)))
if id == "" {
return ""
}
return ensurePrefix(hexPrefix, strings.ToLower(string(id)))
}

func (id PciSubDeviceID) Validate() error {
Expand All @@ -445,7 +451,10 @@ type PciSubVendorID string
const PciSubVendorID_Error_Invalid string = "sub vendor id" + qemuPci_Error_Maximum

func (id PciSubVendorID) String() string {
return ensurePrefix("0x", strings.ToLower(string(id)))
if id == "" {
return ""
}
return ensurePrefix(hexPrefix, strings.ToLower(string(id)))
}

func (id PciSubVendorID) Validate() error {
Expand All @@ -465,7 +474,10 @@ type PciVendorID string
const PciVendorID_Error_Invalid string = "vendor id" + qemuPci_Error_Maximum

func (id PciVendorID) String() string {
return ensurePrefix("0x", strings.ToLower(string(id)))
if id == "" {
return ""
}
return ensurePrefix(hexPrefix, strings.ToLower(string(id)))
}

func (id PciVendorID) Validate() error {
Expand Down
84 changes: 84 additions & 0 deletions proxmox/config_qemu_pcie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ func Test_QemuPci_Validate(t *testing.T) {
}
}

func Test_PciDeviceID_String(t *testing.T) {
tests := []struct {
name string
input PciDeviceID
output string
}{
{name: `No prefix`,
input: "ffff",
output: "0xffff"},
{name: `With prefix`,
input: "0x0000",
output: "0x0000"},
{name: `Empty`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.output, test.input.String())
})
}
}

func Test_PciDeviceID_Validate(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -299,6 +320,27 @@ func Test_PciDeviceID_Validate(t *testing.T) {
}
}

func Test_PciSubDeviceID_String(t *testing.T) {
tests := []struct {
name string
input PciSubDeviceID
output string
}{
{name: `No prefix`,
input: "ffff",
output: "0xffff"},
{name: `With prefix`,
input: "0x0000",
output: "0x0000"},
{name: `Empty`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.output, test.input.String())
})
}
}

func Test_PciSubDeviceID_Validate(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -327,6 +369,27 @@ func Test_PciSubDeviceID_Validate(t *testing.T) {
}
}

func Test_PciSubVendorID_String(t *testing.T) {
tests := []struct {
name string
input PciSubVendorID
output string
}{
{name: `No prefix`,
input: "ffff",
output: "0xffff"},
{name: `With prefix`,
input: "0x0000",
output: "0x0000"},
{name: `Empty`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.output, test.input.String())
})
}
}

func Test_PciSubVendorID_Validate(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -355,6 +418,27 @@ func Test_PciSubVendorID_Validate(t *testing.T) {
}
}

func Test_PciVendorID_String(t *testing.T) {
tests := []struct {
name string
input PciVendorID
output string
}{
{name: `No prefix`,
input: "ffff",
output: "0xffff"},
{name: `With prefix`,
input: "0x0000",
output: "0x0000"},
{name: `Empty`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.output, test.input.String())
})
}
}

func Test_PciVendorID_Validate(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 2 additions & 0 deletions proxmox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

var rxUserTokenExtract = regexp.MustCompile("[a-z0-9]+@[a-z0-9]+!([a-z0-9]+)")

const hexPrefix string = "0x"

func inArray(arr []string, str string) bool {
for _, elem := range arr {
if elem == str {
Expand Down

0 comments on commit 0694dab

Please sign in to comment.