-
Notifications
You must be signed in to change notification settings - Fork 1
/
entities.go
36 lines (29 loc) · 879 Bytes
/
entities.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
package calc
import "fmt"
// Request describes service request data structure.
type Request struct {
Operation string `json:"operation"`
Arguments []float64 `json:"arguments"`
}
// Validate implements request validation.
func (r *Request) Validate() error {
operation, ok := Operations[r.Operation]
if !ok {
return fmt.Errorf("unsupported operation '%s'", r.Operation)
}
return operation.validation(r.Arguments)
}
// Response describes service response data structure.
type Response struct {
Operation string `json:"operation"`
Arguments []float64 `json:"arguments"`
Result float64 `json:"result"`
}
// ErrorResponse describes service error response data structure.
type ErrorResponse struct {
Message string `json:"message"`
}
// NewErrorResponse ...
func NewErrorResponse(err error) *ErrorResponse {
return &ErrorResponse{Message: err.Error()}
}