Skip to content

feature: Add omit Struct Tag #652

@theory

Description

@theory

Is your feature request related to a problem? Please describe.

I'd like to have cbor marshal a struct but omit certain fields.

Describe the solution you'd like

Add a new strut tag, perhaps simply omit, that tells cbor not to marshal a field. It'd look something like this:

type Entity struct {
	_   struct{} `cbor:",toarray"`
	ID   uint64  `json:"id"`
	Type string  `cbor:",omit" json:"typeOf"`
	Name string  `json:"name"`
}

When cbor marshals an instance of this strut, it would include data for ID and Name but not Type.

Describe alternatives you've considered

I'm currently managing this by using an inner struct that contains just the fields I want marshaled and just marshaling it in MarshsalCBOR. This works, but conflicts with the JSON marshaling, in which I want to include Type.

Perhaps I'm missing some other solution, though. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions