Skip to content

Commit 2dfeb43

Browse files
committed
handling edge cases
1 parent a8e7f79 commit 2dfeb43

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

internal/powerplatform/services/data_record/api_data_record.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,18 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
238238
if nestedMap, ok := value.(map[string]interface{}); ok {
239239
delete(columns, key)
240240
if len(nestedMap) > 0 {
241-
tableLogicalName := nestedMap["table_logical_name"].(string)
241+
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
242+
if !ok {
243+
return nil, fmt.Errorf("table_logical_name field is missing or not a string")
244+
}
245+
dataRecordId, ok := nestedMap["data_record_id"].(string)
246+
if !ok {
247+
return nil, fmt.Errorf("data_record_id field is missing or not a string")
248+
}
242249

243250
entityDefinition := getEntityDefinition(ctx, client, environmentUrl, tableLogicalName)
244251

245-
columns[fmt.Sprintf("%[email protected]", key)] = fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, entityDefinition.LogicalCollectionName, nestedMap["data_record_id"])
252+
columns[fmt.Sprintf("%[email protected]", key)] = fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, entityDefinition.LogicalCollectionName, dataRecordId)
246253
}
247254
}
248255
if nestedMapList, ok := value.([]interface{}); ok {
@@ -317,8 +324,18 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
317324
delete := true
318325
for _, nestedItem := range nestedMapList {
319326
nestedMap := nestedItem.(map[string]interface{})
320-
relationEntityDefinition := getEntityDefinition(ctx, client, environmentUrl, nestedMap["table_logical_name"].(string))
321-
if existingRelation.OdataID == fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, relationEntityDefinition.LogicalCollectionName, nestedMap["data_record_id"]) {
327+
328+
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
329+
if !ok {
330+
return nil, fmt.Errorf("table_logical_name field is missing or not a string")
331+
}
332+
dataRecordId, ok := nestedMap["data_record_id"].(string)
333+
if !ok {
334+
return nil, fmt.Errorf("data_record_id field is missing or not a string")
335+
}
336+
337+
relationEntityDefinition := getEntityDefinition(ctx, client, environmentUrl, tableLogicalName)
338+
if existingRelation.OdataID == fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, relationEntityDefinition.LogicalCollectionName, dataRecordId) {
322339
delete = false
323340
break
324341
}
@@ -338,12 +355,19 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
338355
for _, nestedItem := range nestedMapList {
339356
nestedMap := nestedItem.(map[string]interface{})
340357

341-
tableLogicalName := nestedMap["table_logical_name"].(string)
358+
tableLogicalName, ok := nestedMap["table_logical_name"].(string)
359+
if !ok {
360+
return nil, fmt.Errorf("table_logical_name field is missing or not a string")
361+
}
362+
dataRecordId, ok := nestedMap["data_record_id"].(string)
363+
if !ok {
364+
return nil, fmt.Errorf("data_record_id field is missing or not a string")
365+
}
342366

343367
entityDefinition := getEntityDefinition(ctx, client, environmentUrl, tableLogicalName)
344368

345369
relation := RelationApiBody{
346-
OdataID: fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, entityDefinition.LogicalCollectionName, nestedMap["data_record_id"]),
370+
OdataID: fmt.Sprintf("%s/api/data/%s/%s(%s)", environmentUrl, constants.DATAVERSE_API_VERSION, entityDefinition.LogicalCollectionName, dataRecordId),
347371
}
348372
_, err = client.Api.Execute(ctx, "POST", apiUrl.String(), nil, relation, []int{http.StatusOK, http.StatusNoContent}, nil)
349373
if err != nil {
@@ -381,10 +405,15 @@ func (client *DataRecordClient) DeleteDataRecord(ctx context.Context, recordId s
381405
for _, nestedItem := range nestedMapList {
382406
nestedMap := nestedItem.(map[string]interface{})
383407

408+
dataRecordId, ok := nestedMap["data_record_id"].(string)
409+
if !ok {
410+
return fmt.Errorf("data_record_id field is missing or not a string")
411+
}
412+
384413
apiUrl = &url.URL{
385414
Scheme: e.Scheme,
386415
Host: e.Host,
387-
Path: fmt.Sprintf("/api/data/%s/%s(%s)/%s(%s)/$ref", constants.DATAVERSE_API_VERSION, tableEntityDefinition.LogicalCollectionName, recordId, key, nestedMap["data_record_id"].(string)),
416+
Path: fmt.Sprintf("/api/data/%s/%s(%s)/%s(%s)/$ref", constants.DATAVERSE_API_VERSION, tableEntityDefinition.LogicalCollectionName, recordId, key, dataRecordId),
388417
}
389418
_, err = client.Api.Execute(ctx, "DELETE", apiUrl.String(), nil, nil, []int{http.StatusOK, http.StatusNoContent}, nil)
390419
if err != nil {

0 commit comments

Comments
 (0)