Skip to content

Commit e18301a

Browse files
committed
chore(logs): copy utils func
1 parent fb04460 commit e18301a

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
@@ -197,3 +197,12 @@ func FormatPossibleValues(values ...string) []string {
197197
}
198198
return formattedValues
199199
}
200+
201+
// Map applies mapFn to each element in input and collects the results in a new slice
202+
func Map[T, U any](input []T, mapFn func(T) U) []U {
203+
values := make([]U, len(input))
204+
for i := range input {
205+
values[i] = mapFn(input[i])
206+
}
207+
return values
208+
}

internal/pkg/utils/utils_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,44 @@ func TestGetSliceFromPointer(t *testing.T) {
363363
})
364364
}
365365
}
366+
367+
func TestMap(t *testing.T) {
368+
type args[T any, U any] struct {
369+
input []T
370+
mapFn func(T) U
371+
}
372+
type testCase[T any, U any] struct {
373+
name string
374+
args args[T, U]
375+
want []U
376+
}
377+
tests := []testCase[string, *string]{
378+
{
379+
name: "default",
380+
args: args[string, *string]{
381+
input: []string{"foo", "bar"},
382+
mapFn: func(s string) *string {
383+
return Ptr(s)
384+
},
385+
},
386+
want: []*string{Ptr("foo"), Ptr("bar")},
387+
},
388+
{
389+
name: "input slice is nil",
390+
args: args[string, *string]{
391+
input: nil,
392+
mapFn: func(s string) *string {
393+
return Ptr(s)
394+
},
395+
},
396+
want: []*string{},
397+
},
398+
}
399+
for _, tt := range tests {
400+
t.Run(tt.name, func(t *testing.T) {
401+
if got := Map(tt.args.input, tt.args.mapFn); !reflect.DeepEqual(got, tt.want) {
402+
t.Errorf("Map() = %v, want %v", got, tt.want)
403+
}
404+
})
405+
}
406+
}

0 commit comments

Comments
 (0)