-
Notifications
You must be signed in to change notification settings - Fork 54
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
datasize, blueprint: add new datasizes.Size type and use in blueprints #1057
Merged
achilleas-k
merged 4 commits into
osbuild:main
from
mvo5:datasizes-2-return-of-the-decoupler
Nov 22, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
54f019d
datasizes: add new `Size` type with json/toml decoding support
mvo5 af7cd9b
blueprint: decouple FilesystemCustomization from marshaling
mvo5 718e50c
blueprint: extract unmarshalTOMLviaJSON() helper
mvo5 bf18923
blueprint: fix DiskCustomization.MinSize using datasize.Size
mvo5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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,24 @@ | ||
package blueprint | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// XXX: move to interal/common ? | ||
func unmarshalTOMLviaJSON(u json.Unmarshaler, data any) error { | ||
// This is the most efficient way to reuse code when unmarshaling | ||
// structs in toml, it leaks json errors which is a bit sad but | ||
// because the toml unmarshaler gives us not "[]byte" but an | ||
// already pre-processed "any" we cannot just unmarshal into our | ||
// "fooMarshaling" struct and reuse the result so we resort to | ||
// this workaround (but toml will go away long term anyway). | ||
dataJSON, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error unmarshaling TOML data %v: %w", data, err) | ||
} | ||
if err := u.UnmarshalJSON(dataJSON); err != nil { | ||
return fmt.Errorf("error decoding TOML %v: %w", data, 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,72 @@ | ||
package datasizes | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// Size is a wrapper around uint64 with support for reading from string | ||
// yaml/toml, so {"size": 123}, {"size": "1234"}, {"size": "1 GiB"} are | ||
// all supported | ||
type Size uint64 | ||
|
||
// Uint64 returns the size as uint64. This is a convenience functions, | ||
// it is strictly equivalent to uint64(Size(1)) | ||
func (si Size) Uint64() uint64 { | ||
return uint64(si) | ||
} | ||
|
||
func (si *Size) UnmarshalTOML(data interface{}) error { | ||
i, err := decodeSize(data) | ||
if err != nil { | ||
return fmt.Errorf("error decoding TOML size: %w", err) | ||
} | ||
*si = Size(i) | ||
return nil | ||
} | ||
|
||
func (si *Size) UnmarshalJSON(data []byte) error { | ||
dec := json.NewDecoder(bytes.NewBuffer(data)) | ||
dec.UseNumber() | ||
|
||
var v interface{} | ||
if err := dec.Decode(&v); err != nil { | ||
return err | ||
} | ||
i, err := decodeSize(v) | ||
if err != nil { | ||
// if only we could do better here and include e.g. the field | ||
// name where this happend but encoding/json does not | ||
// support this, c.f. https://github.com/golang/go/issues/58655 | ||
return fmt.Errorf("error decoding size: %w", err) | ||
} | ||
*si = Size(i) | ||
return nil | ||
} | ||
|
||
// decodeSize takes an integer or string representing a data size (with a data | ||
// suffix) and returns the uint64 representation. | ||
func decodeSize(size any) (uint64, error) { | ||
switch s := size.(type) { | ||
case string: | ||
return Parse(s) | ||
case json.Number: | ||
i, err := s.Int64() | ||
if i < 0 { | ||
return 0, fmt.Errorf("cannot be negative") | ||
} | ||
return uint64(i), err | ||
case int64: | ||
if s < 0 { | ||
return 0, fmt.Errorf("cannot be negative") | ||
} | ||
return uint64(s), nil | ||
case uint64: | ||
return s, nil | ||
case float64, float32: | ||
return 0, fmt.Errorf("cannot be float") | ||
default: | ||
return 0, fmt.Errorf("failed to convert value \"%v\" to number", size) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably grow it's own test.