mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
remove IntBody and FloatBody from consumer message
This commit is contained in:
parent
2f18234308
commit
85790d319c
@ -80,19 +80,3 @@ func (m *ConsumerMessage) BytesBody() ([]byte, error) {
|
|||||||
|
|
||||||
return nil, fmt.Errorf("message body is not a byte array: %v", m.Body)
|
return nil, fmt.Errorf("message body is not a byte array: %v", m.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ConsumerMessage) IntBody() (int, error) {
|
|
||||||
if m.Body.Type() == js.TypeNumber {
|
|
||||||
return m.Body.Int(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, fmt.Errorf("message body is not a number: %v", m.Body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *ConsumerMessage) FloatBody() (float64, error) {
|
|
||||||
if m.Body.Type() == js.TypeNumber {
|
|
||||||
return m.Body.Float(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, fmt.Errorf("message body is not a number: %v", m.Body)
|
|
||||||
}
|
|
||||||
|
@ -225,97 +225,3 @@ func TestConsumerMessage_BytesBody(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConsumerMessage_IntBody(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
body js.Value
|
|
||||||
want int
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "int",
|
|
||||||
body: js.ValueOf(42),
|
|
||||||
want: 42,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "float",
|
|
||||||
body: js.ValueOf(42.5),
|
|
||||||
want: 42,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "string",
|
|
||||||
body: js.ValueOf("42"),
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "undefined",
|
|
||||||
body: js.Undefined(),
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
m := &ConsumerMessage{
|
|
||||||
Body: tt.body,
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := m.IntBody()
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Fatalf("IntBody() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if got != tt.want {
|
|
||||||
t.Fatalf("IntBody() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConsumerMessage_FloatBody(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
body js.Value
|
|
||||||
want float64
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "int",
|
|
||||||
body: js.ValueOf(42),
|
|
||||||
want: 42.0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "float",
|
|
||||||
body: js.ValueOf(42.5),
|
|
||||||
want: 42.5,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "string",
|
|
||||||
body: js.ValueOf("42"),
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "undefined",
|
|
||||||
body: js.Undefined(),
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
m := &ConsumerMessage{
|
|
||||||
Body: tt.body,
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := m.FloatBody()
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Fatalf("FloatBody() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
if got != tt.want {
|
|
||||||
t.Fatalf("FloatBody() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user