internal/blueprint: introduce custom fs customization parser
The automatically generated parser can only parse integers into the size field. Introduce a new one, which will be able to handle strings as well.
This commit is contained in:
parent
f2f078acd6
commit
f84beee04d
1 changed files with 47 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package blueprint
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
|
@ -76,6 +77,52 @@ type FilesystemCustomization struct {
|
|||
MinSize uint64 `json:"minsize,omitempty" toml:"size,omitempty"`
|
||||
}
|
||||
|
||||
func (fsc *FilesystemCustomization) UnmarshalTOML(data interface{}) error {
|
||||
d, _ := data.(map[string]interface{})
|
||||
|
||||
switch d["mountpoint"].(type) {
|
||||
case string:
|
||||
fsc.Mountpoint = d["mountpoint"].(string)
|
||||
default:
|
||||
return fmt.Errorf("TOML unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"])
|
||||
}
|
||||
|
||||
switch d["size"].(type) {
|
||||
case int64:
|
||||
fsc.MinSize = uint64(d["size"].(int64))
|
||||
default:
|
||||
return fmt.Errorf("TOML unmarshal: size must be integer, got %v of type %T", d["size"], d["size"])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error {
|
||||
var v interface{}
|
||||
if err := json.Unmarshal(data, &v); err != nil {
|
||||
return err
|
||||
}
|
||||
d, _ := v.(map[string]interface{})
|
||||
|
||||
switch d["mountpoint"].(type) {
|
||||
case string:
|
||||
fsc.Mountpoint = d["mountpoint"].(string)
|
||||
default:
|
||||
return fmt.Errorf("JSON unmarshal: mountpoint must be string, got %v of type %T", d["mountpoint"], d["mountpoint"])
|
||||
}
|
||||
|
||||
// The JSON specification only mentions float64 and Go defaults to it: https://go.dev/blog/json
|
||||
switch d["minsize"].(type) {
|
||||
case float64:
|
||||
// Note that it uses different key than the TOML version
|
||||
fsc.MinSize = uint64(d["minsize"].(float64))
|
||||
default:
|
||||
return fmt.Errorf("JSON unmarshal: minsize must be float64 number, got %v of type %T", d["minsize"], d["minsize"])
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type CustomizationError struct {
|
||||
Message string
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue