-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreader_utils.go
238 lines (227 loc) · 7.95 KB
/
reader_utils.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package boardgame
import (
"github.com/jkomoros/boardgame/errors"
"strconv"
)
//stacksForReader returns all stacks in reader, inclduing all StackProps, and all stacks within Boards.
func stacksForReader(reader PropertyReader) []ImmutableStack {
var result []ImmutableStack
for propName, propType := range reader.Props() {
if propType == TypeStack {
stack, err := reader.ImmutableStackProp(propName)
if err != nil {
continue
}
result = append(result, stack)
} else if propType == TypeBoard {
board, err := reader.ImmutableBoardProp(propName)
if err != nil {
continue
}
for _, stack := range board.ImmutableSpaces() {
result = append(result, stack)
}
}
}
return result
}
func setReaderStatePtr(reader PropertyReader, st ImmutableState) error {
statePtr, ok := st.(*state)
if !ok {
return errors.New("The provided non-nil State could not be conveted to a state ptr")
}
for propName, propType := range reader.Props() {
switch propType {
case TypeStack:
val, err := reader.ImmutableStackProp(propName)
if val == nil {
return errors.New("Stack Prop " + propName + " was nil")
}
if err != nil {
return errors.New("Stack prop " + propName + " had unexpected error: " + err.Error())
}
val.setState(statePtr)
case TypeBoard:
val, err := reader.ImmutableBoardProp(propName)
if val == nil {
return errors.New("Board Prop " + propName + " was nil")
}
if err != nil {
return errors.New("Board prop " + propName + " had unexpected error: " + err.Error())
}
val.setState(statePtr)
case TypeTimer:
val, err := reader.ImmutableTimerProp(propName)
if val == nil {
return errors.New("TimerProp " + propName + " was nil")
}
if err != nil {
return errors.New("TimerProp " + propName + " had unexpected error: " + err.Error())
}
val.setState(statePtr)
}
}
return nil
}
//copyReader assumes input and output container are the same "shape" (that is,
//outputContainer can have all of input's properties set). It also assumes
//that the output has all interface types initalized to the right shape.
func copyReader(input PropertyReadSetter, outputContainer PropertyReadSetter) error {
for propName, propType := range input.Props() {
switch propType {
case TypeBool:
boolVal, err := input.BoolProp(propName)
if err != nil {
return errors.New(propName + " did not return a bool as expected: " + err.Error())
}
err = outputContainer.SetBoolProp(propName, boolVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypeInt:
intVal, err := input.IntProp(propName)
if err != nil {
return errors.New(propName + " did not return an int as expected: " + err.Error())
}
err = outputContainer.SetIntProp(propName, intVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypeString:
stringVal, err := input.StringProp(propName)
if err != nil {
return errors.New(propName + " did not return a string as expected: " + err.Error())
}
err = outputContainer.SetStringProp(propName, stringVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypePlayerIndex:
playerIndexVal, err := input.PlayerIndexProp(propName)
if err != nil {
return errors.New(propName + " did not return a player index as expected: " + err.Error())
}
err = outputContainer.SetPlayerIndexProp(propName, playerIndexVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypeIntSlice:
intSliceVal, err := input.IntSliceProp(propName)
if err != nil {
return errors.New(propName + " did not return an int slice as expected: " + err.Error())
}
err = outputContainer.SetIntSliceProp(propName, intSliceVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypeBoolSlice:
boolSliceVal, err := input.BoolSliceProp(propName)
if err != nil {
return errors.New(propName + " did not return an bool slice as expected: " + err.Error())
}
err = outputContainer.SetBoolSliceProp(propName, boolSliceVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypeStringSlice:
stringSliceVal, err := input.StringSliceProp(propName)
if err != nil {
return errors.New(propName + " did not return an string slice as expected: " + err.Error())
}
err = outputContainer.SetStringSliceProp(propName, stringSliceVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypePlayerIndexSlice:
playerIndexSliceVal, err := input.PlayerIndexSliceProp(propName)
if err != nil {
return errors.New(propName + " did not return a player index slice as expected: " + err.Error())
}
err = outputContainer.SetPlayerIndexSliceProp(propName, playerIndexSliceVal)
if err != nil {
return errors.New(propName + " could not be set on output: " + err.Error())
}
case TypeEnum:
enumConst, err := input.EnumProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " did not return an EnumVal as expected: " + err.Error())
}
outputEnum, err := outputContainer.EnumProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " could not get mutable enum on output: " + err.Error())
}
outputEnum.SetValue(enumConst.Value())
case TypeStack:
stackVal, err := input.StackProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " did not return a stack as expected: " + err.Error())
}
outputStack, err := outputContainer.StackProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " could not get mutable stack on output: " + err.Error())
}
if err := outputStack.importFrom(stackVal); err != nil {
return errors.New(propName + " could not import from input: " + err.Error())
}
case TypeBoard:
boardVal, err := input.BoardProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " did not return a board as expected: " + err.Error())
}
outputBoard, err := outputContainer.BoardProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " could not get mutable board on output: " + err.Error())
}
if err := outputBoard.importFrom(boardVal); err != nil {
return errors.New(propName + " could not import from input: " + err.Error())
}
case TypeTimer:
timerVal, err := input.TimerProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " did not return a timer as expected: " + err.Error())
}
outputTimer, err := outputContainer.TimerProp(propName)
if err != nil {
//if the err is ErrPropertyImmutable, that's OK, just skip
if err == ErrPropertyImmutable {
continue
}
return errors.New(propName + " could not get mutable timer on output: " + err.Error())
}
if err := outputTimer.importFrom(timerVal); err != nil {
return errors.New(propName + " could not import from input: " + err.Error())
}
default:
return errors.New(propName + " was an unsupported property type: " + strconv.Itoa(int(propType)))
}
}
return nil
}