golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.66k stars 1.58k forks source link

protocmp.Transform can not handle NaN #1489

Closed molon closed 1 year ago

molon commented 1 year ago

What version of protobuf and what language are you using? Version: v1.28.1

What did you do?

    pb := &pb3.Scalars{
        SFloat:  float32(math.NaN()),
        SDouble: math.NaN(),
    }
    assert.Equal(t, "", cmp.Diff(pb, pb, protocmp.Transform()))

What did you expect to see? PASS

What did you see instead?

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1 +1,8 @@
                            +  (*textpb3.Scalars)(Inverse(protocmp.Transform, protocmp.Message{
                            +   "@type":    s"pb3.Scalars",
                            +-  "s_double": float64(NaN),
                            ++  "s_double": float64(NaN),
                            +-  "s_float":  float32(NaN),
                            ++  "s_float":  float32(NaN),
                            +  }))

Anything else we should know about your project / environment? No

dsnet commented 1 year ago

protocmp.Transform simply transforms a proto.Message into a form that cmp understands. Afterwards, you can compose other options with the result to achieve the desired outcome. In this case, you could compose protocmp.Transform with cmpopts.EquateNaNs.