/
robot.go
128 lines (106 loc) · 2.42 KB
/
robot.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
package robot
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/alarmfox/game-repository/api"
"github.com/alarmfox/game-repository/model"
)
type Robot struct {
ID int64 `json:"id"`
TestClassId string `json:"testClassId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Difficulty string `json:"difficulty"`
Type RobotType `json:"type"`
Scores string `json:"scores"`
}
type RobotType int8
const (
randoop RobotType = iota
evosuite
)
func (rb RobotType) Parse(s string) (RobotType, error) {
switch strings.ToLower(s) {
case randoop.String():
return randoop, nil
case evosuite.String():
return evosuite, nil
default:
return RobotType(0), fmt.Errorf("%w: unsupported test engine",
api.ErrInvalidParam)
}
}
func (rb RobotType) String() string {
switch rb {
case randoop:
return "randoop"
case evosuite:
return "evosuite"
default:
panic("unreachable")
}
}
func (rb RobotType) MarshalJSON() ([]byte, error) {
return json.Marshal(rb.String())
}
func (rb *RobotType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
v, err := rb.Parse(s)
if err != nil {
return err
}
*rb = v
return nil
}
func (rb RobotType) AsInt8() int8 {
return int8(rb)
}
type CreateSingleRequest struct {
TestClassId string `json:"testClassId"`
Scores string `json:"scores"`
Difficulty string `json:"difficulty"`
Type RobotType `json:"type"`
}
func (r CreateSingleRequest) Validate() error {
return nil
}
type CreateRequest struct {
Robots []CreateSingleRequest `json:"robots"`
}
func (robots CreateRequest) Validate() error {
return nil
}
type UpdateRequest struct {
Scores string `json:"scores"`
Difficulty string `json:"difficulty"`
}
func (UpdateRequest) Validate() error {
return nil
}
type CustomString string
// CustomString is a dummy type that implements Convertable and Validable interfaces
func (CustomString) Parse(s string) (CustomString, error) {
return CustomString(s), nil
}
func (s CustomString) AsString() string {
return string(s)
}
func (CustomString) Validate() error {
return nil
}
func fromModel(r *model.Robot) *Robot {
return &Robot{
ID: r.ID,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
TestClassId: r.TestClassId,
Difficulty: r.Difficulty,
Type: RobotType(r.Type),
Scores: r.Scores,
}
}