@@ -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.
1919func 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 {}
0 commit comments