-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check if null before unmarshalling (#290)
* 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
1 parent
e98e2d7
commit 47f66ff
Showing
3 changed files
with
39 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters