internal/encoding/text: change Value.Float{32,64} to Value.Float

Collapse Value.Float32 and Value.Float64 into single API to keep it
consistent with Value.{Int,Uint}.

Change-Id: I07737e72715fe3cc3f6bcad579cf5d6cfe3757d5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/167317
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Herbie Ong 2019-03-12 20:55:10 -07:00
parent 4989810018
commit 250c6eaf92
3 changed files with 14 additions and 43 deletions

View File

@ -281,11 +281,11 @@ func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, err
return pref.ValueOf(uint64(n)), nil
}
case pref.FloatKind:
if n, ok := input.Float32(); ok {
if n, ok := input.Float(b32); ok {
return pref.ValueOf(float32(n)), nil
}
case pref.DoubleKind:
if n, ok := input.Float64(); ok {
if n, ok := input.Float(b64); ok {
return pref.ValueOf(float64(n)), nil
}
case pref.StringKind:

View File

@ -549,10 +549,6 @@ func Test(t *testing.T) {
}, {
in: `crazy:"x'"'\""\''"'z"`,
wantVal: V(Msg{{ID("crazy"), V(`x'""''z`)}}),
}, {
in: `num: 1.02`,
wantVal: V(Msg{{ID("num"), V(float32(1.02))}}), // Use float32 to test marshaling of Float32 type.
wantOut: `num:1.02`,
}, {
in: `nums: [t,T,true,True,TRUE,f,F,false,False,FALSE]`,
wantVal: V(Msg{{ID("nums"), V(Lst{
@ -794,16 +790,9 @@ spouse: null
want, _ := x.Uint(true)
got, ok := y.Uint(math.MaxUint32 < want)
return got == want && ok
case Float32:
want, _ := x.Float32()
got, ok := y.Float32()
if math.IsNaN(float64(got)) || math.IsNaN(float64(want)) {
return math.IsNaN(float64(got)) == math.IsNaN(float64(want))
}
return got == want && ok
case Float64:
want, _ := x.Float64()
got, ok := y.Float64()
case Float32, Float64:
want, _ := x.Float(true)
got, ok := y.Float(math.MaxFloat32 < math.Abs(want))
if math.IsNaN(got) || math.IsNaN(want) {
return math.IsNaN(got) == math.IsNaN(want)
}

View File

@ -223,40 +223,22 @@ func (v Value) Uint(b64 bool) (x uint64, ok bool) {
return 0, false
}
// Float32 returns v as a float32 of the specified precision and reports whether
// Float returns v as a float64 of the specified precision and reports whether
// the conversion succeeded.
func (v Value) Float32() (x float32, ok bool) {
switch v.typ {
case Int:
return float32(int64(v.num)), true // possibly lossy, but allowed
case Uint:
return float32(uint64(v.num)), true // possibly lossy, but allowed
case Float32, Float64:
n := math.Float64frombits(v.num)
if math.IsNaN(n) || math.IsInf(n, 0) {
return float32(n), true
}
if math.Abs(n) <= math.MaxFloat32 {
return float32(n), true
}
}
return 0, false
}
// Float64 returns v as a float64 of the specified precision and reports whether
// the conversion succeeded.
func (v Value) Float64() (x float64, ok bool) {
func (v Value) Float(b64 bool) (x float64, ok bool) {
switch v.typ {
case Int:
return float64(int64(v.num)), true // possibly lossy, but allowed
case Uint:
return float64(uint64(v.num)), true // possibly lossy, but allowed
case Float32:
f, ok := v.Float32()
return float64(f), ok
case Float64:
case Float32, Float64:
n := math.Float64frombits(v.num)
return n, true
if math.IsNaN(n) || math.IsInf(n, 0) {
return float64(n), true
}
if b64 || math.Abs(n) <= math.MaxFloat32 {
return float64(n), true
}
}
return 0, false
}