dynamo icon indicating copy to clipboard operation
dynamo copied to clipboard

Provide public MarshalStruct

Open liangxuuu opened this issue 3 years ago • 3 comments

Can we provide a public function MarshalStruct for external calls, in case the caller just needs to add some extra fields to the marshal process but doesn't want to actually handle the marshal process himself?

For example:

// dynamodb table
type Item struct {
    PK string `dynamo:",hash"`
}

type User struct {
    Username string
    Email string
}

type UserItem struct {
    *Item
    *User
}

// We want to add PK automatically in the marshal process
func (u *User) MarshalDynamoItem() (map[string]*dynamodb.AttributeValue, error) {
    return dynamo.MarshalStruct(&UserItem{&Item{PK:"U#"+u.Username}, u})
}

liangxuuu avatar Nov 11 '21 07:11 liangxuuu

To clarify: Would MarshalStruct work like MarshalItem but ignore custom marshalers (MarshalDynamoItem)?

guregu avatar Nov 18 '21 04:11 guregu

To clarify: Would MarshalStruct work like MarshalItem but ignore custom marshalers (MarshalDynamoItem)?

Yes, I think this will be very useful

liangxuuu avatar Nov 26 '21 09:11 liangxuuu

I don't have support for this yet, but auxiliary types are supported now in the latest version: https://github.com/guregu/dynamo/issues/181

You can use them as a workaround for this issue.

guregu avatar Feb 06 '22 17:02 guregu