forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clear_send_body.go
113 lines (98 loc) · 3.23 KB
/
clear_send_body.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package hit
import (
"github.com/Eun/go-hit/errortrace"
"github.com/Eun/go-hit/internal"
"golang.org/x/xerrors"
)
// IClearSendBody provides a clear functionality to remove previous steps from running in the Send().Body() scope
type IClearSendBody interface {
IStep
// JSON removes all previous Send().Body().JSON() steps.
//
// If you specify an argument it will only remove the Send().Body().JSON() steps matching that argument.
//
// Usage:
// Clear().Send().Body().JSON() // will remove all Send().Body().JSON() steps
// Clear().Send().Body().JSON(map[string]interface{}{"Name": "Joe"}) // will remove all Send().Body().JSON(map[string]interface{}{"Name": "Joe"}) steps
//
// Example:
// MustDo(
// Post("https://example.com"),
// Send().Body().JSON(map[string]interface{}{"Name": "Joe"}),
// Clear().Send().Body().JSON(),
// Send().Body().JSON(map[string]interface{}{"Name": "Alice"}),
// )
JSON(...interface{}) IStep
// Interface removes all previous Send().Body().Interface() steps.
//
// If you specify an argument it will only remove the Send().Body().Interface() steps matching that argument.
//
// Usage:
// Clear().Send().Body().Interface() // will remove all Send().Body().Interface() steps
// Clear().Send().Body().Interface("Hello World") // will remove all Send().Body().Interface("Hello World") steps
//
// Example:
// MustDo(
// Post("https://example.com"),
// Send().Body().Interface("Hello Earth"),
// Clear().Send().Body().Interface(),
// Send().Body().Interface("Hello World"),
// )
Interface(...interface{}) IStep
}
type clearSendBody struct {
cleanPath clearPath
trace *errortrace.ErrorTrace
}
func newClearSendBody(clearPath clearPath, params []interface{}) IClearSendBody {
if _, ok := internal.GetLastArgument(params); ok {
// this runs if we called Clear().Send().Body(something)
return &finalClearSendBody{
removeStep(clearPath),
"only usable with Clear().Send().Body() not with Clear().Send().Body(value)",
}
}
return &clearSendBody{
cleanPath: clearPath,
trace: ett.Prepare(),
}
}
func (*clearSendBody) when() StepTime {
return SendStep
}
func (body *clearSendBody) exec(hit Hit) error {
// this runs if we called Clear().Send().Body()
if err := removeSteps(hit, body.clearPath()); err != nil {
return body.trace.Format(hit.Description(), err.Error())
}
return nil
}
func (body *clearSendBody) clearPath() clearPath {
return body.cleanPath
}
func (body *clearSendBody) JSON(data ...interface{}) IStep {
return removeStep(body.clearPath().Push("JSON", data))
}
func (body *clearSendBody) Interface(data ...interface{}) IStep {
return removeStep(body.clearPath().Push("Interface", data))
}
type finalClearSendBody struct {
IStep
message string
}
func (body *finalClearSendBody) fail() IStep {
return &hitStep{
Trace: ett.Prepare(),
When: CleanStep,
ClearPath: nil,
Exec: func(hit Hit) error {
return xerrors.New(body.message)
},
}
}
func (body *finalClearSendBody) JSON(...interface{}) IStep {
return body.fail()
}
func (body *finalClearSendBody) Interface(...interface{}) IStep {
return body.fail()
}