mirror of https://github.com/knative/pkg.git
Add AsQuantity to configmap parser (#1362)
This commit is contained in:
parent
2d1a04d1ff
commit
6eb9fa1546
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -114,6 +115,21 @@ func AsStringSet(key string, target *sets.String) ParseFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AsQuantity parses the value at key as a *resource.Quantity into the target, if it exists
|
||||||
|
func AsQuantity(key string, target **resource.Quantity) ParseFunc {
|
||||||
|
return func(data map[string]string) error {
|
||||||
|
if raw, ok := data[key]; ok {
|
||||||
|
val, err := resource.ParseQuantity(raw)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse %q: %w", key, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
*target = &val
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse parses the given map using the parser functions passed in.
|
// Parse parses the given map using the parser functions passed in.
|
||||||
func Parse(data map[string]string, parsers ...ParseFunc) error {
|
func Parse(data map[string]string, parsers ...ParseFunc) error {
|
||||||
for _, parse := range parsers {
|
for _, parse := range parsers {
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -32,9 +33,11 @@ type testConfig struct {
|
||||||
f64 float64
|
f64 float64
|
||||||
dur time.Duration
|
dur time.Duration
|
||||||
set sets.String
|
set sets.String
|
||||||
|
qua *resource.Quantity
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
|
fiveHundredM := resource.MustParse("500m")
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
conf testConfig
|
conf testConfig
|
||||||
|
|
@ -51,6 +54,7 @@ func TestParse(t *testing.T) {
|
||||||
"test-float64": "1.0",
|
"test-float64": "1.0",
|
||||||
"test-duration": "1m",
|
"test-duration": "1m",
|
||||||
"test-set": "a,b,c",
|
"test-set": "a,b,c",
|
||||||
|
"test-quantity": "500m",
|
||||||
},
|
},
|
||||||
want: testConfig{
|
want: testConfig{
|
||||||
str: "foo.bar",
|
str: "foo.bar",
|
||||||
|
|
@ -60,6 +64,7 @@ func TestParse(t *testing.T) {
|
||||||
f64: 1.0,
|
f64: 1.0,
|
||||||
dur: time.Minute,
|
dur: time.Minute,
|
||||||
set: sets.NewString("a", "b", "c"),
|
set: sets.NewString("a", "b", "c"),
|
||||||
|
qua: &fiveHundredM,
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
name: "respect defaults",
|
name: "respect defaults",
|
||||||
|
|
@ -70,6 +75,7 @@ func TestParse(t *testing.T) {
|
||||||
i64: 2,
|
i64: 2,
|
||||||
f64: 1.0,
|
f64: 1.0,
|
||||||
dur: time.Minute,
|
dur: time.Minute,
|
||||||
|
qua: &fiveHundredM,
|
||||||
},
|
},
|
||||||
want: testConfig{
|
want: testConfig{
|
||||||
str: "foo.bar",
|
str: "foo.bar",
|
||||||
|
|
@ -78,6 +84,7 @@ func TestParse(t *testing.T) {
|
||||||
i64: 2,
|
i64: 2,
|
||||||
f64: 1.0,
|
f64: 1.0,
|
||||||
dur: time.Minute,
|
dur: time.Minute,
|
||||||
|
qua: &fiveHundredM,
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
name: "bool defaults to false",
|
name: "bool defaults to false",
|
||||||
|
|
@ -111,6 +118,12 @@ func TestParse(t *testing.T) {
|
||||||
"test-duration": "foo",
|
"test-duration": "foo",
|
||||||
},
|
},
|
||||||
expectErr: true,
|
expectErr: true,
|
||||||
|
}, {
|
||||||
|
name: "quantity error",
|
||||||
|
data: map[string]string{
|
||||||
|
"test-quantity": "foo",
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
@ -123,6 +136,7 @@ func TestParse(t *testing.T) {
|
||||||
AsFloat64("test-float64", &test.conf.f64),
|
AsFloat64("test-float64", &test.conf.f64),
|
||||||
AsDuration("test-duration", &test.conf.dur),
|
AsDuration("test-duration", &test.conf.dur),
|
||||||
AsStringSet("test-set", &test.conf.set),
|
AsStringSet("test-set", &test.conf.set),
|
||||||
|
AsQuantity("test-quantity", &test.conf.qua),
|
||||||
); (err == nil) == test.expectErr {
|
); (err == nil) == test.expectErr {
|
||||||
t.Fatal("Failed to parse data:", err)
|
t.Fatal("Failed to parse data:", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue