From 98524c3d232a31f6cf6b326cc3023aee743e5acb Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Tue, 16 Aug 2022 20:24:52 +0300 Subject: [PATCH] pkg/cdi: unexport spec.Write(), NewSpec(). This should reduce confusion about how generates Specs should be written (cache.WriteSpec() vs. spec.Write()) and also reduce the possibility/temptation for users to bypass the registry/cache when interacting with Specs. Signed-off-by: Krisztian Litkey --- pkg/cdi/cache.go | 4 ++-- pkg/cdi/spec.go | 14 +++++++------- pkg/cdi/spec_test.go | 12 ++++++------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/cdi/cache.go b/pkg/cdi/cache.go index 9c77135..60e4d83 100644 --- a/pkg/cdi/cache.go +++ b/pkg/cdi/cache.go @@ -294,12 +294,12 @@ func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error { path += defaultSpecExt } - spec, err = NewSpec(raw, path, prio) + spec, err = newSpec(raw, path, prio) if err != nil { return err } - return spec.Write(true) + return spec.write(true) } // RemoveSpec removes a Spec with the given name from the highest diff --git a/pkg/cdi/spec.go b/pkg/cdi/spec.go index 69cb457..2038bb0 100644 --- a/pkg/cdi/spec.go +++ b/pkg/cdi/spec.go @@ -86,7 +86,7 @@ func ReadSpec(path string, priority int) (*Spec, error) { return nil, errors.Errorf("failed to parse CDI Spec %q, no Spec data", path) } - spec, err := NewSpec(raw, path, priority) + spec, err := newSpec(raw, path, priority) if err != nil { return nil, err } @@ -94,11 +94,11 @@ func ReadSpec(path string, priority int) (*Spec, error) { return spec, nil } -// NewSpec creates a new Spec from the given CDI Spec data. The +// newSpec creates a new Spec from the given CDI Spec data. The // Spec is marked as loaded from the given path with the given -// priority. If Spec data validation fails NewSpec returns a nil +// priority. If Spec data validation fails newSpec returns a nil // Spec and an error. -func NewSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) { +func newSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) { err := validateSpec(raw) if err != nil { return nil, err @@ -124,8 +124,8 @@ func NewSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) { } // Write the CDI Spec to the file associated with it during instantiation -// by NewSpec() or ReadSpec(). -func (s *Spec) Write(overwrite bool) error { +// by newSpec() or ReadSpec(). +func (s *Spec) write(overwrite bool) error { var ( data []byte dir string @@ -259,7 +259,7 @@ func ParseSpec(data []byte) (*cdi.Spec, error) { // SetSpecValidator sets a CDI Spec validator function. This function // is used for extra CDI Spec content validation whenever a Spec file -// loaded (using ReadSpec() or NewSpec()) or written (Spec.Write()). +// loaded (using ReadSpec() or written (using WriteSpec()). func SetSpecValidator(fn func(*cdi.Spec) error) { validatorLock.Lock() defer validatorLock.Unlock() diff --git a/pkg/cdi/spec_test.go b/pkg/cdi/spec_test.go index 6b0bcaa..c20b320 100644 --- a/pkg/cdi/spec_test.go +++ b/pkg/cdi/spec_test.go @@ -246,7 +246,7 @@ devices: } require.NoError(t, err) - spec, err = NewSpec(raw, tc.name, 0) + spec, err = newSpec(raw, tc.name, 0) if tc.invalid || tc.schemaFail { require.Error(t, err) require.Nil(t, spec) @@ -367,22 +367,22 @@ devices: err = yaml.Unmarshal([]byte(tc.data), raw) require.NoError(t, err) - spec, err = NewSpec(raw, filepath.Join(dir, tc.name), 0) + spec, err = newSpec(raw, filepath.Join(dir, tc.name), 0) if tc.invalid { - require.Error(t, err, "NewSpec with invalid data") - require.Nil(t, spec, "NewSpec with invalid data") + require.Error(t, err, "newSpec with invalid data") + require.Nil(t, spec, "newSpec with invalid data") return } require.NoError(t, err) require.NotNil(t, spec) - err = spec.Write(true) + err = spec.write(true) require.NoError(t, err) _, err = os.Stat(spec.GetPath()) require.NoError(t, err, "spec.Write destination file") - err = spec.Write(false) + err = spec.write(false) require.Error(t, err) chk, err = ReadSpec(spec.GetPath(), spec.GetPriority())