-
Notifications
You must be signed in to change notification settings - Fork 216
Description
This one is a bit odd, but I'm trying to test if a payload can successfully round-trip from the original JSON, through my type's Unmarshal+Marshal cycle and come back out the semantically the same on the other end. (the semantic is the tricky bit)
The way I test that is by unmarshalling the original payload into a any
as well as the payload after the Unmarshal+Marshal and then calling cmp.Diff
.
This works, except that for some fields there's 2 possible encodings for them that are semantically equivalent. A field may come in as either a slice of strings, or a singular string. I deal with this in my Unmarshal method, but my Marshaler has no way of knowing what the original "shape" was and in the case of the single element slice outputs the string instead. This is semantically correct, but obviously go-cmp is unhappy about that.
So, if you get this in:
{
"a": "b"
}
Or:
{
"a": ["b"]
}
My Marshaler will output the equivalent and "most compact" form:
{
"a": "b"
}
What I'm trying to figure out is if I can deal with that situation in go-cmp. The Transformer doesn't seem like it would fit, since I only need to apply this to some specific fields. I've taken a look at FilterPath, but I'm not sure how to apply it.
I suspect the answer is "can't be done", in which case I can reprocess the resulting map[string]any
to deal with the difference. It's very few fields that exhibit this characteristic in practice so that's doable. But if anyone has any ideas on how to do it in go-cmp that would be helpful.