whyrusleeping / cbor-gen

Codegen for cbor codecs on your types
MIT License
35 stars 25 forks source link

Fix: add empty struct marshaling #57

Open gallexis opened 3 years ago

gallexis commented 3 years ago

I needed to encode a map[string]struct{} but ran into this error :

testing/cbor_gen.go:172:25: t.EmptyStruct.MarshalCBOR undefined (type struct {} has no field or method MarshalCBOR) which is logical since we can't attach a function to an empty struct :


EmptyStruct struct{}
...
if err := EmptyStruct.UnmarshalCBOR(br); err != nil {.      // it fails here
     return xerrors.Errorf("unmarshaling t.EmptyStruct: %w", err)
}

So I made this fix to handle this case and added some tests

Stebalien commented 3 years ago

In general, I'd expect an empty struct to marshel to an empty object (in our case, that would be an empty list). So I'd rather not handle struct{} generically at the moment.

But in this case, map[string]struct{} really just represents a set. That should probably marshal to an array of keys.

SGTY @whyrusleeping?