From a70c470634630768dca57cb2a9bc1b80adb1ee46 Mon Sep 17 00:00:00 2001 From: Angel Misevski Date: Tue, 13 Feb 2024 14:03:49 -0500 Subject: [PATCH 1/2] Switch to sigs.k8s.io/yaml instead of default; fixup jozufile struct Switch to using sigs.k8s.io/yaml for YAML serialization/deserialization to avoid ambiguity between JSON and YAML representations of the Jozufile. gopkg.in/yaml.v3 uses a separate struct tag for YAML fields, leaving ambiguity between the JSON and YAML serialized format of a struct, unless struct tags are repeated: type myStruct struct { myfield string `json:"myfield,omitempty",yaml:"myfield,omitempty"` } sigs.k8s.io/yaml instead reuses json struct tags, allowing for one specification to apply to both formats. In addition, this commit adds the `omitempty` tag to fields, to avoid serializing empty structs, and converts struct fields to pointers to ensure they can be nil/empty --- go.mod | 3 +- go.sum | 2 ++ pkg/artifact/jozu_file.go | 61 ++++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index c988f621..4f86db20 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.1.0-rc5 github.com/spf13/cobra v1.8.0 - gopkg.in/yaml.v3 v3.0.1 oras.land/oras-go/v2 v2.3.1 + sigs.k8s.io/yaml v1.4.0 ) require ( @@ -30,6 +30,7 @@ require ( golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) require ( diff --git a/go.sum b/go.sum index e1e4472e..37a5bd7f 100644 --- a/go.sum +++ b/go.sum @@ -81,3 +81,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= oras.land/oras-go/v2 v2.3.1 h1:lUC6q8RkeRReANEERLfH86iwGn55lbSWP20egdFHVec= oras.land/oras-go/v2 v2.3.1/go.mod h1:5AQXVEu1X/FKp1F9DMOb5ZItZBOa0y5dha0yCm4NR9c= +sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= +sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/pkg/artifact/jozu_file.go b/pkg/artifact/jozu_file.go index 6703c16b..c2151b6a 100644 --- a/pkg/artifact/jozu_file.go +++ b/pkg/artifact/jozu_file.go @@ -5,58 +5,59 @@ import ( "io" "os" - "gopkg.in/yaml.v3" + "sigs.k8s.io/yaml" ) type ( JozuFile struct { - ManifestVersion string `yaml:"manifestVersion"` - Package Package `yaml:"package"` - Code []Code `yaml:"code"` - DataSets []DataSet `yaml:"datasets"` - Models []TrainedModel `yaml:"models"` + ManifestVersion string `json:"manifestVersion"` + Package Package `json:"package,omitempty"` + Code []Code `json:"code,omitempty"` + DataSets []DataSet `json:"datasets,omitempty"` + Models []TrainedModel `json:"models,omitempty"` } Package struct { - Name string `yaml:"name"` - Version string `yaml:"version"` - Description string `yaml:"description"` - Authors []string `yaml:"authors"` + Name string `json:"name,omitempty"` + Version string `json:"version,omitempty"` + Description string `json:"description,omitempty"` + License string `json:"license,omitempty"` + Authors []string `json:"authors,omitempty"` } Code struct { - Path string `yaml:"path"` - License string `yaml:"license"` - Description string `yaml:"description"` + Path string `json:"path,omitempty"` + License string `json:"license,omitempty"` + Description string `json:"description,omitempty"` } DataSet struct { - Name string `yaml:"name"` - Path string `yaml:"path"` - Description string `yaml:"description"` - License string `yaml:"license"` - Preprocessing string `yaml:"preprocessing"` + Name string `json:"name,omitempty"` + Path string `json:"path,omitempty"` + Description string `json:"description,omitempty"` + License string `json:"license,omitempty"` + Preprocessing string `json:"preprocessing,omitempty"` } TrainedModel struct { - Name string `yaml:"name"` - Path string `yaml:"path"` - Framework string `yaml:"framework"` - Version string `yaml:"version"` - Description string `yaml:"description"` - License string `yaml:"license"` - Training Training `yaml:"training"` - Validation Validation `yaml:"validation"` + Name string `json:"name,omitempty"` + Path string `json:"path,omitempty"` + Framework string `json:"framework,omitempty"` + Version string `json:"version,omitempty"` + Description string `json:"description,omitempty"` + License string `json:"license,omitempty"` + Training *Training `json:"training,omitempty"` + Validation *Validation `json:"validation,omitempty"` } Training struct { - DataSet string `yaml:"dataset"` - Parameters map[string]interface{} `yaml:"parameters"` + DataSet string `json:"dataset,omitempty"` + Parameters map[string]interface{} `json:"parameters,omitempty"` } Validation struct { - DataSet string `yaml:"dataset"` - Metrics map[string]interface{} `yaml:"metrics"` + DataSet string `json:"dataset,omitempty"` + Metrics map[string]interface{} `json:"metrics,omitempty"` } ) From 68c70ebec741b5b547abf395de0ceff12ce9fff6 Mon Sep 17 00:00:00 2001 From: Angel Misevski Date: Tue, 13 Feb 2024 14:20:14 -0500 Subject: [PATCH 2/2] Fixup manifestVersion in sample to avoid being parsed as a number The Yaml spec defines different scalar types, and unquoted strings are parsed as numbers if the string can be parsed as a number. To ensure a number is parsed as a string, it has to be enclosed in quotes or include some character than cannot be in a number. --- examples/onnx/Jozufile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/onnx/Jozufile b/examples/onnx/Jozufile index 4de78185..96f66604 100644 --- a/examples/onnx/Jozufile +++ b/examples/onnx/Jozufile @@ -1,4 +1,4 @@ -manifestVersion: 1.0 +manifestVersion: v1.0.0 package: name: Densenet-ONNX description: >-