Add marshalling for the "boolean" type.

This commit is contained in:
John Beisley 2013-10-27 20:04:48 +00:00
parent ecf830777a
commit aba95ad90c
2 changed files with 43 additions and 0 deletions

View File

@ -377,3 +377,22 @@ func UnmarshalDateTimeTz(s string) (result time.Time, err error) {
result = time.Date(year, time.Month(month), day, hour, minute, second, 0, location) result = time.Date(year, time.Month(month), day, hour, minute, second, 0, location)
return return
} }
// MarshalBoolean marshals bool to SOAP "boolean" type.
func MarshalBoolean(v bool) (string, error) {
if v {
return "1", nil
}
return "0", nil
}
// UnmarshalBoolean unmarshals bool from the SOAP "boolean" type.
func UnmarshalBoolean(s string) (bool, error) {
switch s {
case "0", "false", "no":
return false, nil
case "1", "true", "yes":
return true, nil
}
return false, fmt.Errorf("soap boolean: %q is not a valid boolean value", s)
}

View File

@ -134,6 +134,18 @@ func (v DateTimeTzTest) Equal(result interface{}) bool {
return v.Time.Equal(result.(time.Time)) return v.Time.Equal(result.(time.Time))
} }
type BooleanTest bool
func (v BooleanTest) Marshal() (string, error) {
return MarshalBoolean(bool(v))
}
func (v BooleanTest) Unmarshal(s string) (interface{}, error) {
return UnmarshalBoolean(s)
}
func (v BooleanTest) Equal(result interface{}) bool {
return bool(v) == result.(bool)
}
func Test(t *testing.T) { func Test(t *testing.T) {
const time010203 time.Duration = (1*3600 + 2*60 + 3) * time.Second const time010203 time.Duration = (1*3600 + 2*60 + 3) * time.Second
const time0102 time.Duration = (1*3600 + 2*60) * time.Second const time0102 time.Duration = (1*3600 + 2*60) * time.Second
@ -233,6 +245,18 @@ func Test(t *testing.T) {
{str: "2013-10-08T10:30:50-01", value: DateTimeTzTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:00", -3600))}, noMarshal: true}, {str: "2013-10-08T10:30:50-01", value: DateTimeTzTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:00", -3600))}, noMarshal: true},
{str: "2013-10-08T10:30:50-01:23", value: DateTimeTzTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:23", -(3600+23*60)))}}, {str: "2013-10-08T10:30:50-01:23", value: DateTimeTzTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:23", -(3600+23*60)))}},
{str: "2013-10-08T10:30:50-0123", value: DateTimeTzTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:23", -(3600+23*60)))}, noMarshal: true}, {str: "2013-10-08T10:30:50-0123", value: DateTimeTzTest{time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:23", -(3600+23*60)))}, noMarshal: true},
// boolean
{str: "0", value: BooleanTest(false)},
{str: "1", value: BooleanTest(true)},
{str: "false", value: BooleanTest(false), noMarshal: true},
{str: "true", value: BooleanTest(true), noMarshal: true},
{str: "no", value: BooleanTest(false), noMarshal: true},
{str: "yes", value: BooleanTest(true), noMarshal: true},
{str: "", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true},
{str: "other", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true},
{str: "2", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true},
{str: "-1", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true},
} }
// Generate extra test cases from convTests that implement duper. // Generate extra test cases from convTests that implement duper.