From 718e50c7be1f723efc6b92b7294e7f0c75815b47 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Fri, 22 Nov 2024 12:48:58 +0100 Subject: [PATCH] blueprint: extract unmarshalTOMLviaJSON() helper --- pkg/blueprint/filesystem_customizations.go | 21 ++++--------------- pkg/blueprint/toml_json_bridge.go | 24 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 pkg/blueprint/toml_json_bridge.go diff --git a/pkg/blueprint/filesystem_customizations.go b/pkg/blueprint/filesystem_customizations.go index 7f9d1c1918..90dce38613 100644 --- a/pkg/blueprint/filesystem_customizations.go +++ b/pkg/blueprint/filesystem_customizations.go @@ -19,23 +19,6 @@ type filesystemCustomizationMarshaling struct { MinSize datasizes.Size `json:"minsize,omitempty" toml:"minsize,omitempty"` } -func (fsc *FilesystemCustomization) UnmarshalTOML(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 := fsc.UnmarshalJSON(dataJSON); err != nil { - return fmt.Errorf("error decoding TOML %v: %w", data, err) - } - return nil -} - func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error { var fc filesystemCustomizationMarshaling if err := json.Unmarshal(data, &fc); err != nil { @@ -50,6 +33,10 @@ func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error { return nil } +func (fsc *FilesystemCustomization) UnmarshalTOML(data any) error { + return unmarshalTOMLviaJSON(fsc, data) +} + // CheckMountpointsPolicy checks if the mountpoints are allowed by the policy func CheckMountpointsPolicy(mountpoints []FilesystemCustomization, mountpointAllowList *pathpolicy.PathPolicies) error { var errs []error diff --git a/pkg/blueprint/toml_json_bridge.go b/pkg/blueprint/toml_json_bridge.go new file mode 100644 index 0000000000..6e75cfe9f4 --- /dev/null +++ b/pkg/blueprint/toml_json_bridge.go @@ -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 +}