Skip to content

Commit 53061f6

Browse files
committed
refactoring
1 parent 35ab879 commit 53061f6

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

internal/powerplatform/services/data_record/api_data_record.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId, e
267267
if nestedMap, ok := value.(map[string]interface{}); ok {
268268
delete(columns, key)
269269
if len(nestedMap) > 0 {
270-
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
271-
if !ok {
272-
return nil, fmt.Errorf("table_logical_name field is missing or not a string")
273-
}
274-
dataRecordId, ok := nestedMap["data_record_id"].(string)
275-
if !ok {
276-
return nil, fmt.Errorf("data_record_id field is missing or not a string")
270+
tableLogicalName, dataRecordId, err := getTableLogicalNameAndDataRecordIdFromMap(nestedMap)
271+
if err != nil {
272+
return nil, err
277273
}
278274

279275
entityDefinition, err := getEntityDefinition(ctx, client, environmentUrl, tableLogicalName)
@@ -362,13 +358,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId, e
362358
for _, nestedItem := range nestedMapList {
363359
nestedMap := nestedItem.(map[string]interface{})
364360

365-
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
366-
if !ok {
367-
return nil, fmt.Errorf("table_logical_name field is missing or not a string")
368-
}
369-
dataRecordId, ok := nestedMap["data_record_id"].(string)
370-
if !ok {
371-
return nil, fmt.Errorf("data_record_id field is missing or not a string")
361+
tableLogicalName, dataRecordId, err := getTableLogicalNameAndDataRecordIdFromMap(nestedMap)
362+
if err != nil {
363+
return nil, err
372364
}
373365

374366
relationEntityDefinition, err := getEntityDefinition(ctx, client, environmentUrl, tableLogicalName)
@@ -395,13 +387,9 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId, e
395387
for _, nestedItem := range nestedMapList {
396388
nestedMap := nestedItem.(map[string]interface{})
397389

398-
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
399-
if !ok {
400-
return nil, fmt.Errorf("table_logical_name field is missing or not a string")
401-
}
402-
dataRecordId, ok := nestedMap["data_record_id"].(string)
403-
if !ok {
404-
return nil, fmt.Errorf("data_record_id field is missing or not a string")
390+
tableLogicalName, dataRecordId, err := getTableLogicalNameAndDataRecordIdFromMap(nestedMap)
391+
if err != nil {
392+
return nil, err
405393
}
406394

407395
entityDefinition, err := getEntityDefinition(ctx, client, environmentUrl, tableLogicalName)
@@ -486,3 +474,15 @@ func parseLocationHeader(response *api.ApiHttpResponse, environmentUrl string, e
486474
locationHeader = strings.TrimSuffix(locationHeader, ")")
487475
return locationHeader
488476
}
477+
478+
func getTableLogicalNameAndDataRecordIdFromMap(nestedMap map[string]interface{}) (string, string, error) {
479+
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
480+
if !ok {
481+
return "", "", fmt.Errorf("table_logical_name field is missing or not a string")
482+
}
483+
dataRecordId, ok := nestedMap["data_record_id"].(string)
484+
if !ok {
485+
return "", "", fmt.Errorf("data_record_id field is missing or not a string")
486+
}
487+
return tableLogicalName, dataRecordId, nil
488+
}

0 commit comments

Comments
 (0)