mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
xml: treat bool as value in Unmarshal
R=rsc CC=golang-dev https://golang.org/cl/218050
This commit is contained in:
parent
766c3ff735
commit
1a37656b7a
2 changed files with 19 additions and 9 deletions
|
|
@ -105,7 +105,8 @@ import (
|
||||||
// Unmarshal maps an XML element to a slice by extending the length
|
// Unmarshal maps an XML element to a slice by extending the length
|
||||||
// of the slice and mapping the element to the newly created value.
|
// of the slice and mapping the element to the newly created value.
|
||||||
//
|
//
|
||||||
// Unmarshal maps an XML element to a bool by setting the bool to true.
|
// Unmarshal maps an XML element to a bool by setting it true if the
|
||||||
|
// string value is "true" or "1", or false otherwise.
|
||||||
//
|
//
|
||||||
// Unmarshal maps an XML element to an integer or floating-point
|
// Unmarshal maps an XML element to an integer or floating-point
|
||||||
// field by setting the field to the result of interpreting the string
|
// field by setting the field to the result of interpreting the string
|
||||||
|
|
@ -208,9 +209,6 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
|
||||||
default:
|
default:
|
||||||
return os.ErrorString("unknown type " + v.Type().String())
|
return os.ErrorString("unknown type " + v.Type().String())
|
||||||
|
|
||||||
case *reflect.BoolValue:
|
|
||||||
v.Set(true)
|
|
||||||
|
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
typ := v.Type().(*reflect.SliceType)
|
typ := v.Type().(*reflect.SliceType)
|
||||||
if _, ok := typ.Elem().(*reflect.Uint8Type); ok {
|
if _, ok := typ.Elem().(*reflect.Uint8Type); ok {
|
||||||
|
|
@ -244,7 +242,7 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
|
||||||
*reflect.IntValue, *reflect.UintValue, *reflect.UintptrValue,
|
*reflect.IntValue, *reflect.UintValue, *reflect.UintptrValue,
|
||||||
*reflect.Int8Value, *reflect.Int16Value, *reflect.Int32Value, *reflect.Int64Value,
|
*reflect.Int8Value, *reflect.Int16Value, *reflect.Int32Value, *reflect.Int64Value,
|
||||||
*reflect.Uint8Value, *reflect.Uint16Value, *reflect.Uint32Value, *reflect.Uint64Value,
|
*reflect.Uint8Value, *reflect.Uint16Value, *reflect.Uint32Value, *reflect.Uint64Value,
|
||||||
*reflect.FloatValue, *reflect.Float32Value, *reflect.Float64Value:
|
*reflect.FloatValue, *reflect.Float32Value, *reflect.Float64Value, *reflect.BoolValue:
|
||||||
saveData = v
|
saveData = v
|
||||||
|
|
||||||
case *reflect.StructValue:
|
case *reflect.StructValue:
|
||||||
|
|
@ -474,6 +472,9 @@ Loop:
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.Set(ftmp)
|
t.Set(ftmp)
|
||||||
|
case *reflect.BoolValue:
|
||||||
|
btmp := strings.TrimSpace(string(data))
|
||||||
|
t.Set(strings.ToLower(btmp) == "true" || btmp == "1")
|
||||||
case *reflect.StringValue:
|
case *reflect.StringValue:
|
||||||
t.Set(string(data))
|
t.Set(string(data))
|
||||||
case *reflect.SliceValue:
|
case *reflect.SliceValue:
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,10 @@ func TestSyntax(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type allScalars struct {
|
type allScalars struct {
|
||||||
Bool bool
|
True1 bool
|
||||||
|
True2 bool
|
||||||
|
False1 bool
|
||||||
|
False2 bool
|
||||||
Int int
|
Int int
|
||||||
Int8 int8
|
Int8 int8
|
||||||
Int16 int16
|
Int16 int16
|
||||||
|
|
@ -234,7 +237,10 @@ type allScalars struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var all = allScalars{
|
var all = allScalars{
|
||||||
Bool: true,
|
True1: true,
|
||||||
|
True2: true,
|
||||||
|
False1: false,
|
||||||
|
False2: false,
|
||||||
Int: 1,
|
Int: 1,
|
||||||
Int8: -2,
|
Int8: -2,
|
||||||
Int16: 3,
|
Int16: 3,
|
||||||
|
|
@ -253,7 +259,10 @@ var all = allScalars{
|
||||||
}
|
}
|
||||||
|
|
||||||
const testScalarsInput = `<allscalars>
|
const testScalarsInput = `<allscalars>
|
||||||
<bool/>
|
<true1>true</true1>
|
||||||
|
<true2>1</true2>
|
||||||
|
<false1>false</false1>
|
||||||
|
<false2>0</false2>
|
||||||
<int>1</int>
|
<int>1</int>
|
||||||
<int8>-2</int8>
|
<int8>-2</int8>
|
||||||
<int16>3</int16>
|
<int16>3</int16>
|
||||||
|
|
@ -280,7 +289,7 @@ func TestAllScalars(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(a, all) {
|
if !reflect.DeepEqual(a, all) {
|
||||||
t.Errorf("expected %+v got %+v", a, all)
|
t.Errorf("expected %+v got %+v", all, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue