Merge pull request #5528 from adamreese/v3/capabilities-marshal

fix(pkg/chartutil): marshal capabilities VersionSet into slice
pull/5529/head
Adam Reese 6 years ago committed by GitHub
commit 4b9bdf5baa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,10 +16,13 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"encoding/json"
"fmt" "fmt"
"runtime" "runtime"
"sort"
"k8s.io/apimachinery/pkg/version" "k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
) )
@ -66,7 +69,7 @@ func NewVersionSet(apiVersions ...string) VersionSet {
// Has returns true if the version string is in the set. // Has returns true if the version string is in the set.
// //
// vs.Has("extensions/v1beta1") // vs.Has("apps/v1")
func (v VersionSet) Has(apiVersion string) bool { func (v VersionSet) Has(apiVersion string) bool {
_, ok := v[apiVersion] _, ok := v[apiVersion]
return ok return ok
@ -79,3 +82,23 @@ func allKnownVersions() VersionSet {
} }
return vs return vs
} }
// MarshalJSON implements the encoding/json.Marshaler interface.
func (v VersionSet) MarshalJSON() ([]byte, error) {
out := make([]string, 0, len(v))
for i := range v {
out = append(out, i)
}
sort.Strings(out)
return json.Marshal(out)
}
// UnmarshalJSON implements the encoding/json.Unmarshaler interface.
func (v *VersionSet) UnmarshalJSON(data []byte) error {
var vs []string
if err := json.Unmarshal(data, &vs); err != nil {
return err
}
*v = NewVersionSet(vs...)
return nil
}

@ -16,17 +16,18 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"encoding/json"
"testing" "testing"
) )
func TestVersionSet(t *testing.T) { func TestVersionSet(t *testing.T) {
vs := NewVersionSet("v1", "extensions/v1beta1") vs := NewVersionSet("v1", "apps/v1")
if d := len(vs); d != 2 { if d := len(vs); d != 2 {
t.Errorf("Expected 2 versions, got %d", d) t.Errorf("Expected 2 versions, got %d", d)
} }
if !vs.Has("extensions/v1beta1") { if !vs.Has("apps/v1") {
t.Error("Expected to find extensions/v1beta1") t.Error("Expected to find apps/v1")
} }
if vs.Has("Spanish/inquisition") { if vs.Has("Spanish/inquisition") {
@ -49,3 +50,29 @@ func TestCapabilities(t *testing.T) {
t.Error("APIVersions should have v1") t.Error("APIVersions should have v1")
} }
} }
func TestCapabilitiesJSONMarshal(t *testing.T) {
vs := NewVersionSet("v1", "apps/v1")
b, err := json.Marshal(vs)
if err != nil {
t.Fatal(err)
}
expect := `["apps/v1","v1"]`
if string(b) != expect {
t.Fatalf("JSON marshaled semantic version not equal: expected %q, got %q", expect, string(b))
}
}
func TestCapabilitiesJSONUnmarshal(t *testing.T) {
in := `["apps/v1","v1"]`
var vs VersionSet
if err := json.Unmarshal([]byte(in), &vs); err != nil {
t.Fatal(err)
}
if len(vs) != 2 {
t.Fatalf("JSON unmarshaled semantic version not equal: expected 2, got %d", len(vs))
}
}

Loading…
Cancel
Save