internal/impl: fix Has for proto3 scalars

The definition of Has for proto3 scalars is whether the value is non-zero.

Change-Id: I6aee92dd518d63a66515ad35da84b2be7aa22527
Reviewed-on: https://go-review.googlesource.com/c/150320
Reviewed-by: Herbie Ong <herbie@google.com>
Reviewed-by: Joe Tsai <joetsai@google.com>
This commit is contained in:
Joe Tsai 2018-11-19 15:27:09 -08:00 committed by Joe Tsai
parent c6320b9f23
commit 44e389ca6c
2 changed files with 9 additions and 3 deletions

View File

@ -192,11 +192,11 @@ func fieldInfoForScalar(fd pref.FieldDescriptor, fs reflect.StructField) fieldIn
case reflect.Bool:
return rv.Bool()
case reflect.Int32, reflect.Int64:
return rv.Int() > 0
return rv.Int() != 0
case reflect.Uint32, reflect.Uint64:
return rv.Uint() > 0
return rv.Uint() != 0
case reflect.Float32, reflect.Float64:
return rv.Float() > 0
return rv.Float() != 0
case reflect.String, reflect.Slice:
return rv.Len() > 0
default:

View File

@ -351,6 +351,12 @@ func TestScalarProto3(t *testing.T) {
true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
}},
setFields{
2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
},
hasFields{
2: true, 3: true, 6: true, 7: true,
},
clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
equalMessage{&ScalarProto3{}},
})