/
states.go
158 lines (139 loc) · 3.65 KB
/
states.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dto
import (
"fmt"
"net/url"
"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/process"
"flamingo.me/flamingo-commerce/v3/checkout/domain/placeorder/states"
)
type (
// StateProvider returns a state instance
StateProvider func() map[string]State
// State representation for graphql
State interface {
MapFrom(process.Context)
}
// StateMapper to create dto states from context states
StateMapper struct {
stateProvider StateProvider
}
// Failed state
Failed struct {
Name string
Reason process.FailedReason
}
// Success state
Success struct {
Name string
}
// Wait state
Wait struct {
Name string
}
// WaitForCustomer state
WaitForCustomer struct {
Name string
}
// ShowIframe state
ShowIframe struct {
Name string
URL string
}
// ShowHTML state
ShowHTML struct {
Name string
HTML string
}
// Redirect state
Redirect struct {
Name string
URL string
}
// PostRedirect state
PostRedirect struct {
Name string
URL string
Parameters []FormParameter
}
// FormParameter holds redirect related form data
FormParameter struct {
Key string
Value []string
}
)
var (
_ State = new(Failed)
_ State = new(Success)
_ State = new(Wait)
_ State = new(WaitForCustomer)
_ State = new(ShowIframe)
_ State = new(ShowHTML)
_ State = new(Redirect)
_ State = new(PostRedirect)
)
// MapFrom the internal process state to the graphQL state fields
func (s *Failed) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
s.Reason = pctx.FailedReason
}
// MapFrom the internal process state to the graphQL state fields
func (s *Success) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
}
// MapFrom the internal process state to the graphQL state fields
func (s *Wait) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
}
// MapFrom the internal process state to the graphQL state fields
func (s *WaitForCustomer) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
}
// MapFrom the internal process state to the graphQL state fields
func (s *ShowIframe) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
if stateData, ok := pctx.CurrentStateData.(*url.URL); ok {
s.URL = stateData.String()
}
}
// MapFrom the internal process state to the graphQL state fields
func (s *ShowHTML) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
if stateData, ok := pctx.CurrentStateData.(string); ok {
s.HTML = stateData
}
}
// MapFrom the internal process state to the graphQL state fields
func (s *Redirect) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
if stateData, ok := pctx.CurrentStateData.(*url.URL); ok {
s.URL = stateData.String()
}
}
// MapFrom the internal process state to the graphQL state fields
func (s *PostRedirect) MapFrom(pctx process.Context) {
s.Name = pctx.CurrentStateName
if stateData, ok := pctx.CurrentStateData.(states.PostRedirectData); ok {
s.URL = stateData.URL.String()
parameters := make([]FormParameter, 0, len(stateData.FormFields))
for key, p := range stateData.FormFields {
parameters = append(parameters, FormParameter{
Key: key,
Value: p.Value,
})
}
s.Parameters = parameters
}
}
// Inject dependencies
func (sm *StateMapper) Inject(stateProvider StateProvider) *StateMapper {
sm.stateProvider = stateProvider
return sm
}
// Map a context into a state
func (sm *StateMapper) Map(pctx process.Context) (State, error) {
resultState, found := sm.stateProvider()[pctx.CurrentStateName]
if !found {
return nil, fmt.Errorf("couldn't map the internal process state %q to a GraphQL state", pctx.CurrentStateName)
}
resultState.MapFrom(pctx)
return resultState, nil
}