Skip to content

Commit

Permalink
Check if null before unmarshalling (#290)
Browse files Browse the repository at this point in the history
* Check if null before unmarshalling

Checks if an attribute is null before attempting to unmarshal it.

Closes #289

* Use a helper for safely unamrshalling

Uses a helper method for this so we have it handy next time we need to
unmarshal an attribute that could be null.
  • Loading branch information
mitchnielsen authored Oct 29, 2024
1 parent e98e2d7 commit 47f66ff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
19 changes: 19 additions & 0 deletions internal/provider/helpers/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package helpers

import (
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
)

// SafeUnmarshal is a helper function for safely unmarshalling a JSON object by checking if
// it is null before attempting to unmarshal it. This should always be used for optional attributes.
func SafeUnmarshal(attribute jsontypes.Normalized) (map[string]interface{}, diag.Diagnostics) {
var diags diag.Diagnostics

result := map[string]interface{}{}
if !attribute.IsNull() {
diags = attribute.Unmarshal(&result)
}

return result, diags
}
28 changes: 16 additions & 12 deletions internal/provider/resources/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,20 +307,20 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
return
}

var parameters map[string]interface{}
resp.Diagnostics.Append(plan.Parameters.Unmarshal(&parameters)...)
parameters, diags := helpers.SafeUnmarshal(plan.Parameters)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

var jobVariables map[string]interface{}
resp.Diagnostics.Append(plan.JobVariables.Unmarshal(&jobVariables)...)
jobVariables, diags := helpers.SafeUnmarshal(plan.JobVariables)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

var parameterOpenAPISchema map[string]interface{}
resp.Diagnostics.Append(plan.ParameterOpenAPISchema.Unmarshal(&parameterOpenAPISchema)...)
parameterOpenAPISchema, diags := helpers.SafeUnmarshal(plan.ParameterOpenAPISchema)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -473,15 +473,19 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ
}

var parameters map[string]interface{}
resp.Diagnostics.Append(model.Parameters.Unmarshal(&parameters)...)
if resp.Diagnostics.HasError() {
return
if !model.Parameters.IsNull() {
resp.Diagnostics.Append(model.Parameters.Unmarshal(&parameters)...)
if resp.Diagnostics.HasError() {
return
}
}

var jobVariables map[string]interface{}
resp.Diagnostics.Append(model.JobVariables.Unmarshal(&jobVariables)...)
if resp.Diagnostics.HasError() {
return
if !model.JobVariables.IsNull() {
resp.Diagnostics.Append(model.JobVariables.Unmarshal(&jobVariables)...)
if resp.Diagnostics.HasError() {
return
}
}

payload := api.DeploymentUpdate{
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/resources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func (r *WorkPoolResource) Create(ctx context.Context, req resource.CreateReques
return
}

baseJobTemplate := map[string]interface{}{}
resp.Diagnostics.Append(plan.BaseJobTemplate.Unmarshal(&baseJobTemplate)...)
baseJobTemplate, diag := helpers.SafeUnmarshal(plan.BaseJobTemplate)
resp.Diagnostics.Append(diag...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -277,8 +277,8 @@ func (r *WorkPoolResource) Update(ctx context.Context, req resource.UpdateReques
return
}

baseJobTemplate := map[string]interface{}{}
resp.Diagnostics.Append(plan.BaseJobTemplate.Unmarshal(&baseJobTemplate)...)
baseJobTemplate, diag := helpers.SafeUnmarshal(plan.BaseJobTemplate)
resp.Diagnostics.Append(diag...)
if resp.Diagnostics.HasError() {
return
}
Expand Down

0 comments on commit 47f66ff

Please sign in to comment.