Skip to content

Map Merging Preventing Updates #323

@Linuturk

Description

@Linuturk

I have a struct similar to the following:

type Device struct {
	UUID gocql.UUID `json:"-" db:"uuid"`
	Name string     `json:"name" db:"name"`
	Tags map[string]string `json:"tags" db:"tags"`
}

I've satisfied the FindOne, Update, Delete, and Create methods on this type. I can successfully create, delete, and read a device, but I'm having a problem with the Tags field when Updates are processed.

func (d *Device) Update(obj interface{}, r api2go.Request) (api2go.Responder, error) {

	// assert a device
	device := obj.(*Device)

	// check the device type
	if !device.ValidType() {
		return api2go.Response{}, api2go.NewHTTPError(nil, "Invalid device type given", http.StatusBadRequest)
	}

	// update the device in the database
	err := DeviceTable.UpdateOnex(device)
	if err != nil {
		return api2go.Response{}, api2go.NewHTTPError(err, "Failed to update device", http.StatusInternalServerError)
	}

	return api2go.Response{Code: http.StatusNoContent}, nil
}

Both the Device "d" and the "obj" provided by api2go contain the updated fields. Unfortunately, api2go is pulling any existing data from my database, and overwriting that struct with the struct provided by the client. For example, if the database contains:

{
    "data": {
        "type": "devices",
        "id": "09631dba-c3f9-11e7-88e5-bb32f9c079f7",
        "attributes": {
            "name": "Static Test Device",
            "tags": {
                "building": "1",
                "lab": "1A",
                "rack": "7",
                "shelf": "3",
                "foo": "bar"
            }
        }
    }
}

and I send an update with this data:

{
    "data": {
        "type": "devices",
        "id": "09631dba-c3f9-11e7-88e5-bb32f9c079f7",
        "attributes": {
            "name": "Static Test Device Updated",
            "tags": {
                "building": "E",
                "lab": "Dark Room",
                "rack": "5",
                "shelf": "2"
            }
        }
    }
}

The tags section get merged instead of overwritten. I end up with:

{
    "data": {
        "type": "devices",
        "id": "09631dba-c3f9-11e7-88e5-bb32f9c079f7",
        "attributes": {
            "name": "Static Test Device Updated",
            "tags": {
                "building": "E",
                "lab": "Dark Room",
                "rack": "5",
                "shelf": "2",
                "foo": "bar" <<<<< shouldn't be there
            }
        }
    }
}

I've confirmed this by checking the obj and d variables before I do any database interactions. This might be related to #148

Is there any way I can prevent that map from being merged instead of overwritten?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions