Add many integer marshalled types.

This commit is contained in:
John Beisley 2013-10-27 21:01:36 +00:00
parent e86d73d8f4
commit f6c0a22a5f
2 changed files with 220 additions and 9 deletions

View File

@ -19,6 +19,68 @@ var (
localLoc = time.Local localLoc = time.Local
) )
func MarshalUi1(v uint8) (string, error) {
return strconv.FormatUint(uint64(v), 10), nil
}
func UnmarshalUi1(s string) (uint8, error) {
v, err := strconv.ParseUint(s, 10, 8)
return uint8(v), err
}
func MarshalUi2(v uint16) (string, error) {
return strconv.FormatUint(uint64(v), 10), nil
}
func UnmarshalUi2(s string) (uint16, error) {
v, err := strconv.ParseUint(s, 10, 16)
return uint16(v), err
}
func MarshalUi4(v uint32) (string, error) {
return strconv.FormatUint(uint64(v), 10), nil
}
func UnmarshalUi4(s string) (uint32, error) {
v, err := strconv.ParseUint(s, 10, 32)
return uint32(v), err
}
func MarshalI1(v int8) (string, error) {
return strconv.FormatInt(int64(v), 10), nil
}
func UnmarshalI1(s string) (int8, error) {
v, err := strconv.ParseInt(s, 10, 8)
return int8(v), err
}
func MarshalI2(v int16) (string, error) {
return strconv.FormatInt(int64(v), 10), nil
}
func UnmarshalI2(s string) (int16, error) {
v, err := strconv.ParseInt(s, 10, 16)
return int16(v), err
}
func MarshalI4(v int32) (string, error) {
return strconv.FormatInt(int64(v), 10), nil
}
func UnmarshalI4(s string) (int32, error) {
v, err := strconv.ParseInt(s, 10, 32)
return int32(v), err
}
func MarshalInt(v int64) (string, error) {
return strconv.FormatInt(v, 10), nil
}
func UnmarshalInt(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
// MarshalFixed14_4 marshals float64 to SOAP "fixed.14.4" type. // MarshalFixed14_4 marshals float64 to SOAP "fixed.14.4" type.
func MarshalFixed14_4(v float64) (string, error) { func MarshalFixed14_4(v float64) (string, error) {
if v >= 1e14 || v <= -1e14 { if v >= 1e14 || v <= -1e14 {

View File

@ -16,7 +16,7 @@ type convTest interface {
// duper is an interface that convTest values may optionally also implement to // duper is an interface that convTest values may optionally also implement to
// generate another convTest for a value in an otherwise identical testCase. // generate another convTest for a value in an otherwise identical testCase.
type duper interface { type duper interface {
Dupe(tag string) convTest Dupe(tag string) []convTest
} }
type testCase struct { type testCase struct {
@ -29,6 +29,108 @@ type testCase struct {
tag string tag string
} }
type Ui1Test uint8
func (v Ui1Test) Marshal() (string, error) {
return MarshalUi1(uint8(v))
}
func (v Ui1Test) Unmarshal(s string) (interface{}, error) {
return UnmarshalUi1(s)
}
func (v Ui1Test) Equal(result interface{}) bool {
return uint8(v) == result.(uint8)
}
func (v Ui1Test) Dupe(tag string) []convTest {
if tag == "dupe" {
return []convTest{
Ui2Test(v),
Ui4Test(v),
}
}
return nil
}
type Ui2Test uint16
func (v Ui2Test) Marshal() (string, error) {
return MarshalUi2(uint16(v))
}
func (v Ui2Test) Unmarshal(s string) (interface{}, error) {
return UnmarshalUi2(s)
}
func (v Ui2Test) Equal(result interface{}) bool {
return uint16(v) == result.(uint16)
}
type Ui4Test uint32
func (v Ui4Test) Marshal() (string, error) {
return MarshalUi4(uint32(v))
}
func (v Ui4Test) Unmarshal(s string) (interface{}, error) {
return UnmarshalUi4(s)
}
func (v Ui4Test) Equal(result interface{}) bool {
return uint32(v) == result.(uint32)
}
type I1Test int8
func (v I1Test) Marshal() (string, error) {
return MarshalI1(int8(v))
}
func (v I1Test) Unmarshal(s string) (interface{}, error) {
return UnmarshalI1(s)
}
func (v I1Test) Equal(result interface{}) bool {
return int8(v) == result.(int8)
}
func (v I1Test) Dupe(tag string) []convTest {
if tag == "dupe" {
return []convTest{
I2Test(v),
I4Test(v),
}
}
return nil
}
type I2Test int16
func (v I2Test) Marshal() (string, error) {
return MarshalI2(int16(v))
}
func (v I2Test) Unmarshal(s string) (interface{}, error) {
return UnmarshalI2(s)
}
func (v I2Test) Equal(result interface{}) bool {
return int16(v) == result.(int16)
}
type I4Test int32
func (v I4Test) Marshal() (string, error) {
return MarshalI4(int32(v))
}
func (v I4Test) Unmarshal(s string) (interface{}, error) {
return UnmarshalI4(s)
}
func (v I4Test) Equal(result interface{}) bool {
return int32(v) == result.(int32)
}
type IntTest int64
func (v IntTest) Marshal() (string, error) {
return MarshalInt(int64(v))
}
func (v IntTest) Unmarshal(s string) (interface{}, error) {
return UnmarshalInt(s)
}
func (v IntTest) Equal(result interface{}) bool {
return int64(v) == result.(int64)
}
type Fixed14_4Test float64 type Fixed14_4Test float64
func (v Fixed14_4Test) Marshal() (string, error) { func (v Fixed14_4Test) Marshal() (string, error) {
@ -64,9 +166,9 @@ func (v DateTest) Unmarshal(s string) (interface{}, error) {
func (v DateTest) Equal(result interface{}) bool { func (v DateTest) Equal(result interface{}) bool {
return v.Time.Equal(result.(time.Time)) return v.Time.Equal(result.(time.Time))
} }
func (v DateTest) Dupe(tag string) convTest { func (v DateTest) Dupe(tag string) []convTest {
if tag != "no:dateTime" { if tag != "no:dateTime" {
return DateTimeTest{v.Time} return []convTest{DateTimeTest{v.Time}}
} }
return nil return nil
} }
@ -84,9 +186,9 @@ func (v TimeOfDayTest) Unmarshal(s string) (interface{}, error) {
func (v TimeOfDayTest) Equal(result interface{}) bool { func (v TimeOfDayTest) Equal(result interface{}) bool {
return v.TimeOfDay == result.(TimeOfDay) return v.TimeOfDay == result.(TimeOfDay)
} }
func (v TimeOfDayTest) Dupe(tag string) convTest { func (v TimeOfDayTest) Dupe(tag string) []convTest {
if tag != "no:time.tz" { if tag != "no:time.tz" {
return TimeOfDayTzTest{v.TimeOfDay} return []convTest{TimeOfDayTzTest{v.TimeOfDay}}
} }
return nil return nil
} }
@ -116,9 +218,9 @@ func (v DateTimeTest) Unmarshal(s string) (interface{}, error) {
func (v DateTimeTest) Equal(result interface{}) bool { func (v DateTimeTest) Equal(result interface{}) bool {
return v.Time.Equal(result.(time.Time)) return v.Time.Equal(result.(time.Time))
} }
func (v DateTimeTest) Dupe(tag string) convTest { func (v DateTimeTest) Dupe(tag string) []convTest {
if tag != "no:dateTime.tz" { if tag != "no:dateTime.tz" {
return DateTimeTzTest{v.Time} return []convTest{DateTimeTzTest{v.Time}}
} }
return nil return nil
} }
@ -184,6 +286,53 @@ func Test(t *testing.T) {
}() }()
tests := []testCase{ tests := []testCase{
// ui1
{str: "", value: Ui1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: " ", value: Ui1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: "abc", value: Ui1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: "-1", value: Ui1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: "0", value: Ui1Test(0), tag: "dupe"},
{str: "1", value: Ui1Test(1), tag: "dupe"},
{str: "255", value: Ui1Test(255), tag: "dupe"},
{str: "256", value: Ui1Test(0), wantUnmarshalErr: true, noMarshal: true},
// ui2
{str: "65535", value: Ui2Test(65535)},
{str: "65536", value: Ui2Test(0), wantUnmarshalErr: true, noMarshal: true},
// ui4
{str: "4294967295", value: Ui4Test(4294967295)},
{str: "4294967296", value: Ui4Test(0), wantUnmarshalErr: true, noMarshal: true},
// i1
{str: "", value: I1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: " ", value: I1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: "abc", value: I1Test(0), wantUnmarshalErr: true, noMarshal: true, tag: "dupe"},
{str: "0", value: I1Test(0), tag: "dupe"},
{str: "-1", value: I1Test(-1), tag: "dupe"},
{str: "127", value: I1Test(127), tag: "dupe"},
{str: "-128", value: I1Test(-128), tag: "dupe"},
{str: "128", value: I1Test(0), wantUnmarshalErr: true, noMarshal: true},
{str: "-129", value: I1Test(0), wantUnmarshalErr: true, noMarshal: true},
// i2
{str: "32767", value: I2Test(32767)},
{str: "-32768", value: I2Test(-32768)},
{str: "32768", value: I2Test(0), wantUnmarshalErr: true, noMarshal: true},
{str: "-32769", value: I2Test(0), wantUnmarshalErr: true, noMarshal: true},
// i4
{str: "2147483647", value: I4Test(2147483647)},
{str: "-2147483648", value: I4Test(-2147483648)},
{str: "2147483648", value: I4Test(0), wantUnmarshalErr: true, noMarshal: true},
{str: "-2147483649", value: I4Test(0), wantUnmarshalErr: true, noMarshal: true},
// int
{str: "9223372036854775807", value: IntTest(9223372036854775807)},
{str: "-9223372036854775808", value: IntTest(-9223372036854775808)},
{str: "9223372036854775808", value: IntTest(0), wantUnmarshalErr: true, noMarshal: true},
{str: "-9223372036854775809", value: IntTest(0), wantUnmarshalErr: true, noMarshal: true},
// fixed.14.4 // fixed.14.4
{str: "0.0000", value: Fixed14_4Test(0)}, {str: "0.0000", value: Fixed14_4Test(0)},
{str: "1.0000", value: Fixed14_4Test(1)}, {str: "1.0000", value: Fixed14_4Test(1)},
@ -300,8 +449,8 @@ func Test(t *testing.T) {
var extras []testCase var extras []testCase
for i := range tests { for i := range tests {
if duper, ok := tests[i].value.(duper); ok { if duper, ok := tests[i].value.(duper); ok {
duped := duper.Dupe(tests[i].tag) dupes := duper.Dupe(tests[i].tag)
if duped != nil { for _, duped := range dupes {
dupedCase := testCase(tests[i]) dupedCase := testCase(tests[i])
dupedCase.value = duped dupedCase.value = duped
extras = append(extras, dupedCase) extras = append(extras, dupedCase)