-
Notifications
You must be signed in to change notification settings - Fork 341
Add a library to parse config map values. #1329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.