forked from zoncoen/scenarigo
/
grpc.go
64 lines (53 loc) · 1.33 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package grpc
import (
"bytes"
"errors"
"github.com/goccy/go-yaml"
"github.com/bilus/scenarigo/protocol"
)
// Register registers grpc protocol.
func Register() {
protocol.Register(&GRPC{})
}
// GRPC is a protocol type for the scenarigo step.
type GRPC struct{}
// Name implements protocol.Protocol interface.
func (p *GRPC) Name() string {
return "grpc"
}
// UnmarshalRequest implements protocol.Protocol interface.
func (p *GRPC) UnmarshalRequest(b []byte) (protocol.Invoker, error) {
var r Request
if err := yaml.UnmarshalWithOptions(b, &r, yaml.Strict()); err != nil {
return nil, err
}
// for backward compatibility
if r.Body != nil {
if r.Message != nil {
return nil, errors.New("body is deprecated, use message field only")
}
r.Message = r.Body
r.Body = nil
}
return &r, nil
}
// UnmarshalExpect implements protocol.Protocol interface.
func (p *GRPC) UnmarshalExpect(b []byte) (protocol.AssertionBuilder, error) {
var e Expect
if b == nil {
return &e, nil
}
decoder := yaml.NewDecoder(bytes.NewBuffer(b), yaml.UseOrderedMap(), yaml.Strict())
if err := decoder.Decode(&e); err != nil {
return nil, err
}
// for backward compatibility
if e.Body != nil {
if e.Message != nil {
return nil, errors.New("body is deprecated, use message field only")
}
e.Message = e.Body
e.Body = nil
}
return &e, nil
}