Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions configmap/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package configmap

import (
"strconv"
"strings"
"time"
)

// ParseFunc is a function taking ConfigMap data and applying a parse operation to it.
type ParseFunc func(map[string]string) error

// AsString passes the value at key through into the target, if it exists.
func AsString(key string, target *string) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
*target = raw
}
return nil
}
}

// AsBool parses the value at key as a boolean into the target, if it exists.
func AsBool(key string, target *bool) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
*target = strings.EqualFold(raw, "true")
}
return nil
}
}

// AsInt32 parses the value at key as an int32 into the target, if it exists.
func AsInt32(key string, target *int32) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseInt(raw, 10, 32)
if err != nil {
return err
}
*target = int32(val)
}
return nil
}
}

// AsInt64 parses the value at key as an int64 into the target, if it exists.
func AsInt64(key string, target *int64) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return err
}
*target = val
}
return nil
}
}

// AsFloat64 parses the value at key as a float64 into the target, if it exists.
func AsFloat64(key string, target *float64) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := strconv.ParseFloat(raw, 64)
if err != nil {
return err
}
*target = val
}
return nil
}
}

// AsDuration parses the value at key as a time.Duration into the target, if it exists.
func AsDuration(key string, target *time.Duration) ParseFunc {
return func(data map[string]string) error {
if raw, ok := data[key]; ok {
val, err := time.ParseDuration(raw)
if err != nil {
return err
}
*target = val
}
return nil
}
}

// Parse parses the given map using the parser functions passed in.
func Parse(data map[string]string, parsers ...ParseFunc) error {
for _, parse := range parsers {
if err := parse(data); err != nil {
return err
}
}
return nil
}
128 changes: 128 additions & 0 deletions configmap/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package configmap

import (
"testing"
"time"
)

type testConfig struct {
str string
boo bool
i32 int32
i64 int64
f64 float64
dur time.Duration
}

func TestParse(t *testing.T) {
tests := []struct {
name string
conf testConfig
data map[string]string
want testConfig
expectErr bool
}{{
name: "all good",
data: map[string]string{
"test-string": "foo.bar",
"test-bool": "true",
"test-int32": "1",
"test-int64": "2",
"test-float64": "1.0",
"test-duration": "1m",
},
want: testConfig{
str: "foo.bar",
boo: true,
i32: 1,
i64: 2,
f64: 1.0,
dur: time.Minute,
},
}, {
name: "respect defaults",
conf: testConfig{
str: "foo.bar",
boo: true,
i32: 1,
i64: 2,
f64: 1.0,
dur: time.Minute,
},
want: testConfig{
str: "foo.bar",
boo: true,
i32: 1,
i64: 2,
f64: 1.0,
dur: time.Minute,
},
}, {
name: "bool defaults to false",
data: map[string]string{
"test-bool": "foo",
},
want: testConfig{
boo: false,
},
}, {
name: "int32 error",
data: map[string]string{
"test-int32": "foo",
},
expectErr: true,
}, {
name: "int64 error",
data: map[string]string{
"test-int64": "foo",
},
expectErr: true,
}, {
name: "float64 error",
data: map[string]string{
"test-float64": "foo",
},
expectErr: true,
}, {
name: "duration error",
data: map[string]string{
"test-duration": "foo",
},
expectErr: true,
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := Parse(test.data,
AsString("test-string", &test.conf.str),
AsBool("test-bool", &test.conf.boo),
AsInt32("test-int32", &test.conf.i32),
AsInt64("test-int64", &test.conf.i64),
AsFloat64("test-float64", &test.conf.f64),
AsDuration("test-duration", &test.conf.dur),
); (err == nil) == test.expectErr {
t.Fatal("Failed to parse data:", err)
}

if test.conf != test.want {
t.Fatalf("parsed = %v, want %v", test.conf, test.want)
}
})
}
}