Skip to content

Commit 5a24a5b

Browse files
committed
useragent: move all processing to fromAny
1 parent ac02a7c commit 5a24a5b

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

useragent/from.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,31 @@ import (
1010
"github.com/hashicorp/aws-sdk-go-base/v2/internal/slices"
1111
)
1212

13-
// FromSlice applies the conversion defined in [fromString] to all elements
13+
// FromSlice applies the conversion defined in [fromAny] to all elements
1414
// of a slice
1515
//
1616
// Slices of types which cannot assert to a string, empty string values, and string
1717
// values which do not match the expected `{product}/{version} ({comment})`
1818
// pattern (where version and comment are optional) return a zero value struct.
1919
func FromSlice[T any](sl []T) config.UserAgentProducts {
20-
return slices.ApplyToAll(sl, func(v T) config.UserAgentProduct {
21-
if s, ok := any(v).(string); ok {
22-
return fromString(s)
23-
}
24-
return config.UserAgentProduct{}
25-
})
20+
return slices.ApplyToAll(sl, func(v T) config.UserAgentProduct { return fromAny(v) })
2621
}
2722

28-
// fromString separates the provided string into the constituent parts
29-
// expected by the UserAgentProduct struct
30-
//
31-
// Values which do not match the expected `{product}/{version} ({comment})`
32-
// pattern, where version and comment are optional, return a zero value struct.
33-
func fromString(s string) config.UserAgentProduct {
34-
parts := strings.Split(s, "/")
35-
switch len(parts) {
36-
case 1:
37-
return config.UserAgentProduct{Name: parts[0]}
38-
case 2: //nolint: mnd
39-
subparts := strings.Split(parts[1], "(")
40-
if len(subparts) == 2 { //nolint: mnd
41-
version := strings.TrimSpace(subparts[0])
42-
comment := strings.TrimSuffix(subparts[1], ")")
43-
return config.UserAgentProduct{Name: parts[0], Version: version, Comment: comment}
23+
func fromAny(v any) config.UserAgentProduct {
24+
if s, ok := v.(string); ok {
25+
parts := strings.Split(s, "/")
26+
switch len(parts) {
27+
case 1:
28+
return config.UserAgentProduct{Name: parts[0]}
29+
case 2: //nolint: mnd
30+
subparts := strings.Split(parts[1], "(")
31+
if len(subparts) == 2 { //nolint: mnd
32+
version := strings.TrimSpace(subparts[0])
33+
comment := strings.TrimSuffix(subparts[1], ")")
34+
return config.UserAgentProduct{Name: parts[0], Version: version, Comment: comment}
35+
}
36+
return config.UserAgentProduct{Name: parts[0], Version: parts[1]}
4437
}
45-
return config.UserAgentProduct{Name: parts[0], Version: parts[1]}
4638
}
4739

4840
return config.UserAgentProduct{}

useragent/from_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,24 @@ func TestFromSlice(t *testing.T) {
6161
}
6262
}
6363

64-
func Test_fromString(t *testing.T) {
64+
func Test_fromAny(t *testing.T) {
6565
t.Parallel()
6666

6767
tests := []struct {
6868
name string
69-
s string
69+
v any
7070
want config.UserAgentProduct
7171
}{
72+
{
73+
"nil",
74+
nil,
75+
config.UserAgentProduct{},
76+
},
77+
{
78+
"non-string",
79+
1,
80+
config.UserAgentProduct{},
81+
},
7282
{
7383
"empty",
7484
"",
@@ -126,9 +136,9 @@ func Test_fromString(t *testing.T) {
126136
for _, tt := range tests {
127137
t.Run(tt.name, func(t *testing.T) {
128138
t.Parallel()
129-
got := fromString(tt.s)
139+
got := fromAny(tt.v)
130140
if !reflect.DeepEqual(got, tt.want) {
131-
t.Errorf("fromString() = %+v, want %+v", got, tt.want)
141+
t.Errorf("fromAny() = %+v, want %+v", got, tt.want)
132142
}
133143
})
134144
}

0 commit comments

Comments
 (0)