Skip to content

feat(config-qemu-memory-string): parse memory capacity both from string and integer. #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions proxmox/config_qemu_memory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package proxmox

import (
"encoding/json"
"errors"
"fmt"
"strconv"

"github.com/Telmate/proxmox-api-go/internal/parse"
)
Expand Down Expand Up @@ -135,11 +138,51 @@ func (m QemuMemoryBalloonCapacity) Validate() error {
type QemuMemoryCapacity uint32 // max 4178944

const (
QemuMemoryCapacity_Error_Maximum string = "memory capacity has a maximum of 4178944"
QemuMemoryCapacity_Error_Minimum string = "memory capacity has a minimum of 1"
qemuMemoryCapacity_Max QemuMemoryCapacity = 4178944
QemuMemoryCapacity_Error_Maximum string = "memory capacity has a maximum of 4178944"
QemuMemoryCapacity_Error_Minimum string = "memory capacity has a minimum of 1"
QemuMemoryCapacity_Error_InvalidType string = "memory capacity has invalid type"
qemuMemoryCapacity_Max QemuMemoryCapacity = 4178944
qemuMemoryCapacity_Base = 10
)

func (capacity *QemuMemoryCapacity) populateString(value string) error {
resultValue, err := strconv.ParseUint(value, qemuMemoryCapacity_Base, 32)
if err != nil {
return err
}

*capacity = QemuMemoryCapacity(resultValue)
return nil
}

func (capacity *QemuMemoryCapacity) populateNumber(value float64) {
*capacity = QemuMemoryCapacity(value)
}

func (capacity *QemuMemoryCapacity) populate(rawValue interface{}) error {
switch value := rawValue.(type) {
case string:
return capacity.populateString(value)

case float64:
capacity.populateNumber(value)
return nil

default:
return fmt.Errorf("%s: %T", QemuMemoryCapacity_Error_InvalidType, rawValue)
}
}

func (capacity *QemuMemoryCapacity) UnmarshalJSON(data []byte) error {
var rawValue interface{}

if err := json.Unmarshal(data, &rawValue); err != nil {
return err
}

return capacity.populate(rawValue)
}

func (m QemuMemoryCapacity) Validate() error {
if m == 0 {
return errors.New(QemuMemoryCapacity_Error_Minimum)
Expand Down
44 changes: 44 additions & 0 deletions proxmox/config_qemu_memory_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package proxmox

import (
"encoding/json"
"errors"
"fmt"
"strconv"
"testing"

"github.com/Telmate/proxmox-api-go/internal/util"
Expand Down Expand Up @@ -219,3 +222,44 @@ func Test_QemuMemoryShares_Validate(t *testing.T) {
})
}
}

func Test_QemuMemoryCapacity_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input []byte
outputValue QemuMemoryCapacity
outputError error
}{
{name: `Integer`,
input: []byte(`32768`),
outputValue: QemuMemoryCapacity(32768),
outputError: nil,
},
{name: `String`,
input: []byte(`"32768"`),
outputValue: QemuMemoryCapacity(32768),
outputError: nil,
},
{name: `InvalidSyntax`,
input: []byte(`"abcd"`),
outputValue: QemuMemoryCapacity(0),
outputError: &strconv.NumError{Func: "ParseUint", Num: "abcd", Err: strconv.ErrSyntax},
},
{name: `InvalidType`,
input: []byte(`true`),
outputValue: QemuMemoryCapacity(0),
outputError: fmt.Errorf("%s: %T", QemuMemoryCapacity_Error_InvalidType, true),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var capacity QemuMemoryCapacity

err := json.Unmarshal(test.input, &capacity)

require.Equal(t, test.outputError, err)
require.Equal(t, test.outputValue, capacity)
})
}
}