Skip to content

Commit 27cd603

Browse files
committed
chore(logs): copy utils func
1 parent 0646fb9 commit 27cd603

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

internal/cmd/logs/access_token/create/create.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
138138

139139
func buildRequest(ctx context.Context, model *inputModel, apiClient *logs.APIClient) logs.ApiCreateAccessTokenRequest {
140140
req := apiClient.DefaultAPI.CreateAccessToken(ctx, model.ProjectId, model.Region, model.InstanceId)
141-
permissions := make([]logs.PermissionsInner, len(model.Permissions))
142-
for i, permission := range model.Permissions {
143-
permissions[i] = logs.PermissionsInner(permission)
144-
}
141+
permissions := utils.Map(model.Permissions, func(t string) logs.PermissionsInner {
142+
return logs.PermissionsInner(t)
143+
})
145144
return req.CreateAccessTokenPayload(logs.CreateAccessTokenPayload{
146145
Description: model.Description,
147146
DisplayName: model.DisplayName,

internal/pkg/utils/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,12 @@ func FormatPossibleValues(values ...string) []string {
186186
}
187187
return formattedValues
188188
}
189+
190+
// Map applies mapFn to each element in input and collects the results in a new slice
191+
func Map[T, U any](input []T, mapFn func(T) U) []U {
192+
values := make([]U, len(input))
193+
for i := range input {
194+
values[i] = mapFn(input[i])
195+
}
196+
return values
197+
}

internal/pkg/utils/utils_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,44 @@ func TestGetSliceFromPointer(t *testing.T) {
311311
})
312312
}
313313
}
314+
315+
func TestMap(t *testing.T) {
316+
type args[T any, U any] struct {
317+
input []T
318+
mapFn func(T) U
319+
}
320+
type testCase[T any, U any] struct {
321+
name string
322+
args args[T, U]
323+
want []U
324+
}
325+
tests := []testCase[string, *string]{
326+
{
327+
name: "default",
328+
args: args[string, *string]{
329+
input: []string{"foo", "bar"},
330+
mapFn: func(s string) *string {
331+
return Ptr(s)
332+
},
333+
},
334+
want: []*string{Ptr("foo"), Ptr("bar")},
335+
},
336+
{
337+
name: "input slice is nil",
338+
args: args[string, *string]{
339+
input: nil,
340+
mapFn: func(s string) *string {
341+
return Ptr(s)
342+
},
343+
},
344+
want: []*string{},
345+
},
346+
}
347+
for _, tt := range tests {
348+
t.Run(tt.name, func(t *testing.T) {
349+
if got := Map(tt.args.input, tt.args.mapFn); !reflect.DeepEqual(got, tt.want) {
350+
t.Errorf("Map() = %v, want %v", got, tt.want)
351+
}
352+
})
353+
}
354+
}

0 commit comments

Comments
 (0)