Fix setting host catalog attributes to nil (#1743)
Internally attributes are not allowed to be the null value. However, as currently implemented, this then breaks our normal model of sending an explicit `null` in the JSON to reset/clear a field. This is a problem because the CLI doesn't have a way to say "set this but to empty" without a lot of hoops, and causes some issues for API users and TF as well. This changes PatchBytes and PatchStruct to return an empty value (or marshaled empty value in the case of PatchBytes) if the source is null or empty. In the host catalog update function we then always set this value if it's in the db mask (removing the nil check). Together this means a null value ends up coming out as an empty value (and thus valid for the DB) from PatchStruct/PatchBytes, and our API translation function already returns nothing for attributes if the length of the fields are zero.
This commit is contained in:
1 parent
b2efce961d
commit
1c39691023
4 files changed
+40
-25
No files matched your search
@@ -118,10 +118,8 @@ var testCases = []testCase{
|
||||
dst: map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
src: nil,
|
||||
expected: map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
src: nil,
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "nil dst",
|
||||
@@ -189,12 +187,6 @@ func TestPatchBytes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPatchBytesErr(t *testing.T) {
|
||||
t.Run("dst", func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
_, err := PatchBytes([]byte("foo"), nil)
|
||||
require.ErrorIs(err, proto.Error)
|
||||
require.True(strings.HasPrefix(err.Error(), "error reading destination data: "))
|
||||
})
|
||||
t.Run("src", func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
_, err := PatchBytes(nil, []byte("foo"))
|
||||
|
||||
Reference in new issue
Block a user