Skip to content

Commit 472ff26

Browse files
authored
refactor(cli): add ConvertStringMapToInterfaceMap util function (#790)
1 parent 2c00ad0 commit 472ff26

File tree

20 files changed

+133
-173
lines changed

20 files changed

+133
-173
lines changed

internal/cmd/image/create/create.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,18 +340,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
340340
}
341341

342342
func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload {
343-
var labelsMap *map[string]any
344-
if model.Labels != nil && len(*model.Labels) > 0 {
345-
// convert map[string]string to map[string]interface{}
346-
labelsMap = utils.Ptr(map[string]interface{}{})
347-
for k, v := range *model.Labels {
348-
(*labelsMap)[k] = v
349-
}
350-
}
351343
payload := iaas.CreateImagePayload{
352344
DiskFormat: &model.DiskFormat,
353345
Name: &model.Name,
354-
Labels: labelsMap,
346+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
355347
MinDiskSize: model.MinDiskSize,
356348
MinRam: model.MinRam,
357349
Protected: model.Protected,

internal/cmd/image/update/update.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command, cliArgs []string) (*inputM
243243
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateImageRequest {
244244
request := apiClient.UpdateImage(ctx, model.ProjectId, model.Id)
245245
payload := iaas.NewUpdateImagePayload()
246-
var labelsMap *map[string]any
247-
if model.Labels != nil && len(*model.Labels) > 0 {
248-
// convert map[string]string to map[string]interface{}
249-
labelsMap = utils.Ptr(map[string]interface{}{})
250-
for k, v := range *model.Labels {
251-
(*labelsMap)[k] = v
252-
}
253-
}
246+
254247
// Config *ImageConfig `json:"config,omitempty"`
255248
payload.DiskFormat = model.DiskFormat
256-
payload.Labels = labelsMap
249+
payload.Labels = utils.ConvertStringMapToInterfaceMap(model.Labels)
257250
payload.MinDiskSize = model.MinDiskSize
258251
payload.MinRam = model.MinRam
259252
payload.Name = model.Name

internal/cmd/key-pair/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
124124
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateKeyPairRequest {
125125
req := apiClient.CreateKeyPair(ctx)
126126

127-
var labelsMap *map[string]interface{}
128-
if model.Labels != nil && len(*model.Labels) > 0 {
129-
// convert map[string]string to map[string]interface{}
130-
labelsMap = utils.Ptr(map[string]interface{}{})
131-
for k, v := range *model.Labels {
132-
(*labelsMap)[k] = v
133-
}
134-
}
135-
136127
payload := iaas.CreateKeyPairPayload{
137128
Name: model.Name,
138-
Labels: labelsMap,
129+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
139130
PublicKey: model.PublicKey,
140131
}
141132

internal/cmd/key-pair/update/update.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,8 @@ func configureFlags(cmd *cobra.Command) {
8787
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateKeyPairRequest {
8888
req := apiClient.UpdateKeyPair(ctx, *model.KeyPairName)
8989

90-
var labelsMap *map[string]interface{}
91-
if model.Labels != nil && len(*model.Labels) > 0 {
92-
// convert map[string]string to map[string]interface{}
93-
labelsMap = utils.Ptr(map[string]interface{}{})
94-
for k, v := range *model.Labels {
95-
(*labelsMap)[k] = v
96-
}
97-
}
9890
payload := iaas.UpdateKeyPairPayload{
99-
Labels: labelsMap,
91+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
10092
}
10193
return req.UpdateKeyPairPayload(payload)
10294
}

internal/cmd/network-area/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,9 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
172172
}
173173
}
174174

175-
var labelsMap *map[string]interface{}
176-
if model.Labels != nil && len(*model.Labels) > 0 {
177-
// convert map[string]string to map[string]interface{}
178-
labelsMap = utils.Ptr(map[string]interface{}{})
179-
for k, v := range *model.Labels {
180-
(*labelsMap)[k] = v
181-
}
182-
}
183-
184175
payload := iaas.CreateNetworkAreaPayload{
185176
Name: model.Name,
186-
Labels: labelsMap,
177+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
187178
AddressFamily: &iaas.CreateAreaAddressFamily{
188179
Ipv4: &iaas.CreateAreaIPv4{
189180
DefaultNameservers: model.DnsNameServers,

internal/cmd/network-area/route/create/create.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,21 +147,12 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
147147
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNetworkAreaRouteRequest {
148148
req := apiClient.CreateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId)
149149

150-
var labelsMap *map[string]interface{}
151-
if model.Labels != nil && len(*model.Labels) > 0 {
152-
// convert map[string]string to map[string]interface{}
153-
labelsMap = utils.Ptr(map[string]interface{}{})
154-
for k, v := range *model.Labels {
155-
(*labelsMap)[k] = v
156-
}
157-
}
158-
159150
payload := iaas.CreateNetworkAreaRoutePayload{
160151
Ipv4: &[]iaas.Route{
161152
{
162153
Prefix: model.Prefix,
163154
Nexthop: model.Nexthop,
164-
Labels: labelsMap,
155+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
165156
},
166157
},
167158
}

internal/cmd/network-area/route/update/update.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,8 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
130130
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateNetworkAreaRouteRequest {
131131
req := apiClient.UpdateNetworkAreaRoute(ctx, *model.OrganizationId, *model.NetworkAreaId, model.RouteId)
132132

133-
// convert map[string]string to map[string]interface{}
134-
labelsMap := make(map[string]interface{})
135-
for k, v := range *model.Labels {
136-
labelsMap[k] = v
137-
}
138-
139133
payload := iaas.UpdateNetworkAreaRoutePayload{
140-
Labels: &labelsMap,
134+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
141135
}
142136
req = req.UpdateNetworkAreaRoutePayload(payload)
143137

internal/cmd/network-area/update/update.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
153153
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiPartialUpdateNetworkAreaRequest {
154154
req := apiClient.PartialUpdateNetworkArea(ctx, *model.OrganizationId, model.AreaId)
155155

156-
var labelsMap *map[string]interface{}
157-
if model.Labels != nil && len(*model.Labels) > 0 {
158-
// convert map[string]string to map[string]interface{}
159-
labelsMap = utils.Ptr(map[string]interface{}{})
160-
for k, v := range *model.Labels {
161-
(*labelsMap)[k] = v
162-
}
163-
}
164-
165156
payload := iaas.PartialUpdateNetworkAreaPayload{
166157
Name: model.Name,
167-
Labels: labelsMap,
158+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
168159
AddressFamily: &iaas.UpdateAreaAddressFamily{
169160
Ipv4: &iaas.UpdateAreaIPv4{
170161
DefaultNameservers: model.DnsNameServers,

internal/cmd/network-interface/create/create.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
207207
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNicRequest {
208208
req := apiClient.CreateNic(ctx, model.ProjectId, *model.NetworkId)
209209

210-
var labelsMap *map[string]interface{}
211-
if model.Labels != nil && len(*model.Labels) > 0 {
212-
// convert map[string]string to map[string]interface{}
213-
convertedMap := make(map[string]interface{}, len(*model.Labels))
214-
for k, v := range *model.Labels {
215-
convertedMap[k] = v
216-
}
217-
labelsMap = &convertedMap
218-
}
219-
220210
payload := iaas.CreateNicPayload{
221211
AllowedAddresses: model.AllowedAddresses,
222212
Ipv4: model.Ipv4,
223213
Ipv6: model.Ipv6,
224-
Labels: labelsMap,
214+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
225215
Name: model.Name,
226216
NicSecurity: model.NicSecurity,
227217
SecurityGroups: model.SecurityGroups,

internal/cmd/network-interface/update/update.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
199199
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiUpdateNicRequest {
200200
req := apiClient.UpdateNic(ctx, model.ProjectId, *model.NetworkId, model.NicId)
201201

202-
var labelsMap *map[string]interface{}
203-
if model.Labels != nil && len(*model.Labels) > 0 {
204-
// convert map[string]string to map[string]interface{}
205-
convertedMap := make(map[string]interface{}, len(*model.Labels))
206-
for k, v := range *model.Labels {
207-
convertedMap[k] = v
208-
}
209-
labelsMap = &convertedMap
210-
}
211-
212202
payload := iaas.UpdateNicPayload{
213203
AllowedAddresses: model.AllowedAddresses,
214-
Labels: labelsMap,
204+
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
215205
Name: model.Name,
216206
NicSecurity: model.NicSecurity,
217207
SecurityGroups: model.SecurityGroups,

0 commit comments

Comments
 (0)