Skip to content

Commit 911ea81

Browse files
upgrade to latest dependencies (#942)
bumping knative.dev/pkg 4e27b2e...05e18ff: > 05e18ff pull configmap parsing into separate package (# 3185) Signed-off-by: Knative Automation <[email protected]>
1 parent 0d09958 commit 911ea81

File tree

5 files changed

+129
-120
lines changed

5 files changed

+129
-120
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
k8s.io/code-generator v0.33.1
1212
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff
1313
knative.dev/hack v0.0.0-20250514121446-f525e187efdc
14-
knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090
14+
knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7
1515
)
1616

1717
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ k8s.io/utils v0.0.0-20241210054802-24370beab758 h1:sdbE21q2nlQtFh65saZY+rRM6x6aJ
676676
k8s.io/utils v0.0.0-20241210054802-24370beab758/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
677677
knative.dev/hack v0.0.0-20250514121446-f525e187efdc h1:8HmclJlA0zNE/G1SkgdC3/IFSSyhaSz2iIhihU6YbEo=
678678
knative.dev/hack v0.0.0-20250514121446-f525e187efdc/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY=
679-
knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090 h1:lrYxxGsWJJjphcg7ZS4gYmC52qjoYbU/+MasnoaVn0g=
680-
knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090/go.mod h1:nrao281iv0FJ5u/c0j3zmrinAlyOUZo9hJUxbrbsxWo=
679+
knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7 h1:034E/3HHcG7y533bMXa+d9vD5yjEow2thggnnsGlbTM=
680+
knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7/go.mod h1:nrao281iv0FJ5u/c0j3zmrinAlyOUZo9hJUxbrbsxWo=
681681
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
682682
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
683683
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

vendor/knative.dev/pkg/configmap/parse.go

Lines changed: 15 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -18,152 +18,51 @@ package configmap
1818

1919
import (
2020
"fmt"
21-
"strconv"
2221
"strings"
2322
"time"
2423

2524
"k8s.io/apimachinery/pkg/api/resource"
2625
"k8s.io/apimachinery/pkg/api/validation"
2726
"k8s.io/apimachinery/pkg/types"
2827
"k8s.io/apimachinery/pkg/util/sets"
28+
"knative.dev/pkg/configmap/parser"
2929
)
3030

3131
// ParseFunc is a function taking ConfigMap data and applying a parse operation to it.
32-
type ParseFunc func(map[string]string) error
32+
type ParseFunc = parser.ParseFunc
3333

3434
// AsString passes the value at key through into the target, if it exists.
35-
func AsString(key string, target *string) ParseFunc {
36-
return func(data map[string]string) error {
37-
if raw, ok := data[key]; ok {
38-
*target = raw
39-
}
40-
return nil
41-
}
42-
}
35+
var AsString = parser.As[string]
4336

4437
// AsBool parses the value at key as a boolean into the target, if it exists.
45-
func AsBool(key string, target *bool) ParseFunc {
46-
return func(data map[string]string) error {
47-
if raw, ok := data[key]; ok {
48-
val, err := strconv.ParseBool(raw)
49-
*target = val // If err != nil — this is always false.
50-
return err
51-
}
52-
return nil
53-
}
54-
}
38+
var AsBool = parser.As[bool]
5539

5640
// AsInt16 parses the value at key as an int16 into the target, if it exists.
57-
func AsInt16(key string, target *int16) ParseFunc {
58-
return func(data map[string]string) error {
59-
if raw, ok := data[key]; ok {
60-
val, err := strconv.ParseInt(raw, 10, 16)
61-
if err != nil {
62-
return fmt.Errorf("failed to parse %q: %w", key, err)
63-
}
64-
*target = int16(val)
65-
}
66-
return nil
67-
}
68-
}
41+
var AsInt16 = parser.As[int16]
6942

7043
// AsInt32 parses the value at key as an int32 into the target, if it exists.
71-
func AsInt32(key string, target *int32) ParseFunc {
72-
return func(data map[string]string) error {
73-
if raw, ok := data[key]; ok {
74-
val, err := strconv.ParseInt(raw, 10, 32)
75-
if err != nil {
76-
return fmt.Errorf("failed to parse %q: %w", key, err)
77-
}
78-
*target = int32(val)
79-
}
80-
return nil
81-
}
82-
}
44+
var AsInt32 = parser.As[int32]
8345

8446
// AsInt64 parses the value at key as an int64 into the target, if it exists.
85-
func AsInt64(key string, target *int64) ParseFunc {
86-
return func(data map[string]string) error {
87-
if raw, ok := data[key]; ok {
88-
val, err := strconv.ParseInt(raw, 10, 64)
89-
if err != nil {
90-
return fmt.Errorf("failed to parse %q: %w", key, err)
91-
}
92-
*target = val
93-
}
94-
return nil
95-
}
96-
}
47+
var AsInt64 = parser.As[int64]
9748

9849
// AsInt parses the value at key as an int into the target, if it exists.
99-
func AsInt(key string, target *int) ParseFunc {
100-
return func(data map[string]string) error {
101-
if raw, ok := data[key]; ok {
102-
val, err := strconv.Atoi(raw)
103-
if err != nil {
104-
return fmt.Errorf("failed to parse %q: %w", key, err)
105-
}
106-
*target = val
107-
}
108-
return nil
109-
}
110-
}
50+
var AsInt = parser.As[int]
11151

11252
// AsUint16 parses the value at key as an uint16 into the target, if it exists.
113-
func AsUint16(key string, target *uint16) ParseFunc {
114-
return func(data map[string]string) error {
115-
if raw, ok := data[key]; ok {
116-
val, err := strconv.ParseUint(raw, 10, 16)
117-
if err != nil {
118-
return fmt.Errorf("failed to parse %q: %w", key, err)
119-
}
120-
*target = uint16(val)
121-
}
122-
return nil
123-
}
124-
}
53+
var AsUint16 = parser.As[uint16]
12554

12655
// AsUint32 parses the value at key as an uint32 into the target, if it exists.
127-
func AsUint32(key string, target *uint32) ParseFunc {
128-
return func(data map[string]string) error {
129-
if raw, ok := data[key]; ok {
130-
val, err := strconv.ParseUint(raw, 10, 32)
131-
if err != nil {
132-
return fmt.Errorf("failed to parse %q: %w", key, err)
133-
}
134-
*target = uint32(val)
135-
}
136-
return nil
137-
}
138-
}
56+
var AsUint32 = parser.As[uint32]
57+
58+
// AsUint64 parses the value at key as an uint32 into the target, if it exists.
59+
var AsUint64 = parser.As[uint32]
13960

14061
// AsFloat64 parses the value at key as a float64 into the target, if it exists.
141-
func AsFloat64(key string, target *float64) ParseFunc {
142-
return func(data map[string]string) error {
143-
if raw, ok := data[key]; ok {
144-
val, err := strconv.ParseFloat(raw, 64)
145-
if err != nil {
146-
return fmt.Errorf("failed to parse %q: %w", key, err)
147-
}
148-
*target = val
149-
}
150-
return nil
151-
}
152-
}
62+
var AsFloat64 = parser.As[float64]
15363

15464
// AsDuration parses the value at key as a time.Duration into the target, if it exists.
155-
func AsDuration(key string, target *time.Duration) ParseFunc {
156-
return func(data map[string]string) error {
157-
if raw, ok := data[key]; ok {
158-
val, err := time.ParseDuration(raw)
159-
if err != nil {
160-
return fmt.Errorf("failed to parse %q: %w", key, err)
161-
}
162-
*target = val
163-
}
164-
return nil
165-
}
166-
}
65+
var AsDuration = parser.As[time.Duration]
16766

16867
// AsStringSet parses the value at key as a sets.Set[string] (split by ',') into the target, if it exists.
16968
func AsStringSet(key string, target *sets.Set[string]) ParseFunc {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2025 The Knative Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package parser
18+
19+
import (
20+
"fmt"
21+
"strconv"
22+
"time"
23+
)
24+
25+
// ParseFunc is a function taking ConfigMap data and applying a parse operation to it.
26+
type ParseFunc func(map[string]string) error
27+
28+
func Parse(data map[string]string, parsers ...ParseFunc) error {
29+
for _, parse := range parsers {
30+
if err := parse(data); err != nil {
31+
return err
32+
}
33+
}
34+
return nil
35+
}
36+
37+
func AsFunc[T any](
38+
key string,
39+
target *T,
40+
parseVal func(s string) (T, error),
41+
) ParseFunc {
42+
return func(data map[string]string) error {
43+
if raw, ok := data[key]; ok {
44+
val, err := parseVal(raw)
45+
if err != nil {
46+
return fmt.Errorf("failed to parse %q: %w", key, err)
47+
}
48+
*target = val
49+
}
50+
return nil
51+
}
52+
}
53+
54+
func As[T parseable](key string, target *T) ParseFunc {
55+
return AsFunc(key, target, parse)
56+
}
57+
58+
type parseable interface {
59+
int | int16 | int32 | int64 |
60+
uint | uint16 | uint32 | uint64 |
61+
string | bool | float64 | float32 |
62+
time.Duration
63+
}
64+
65+
//nolint:gosec // ignore integer overflow
66+
func parse[T parseable](s string) (T, error) {
67+
var zero T
68+
69+
var val any
70+
var err error
71+
72+
switch any(zero).(type) {
73+
case string:
74+
val = s
75+
case int16:
76+
val, err = strconv.ParseInt(s, 10, 16)
77+
val = int16(val.(int64))
78+
case int32:
79+
val, err = strconv.ParseInt(s, 10, 32)
80+
val = int32(val.(int64))
81+
case int64:
82+
val, err = strconv.ParseInt(s, 10, 64)
83+
case uint16:
84+
val, err = strconv.ParseUint(s, 10, 16)
85+
val = uint16(val.(uint64))
86+
case uint32:
87+
val, err = strconv.ParseUint(s, 10, 32)
88+
val = uint32(val.(uint64))
89+
case uint64:
90+
val, err = strconv.ParseUint(s, 10, 64)
91+
case float64:
92+
val, err = strconv.ParseFloat(s, 64)
93+
case float32:
94+
val, err = strconv.ParseFloat(s, 64)
95+
val = float32(val.(float64))
96+
case bool:
97+
val, err = strconv.ParseBool(s)
98+
case time.Duration:
99+
val, err = time.ParseDuration(s)
100+
case int:
101+
val, err = strconv.ParseInt(s, 10, 0)
102+
val = int(val.(int64))
103+
case uint:
104+
val, err = strconv.ParseUint(s, 10, 0)
105+
val = uint(val.(uint64))
106+
}
107+
108+
return val.(T), err
109+
}

vendor/modules.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ k8s.io/utils/trace
852852
# knative.dev/hack v0.0.0-20250514121446-f525e187efdc
853853
## explicit; go 1.21
854854
knative.dev/hack
855-
# knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090
855+
# knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7
856856
## explicit; go 1.24.0
857857
knative.dev/pkg/apis
858858
knative.dev/pkg/apis/duck
@@ -872,6 +872,7 @@ knative.dev/pkg/codegen/cmd/injection-gen/generators/reconciler
872872
knative.dev/pkg/codegen/cmd/injection-gen/namer
873873
knative.dev/pkg/codegen/cmd/injection-gen/tags
874874
knative.dev/pkg/configmap
875+
knative.dev/pkg/configmap/parser
875876
knative.dev/pkg/controller
876877
knative.dev/pkg/environment
877878
knative.dev/pkg/hack

0 commit comments

Comments
 (0)