Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update an item using a struct containing only some items. #79

Open
chackett opened this issue Nov 26, 2018 · 2 comments
Open

How to update an item using a struct containing only some items. #79

chackett opened this issue Nov 26, 2018 · 2 comments

Comments

@chackett
Copy link

chackett commented Nov 26, 2018

So, my use case is for multi step registration:

User submits first form with details I want to use to "create" a user item:

type UserStageOne struct {
UserID *string json:"userID,omitempty"
Name *string json:"name,omitempty"
Email *string json:"email,omitempty"
Password *string json:"password,omitempty"
SignUpIP *string json:"signUpIP,omitempty"
RegisteredTime time.Time json:"registeredTime,omitempty"
Verified bool json:"verified,omitempty"
}

So when the user submits the first firm the above item is created. The user is then presented with a second form which contains the following struct. I now want to "update" the item created from the first request to include the new fields from the following struct:

type UserFinal struct {
UserID *string json:"userID,omitempty" dynamo:"UserID"
RegData1 *string json:"regData1,omitempty"
RegData2 *string json:"regData2,omitempty"
RegData3 *string json:"regData3,omitempty"
AgreedTerms bool json:"agreedTerms,omitempty"
}

How can I achieve that? PUT isn't appropriate and I can't seem to get UPDATE to work as I would like. I don't want to have to enumerate the various fields etc.

Thank you.

@guregu
Copy link
Owner

guregu commented Nov 27, 2018

Right now there isn't a super easy way to do this. But I think this is an interesting use case and I would like to add support.

In the meantime, I wonder if something like this would work? Totally untested.

func UpdateFromItem(table dynamo.Table, hashKey string, item interface{}) (*dynamo.Update, error) {
	m, err := dynamo.MarshalItem(item)
	if err != nil {
		return nil, err
	}
	u := table.Update(hashKey, m[hashKey])
	for k, v := range m {
		if k == hashKey {
			continue
		}
		u.Set(k, v)
	}
	return u, nil
}

you would use it like update, err := UpdateFromItem(table, "UserID", userFinalData) and then run the update.

If you wanted logic that ignored nils and stuff you could include it in the loop over m.

@chackett
Copy link
Author

Yea that would probably work alright. I'll give it a go the next time i'm working on that piece of code.

In the end I ended up just retrieving the record and updating the fields then calling update. Crude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants