Repository: knative/pkg
Pull request #2365 (2021-11-29 11:45:30)

Add ConfigMap field parsing for int16 and uint16 types
- Support ConfigMap field parsing for int16 and uint16 types

knative-prow-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2365#" title="Author self-approved">pierDipi</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **vagababov** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @vagababov` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

<details open>
Needs approval from an approver in each of these files:

- **[configmap/OWNERS](https://github.com/knative/pkg/blob/main/configmap/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["vagababov"]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) Report
> Merging [#2365](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (7295dfb) into [main](https://codecov.io/gh/knative/pkg/commit/b35fcddda935c8d7de1eb58c505f6a022e719bc9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) (b35fcdd) will **increase** coverage by `0.01%`.
> The diff coverage is `68.42%`.

[![Impacted file tree graph](https://codecov.io/gh/knative/pkg/pull/2365/graphs/tree.svg?width=650&height=150&src=pr&token=rlqpDnfxbO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)

```diff
@@            Coverage Diff             @@
##             main    #2365      +/-   ##
==========================================
+ Coverage   63.66%   63.68%   +0.01%     
==========================================
  Files         228      228              
  Lines        9854     9866      +12     
==========================================
+ Hits         6274     6283       +9     
- Misses       3305     3306       +1     
- Partials      275      277       +2     
```


| [Impacted Files](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative) | Coverage Δ | |
|---|---|---|
| [metrics/config.go](https://codecov.io/gh/knative/pkg/pull/2365/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-bWV0cmljcy9jb25maWcuZ28=) | `78.35% <60.00%> (+0.57%)` | :arrow_up: |
| [configmap/parse.go](https://codecov.io/gh/knative/pkg/pull/2365/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative#diff-Y29uZmlnbWFwL3BhcnNlLmdv) | `95.95% <71.42%> (-4.05%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Last update [608fc87...7295dfb](https://codecov.io/gh/knative/pkg/pull/2365?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative).

knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative/pkg/pull/2365#pullrequestreview-817846033" title="Approved">markusthoemmes</a>*, *<a href="https://github.com/knative/pkg/pull/2365#" title="Author self-approved">pierDipi</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative%2Fpkg).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[configmap/OWNERS](https://github.com/knative/pkg/blob/main/configmap/OWNERS)~~ [markusthoemmes]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/configmap/parse.go b/configmap/parse.go
index faeacab14..e93f9af2a 100644
--- a/configmap/parse.go
+++ b/configmap/parse.go
@@ -53,6 +53,20 @@ func AsBool(key string, target *bool) ParseFunc {
 	}
 }
 
+// AsInt16 parses the value at key as an int16 into the target, if it exists.
+func AsInt16(key string, target *int16) ParseFunc {
+	return func(data map[string]string) error {
+		if raw, ok := data[key]; ok {
+			val, err := strconv.ParseInt(raw, 10, 16)
+			if err != nil {
+				return fmt.Errorf("failed to parse %q: %w", key, err)
+			}
+			*target = int16(val)
+		}
+		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 {
@@ -95,6 +109,20 @@ func AsInt(key string, target *int) ParseFunc {
 	}
 }
 
+// AsUint16 parses the value at key as an uint16 into the target, if it exists.
+func AsUint16(key string, target *uint16) ParseFunc {
+	return func(data map[string]string) error {
+		if raw, ok := data[key]; ok {
+			val, err := strconv.ParseUint(raw, 10, 16)
+			if err != nil {
+				return fmt.Errorf("failed to parse %q: %w", key, err)
+			}
+			*target = uint16(val)
+		}
+		return nil
+	}
+}
+
 // AsUint32 parses the value at key as an uint32 into the target, if it exists.
 func AsUint32(key string, target *uint32) ParseFunc {
 	return func(data map[string]string) error {


Test patch:
diff --git a/configmap/parse_test.go b/configmap/parse_test.go
index 34bc2c3ef..4ea896e6c 100644
--- a/configmap/parse_test.go
+++ b/configmap/parse_test.go
@@ -29,8 +29,10 @@ import (
 type testConfig struct {
 	str    string
 	toggle bool
+	i16    int16
 	i32    int32
 	i64    int64
+	u16    uint16
 	u32    uint32
 	i      int
 	f64    float64
@@ -55,8 +57,10 @@ func TestParse(t *testing.T) {
 		data: map[string]string{
 			"test-string":   "foo.bar",
 			"test-bool":     "true",
+			"test-int16":    "6",
 			"test-int32":    "1",
 			"test-int64":    "2",
+			"test-uint16":   "5",
 			"test-uint32":   "3",
 			"test-int":      "4",
 			"test-float64":  "1.0",
@@ -70,8 +74,10 @@ func TestParse(t *testing.T) {
 		want: testConfig{
 			str:    "foo.bar",
 			toggle: true,
+			i16:    6,
 			i32:    1,
 			i64:    2,
+			u16:    5,
 			u32:    3,
 			f64:    1.0,
 			i:      4,
@@ -176,8 +182,10 @@ func TestParse(t *testing.T) {
 			if err := Parse(test.data,
 				AsString("test-string", &test.conf.str),
 				AsBool("test-bool", &test.conf.toggle),
+				AsInt16("test-int16", &test.conf.i16),
 				AsInt32("test-int32", &test.conf.i32),
 				AsInt64("test-int64", &test.conf.i64),
+				AsUint16("test-uint16", &test.conf.u16),
 				AsUint32("test-uint32", &test.conf.u32),
 				AsInt("test-int", &test.conf.i),
 				AsFloat64("test-float64", &test.conf.f64),