mirror of https://github.com/knative/caching.git
upgrade to latest dependencies (#942)
bumping knative.dev/pkg 4e27b2e...05e18ff: > 05e18ff pull configmap parsing into separate package (# 3185) Signed-off-by: Knative Automation <automation@knative.team>
This commit is contained in:
parent
0d09958897
commit
911ea8102c
2
go.mod
2
go.mod
|
@ -11,7 +11,7 @@ require (
|
|||
k8s.io/code-generator v0.33.1
|
||||
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff
|
||||
knative.dev/hack v0.0.0-20250514121446-f525e187efdc
|
||||
knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090
|
||||
knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
4
go.sum
4
go.sum
|
@ -676,8 +676,8 @@ k8s.io/utils v0.0.0-20241210054802-24370beab758 h1:sdbE21q2nlQtFh65saZY+rRM6x6aJ
|
|||
k8s.io/utils v0.0.0-20241210054802-24370beab758/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
knative.dev/hack v0.0.0-20250514121446-f525e187efdc h1:8HmclJlA0zNE/G1SkgdC3/IFSSyhaSz2iIhihU6YbEo=
|
||||
knative.dev/hack v0.0.0-20250514121446-f525e187efdc/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY=
|
||||
knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090 h1:lrYxxGsWJJjphcg7ZS4gYmC52qjoYbU/+MasnoaVn0g=
|
||||
knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090/go.mod h1:nrao281iv0FJ5u/c0j3zmrinAlyOUZo9hJUxbrbsxWo=
|
||||
knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7 h1:034E/3HHcG7y533bMXa+d9vD5yjEow2thggnnsGlbTM=
|
||||
knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7/go.mod h1:nrao281iv0FJ5u/c0j3zmrinAlyOUZo9hJUxbrbsxWo=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
|
||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
||||
|
|
|
@ -18,7 +18,6 @@ package configmap
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -26,144 +25,44 @@ import (
|
|||
"k8s.io/apimachinery/pkg/api/validation"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"knative.dev/pkg/configmap/parser"
|
||||
)
|
||||
|
||||
// ParseFunc is a function taking ConfigMap data and applying a parse operation to it.
|
||||
type ParseFunc func(map[string]string) error
|
||||
type ParseFunc = parser.ParseFunc
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
var AsString = parser.As[string]
|
||||
|
||||
// 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 {
|
||||
val, err := strconv.ParseBool(raw)
|
||||
*target = val // If err != nil — this is always false.
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsBool = parser.As[bool]
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
var AsInt16 = parser.As[int16]
|
||||
|
||||
// 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 fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = int32(val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsInt32 = parser.As[int32]
|
||||
|
||||
// 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 fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsInt64 = parser.As[int64]
|
||||
|
||||
// AsInt parses the value at key as an int into the target, if it exists.
|
||||
func AsInt(key string, target *int) ParseFunc {
|
||||
return func(data map[string]string) error {
|
||||
if raw, ok := data[key]; ok {
|
||||
val, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsInt = parser.As[int]
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
var AsUint16 = parser.As[uint16]
|
||||
|
||||
// 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 {
|
||||
if raw, ok := data[key]; ok {
|
||||
val, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = uint32(val)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsUint32 = parser.As[uint32]
|
||||
|
||||
// AsUint64 parses the value at key as an uint32 into the target, if it exists.
|
||||
var AsUint64 = parser.As[uint32]
|
||||
|
||||
// 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 fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsFloat64 = parser.As[float64]
|
||||
|
||||
// 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 fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
var AsDuration = parser.As[time.Duration]
|
||||
|
||||
// AsStringSet parses the value at key as a sets.Set[string] (split by ',') into the target, if it exists.
|
||||
func AsStringSet(key string, target *sets.Set[string]) ParseFunc {
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
Copyright 2025 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 parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ParseFunc is a function taking ConfigMap data and applying a parse operation to it.
|
||||
type ParseFunc func(map[string]string) error
|
||||
|
||||
func Parse(data map[string]string, parsers ...ParseFunc) error {
|
||||
for _, parse := range parsers {
|
||||
if err := parse(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AsFunc[T any](
|
||||
key string,
|
||||
target *T,
|
||||
parseVal func(s string) (T, error),
|
||||
) ParseFunc {
|
||||
return func(data map[string]string) error {
|
||||
if raw, ok := data[key]; ok {
|
||||
val, err := parseVal(raw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse %q: %w", key, err)
|
||||
}
|
||||
*target = val
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func As[T parseable](key string, target *T) ParseFunc {
|
||||
return AsFunc(key, target, parse)
|
||||
}
|
||||
|
||||
type parseable interface {
|
||||
int | int16 | int32 | int64 |
|
||||
uint | uint16 | uint32 | uint64 |
|
||||
string | bool | float64 | float32 |
|
||||
time.Duration
|
||||
}
|
||||
|
||||
//nolint:gosec // ignore integer overflow
|
||||
func parse[T parseable](s string) (T, error) {
|
||||
var zero T
|
||||
|
||||
var val any
|
||||
var err error
|
||||
|
||||
switch any(zero).(type) {
|
||||
case string:
|
||||
val = s
|
||||
case int16:
|
||||
val, err = strconv.ParseInt(s, 10, 16)
|
||||
val = int16(val.(int64))
|
||||
case int32:
|
||||
val, err = strconv.ParseInt(s, 10, 32)
|
||||
val = int32(val.(int64))
|
||||
case int64:
|
||||
val, err = strconv.ParseInt(s, 10, 64)
|
||||
case uint16:
|
||||
val, err = strconv.ParseUint(s, 10, 16)
|
||||
val = uint16(val.(uint64))
|
||||
case uint32:
|
||||
val, err = strconv.ParseUint(s, 10, 32)
|
||||
val = uint32(val.(uint64))
|
||||
case uint64:
|
||||
val, err = strconv.ParseUint(s, 10, 64)
|
||||
case float64:
|
||||
val, err = strconv.ParseFloat(s, 64)
|
||||
case float32:
|
||||
val, err = strconv.ParseFloat(s, 64)
|
||||
val = float32(val.(float64))
|
||||
case bool:
|
||||
val, err = strconv.ParseBool(s)
|
||||
case time.Duration:
|
||||
val, err = time.ParseDuration(s)
|
||||
case int:
|
||||
val, err = strconv.ParseInt(s, 10, 0)
|
||||
val = int(val.(int64))
|
||||
case uint:
|
||||
val, err = strconv.ParseUint(s, 10, 0)
|
||||
val = uint(val.(uint64))
|
||||
}
|
||||
|
||||
return val.(T), err
|
||||
}
|
|
@ -852,7 +852,7 @@ k8s.io/utils/trace
|
|||
# knative.dev/hack v0.0.0-20250514121446-f525e187efdc
|
||||
## explicit; go 1.21
|
||||
knative.dev/hack
|
||||
# knative.dev/pkg v0.0.0-20250610210745-4e27b2e68090
|
||||
# knative.dev/pkg v0.0.0-20250612083447-05e18ffb29c7
|
||||
## explicit; go 1.24.0
|
||||
knative.dev/pkg/apis
|
||||
knative.dev/pkg/apis/duck
|
||||
|
@ -872,6 +872,7 @@ knative.dev/pkg/codegen/cmd/injection-gen/generators/reconciler
|
|||
knative.dev/pkg/codegen/cmd/injection-gen/namer
|
||||
knative.dev/pkg/codegen/cmd/injection-gen/tags
|
||||
knative.dev/pkg/configmap
|
||||
knative.dev/pkg/configmap/parser
|
||||
knative.dev/pkg/controller
|
||||
knative.dev/pkg/environment
|
||||
knative.dev/pkg/hack
|
||||
|
|
Loading…
Reference in New Issue