-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.go
More file actions
37 lines (30 loc) · 760 Bytes
/
Copy pathruntime.go
File metadata and controls
37 lines (30 loc) · 760 Bytes
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
package data
import (
"errors"
"fmt"
"strconv"
"strings"
)
type Runtime int32
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")
func (r Runtime) MarshalJSON() ([]byte, error) {
jsonValue := fmt.Sprintf("%d mins", r)
quotedJSONValue := strconv.Quote(jsonValue)
return []byte(quotedJSONValue), nil
}
func (r *Runtime) UnmarshalJSON(jsonValue []byte) error {
unquotedJSONValue, err := strconv.Unquote(string(jsonValue))
if err != nil {
return ErrInvalidRuntimeFormat
}
parts := strings.Split(unquotedJSONValue, " ")
if len(parts) != 2 || parts[1] != "mins" {
return ErrInvalidRuntimeFormat
}
i, err := strconv.ParseInt(parts[0], 10, 32)
if err != nil {
return ErrInvalidRuntimeFormat
}
*r = Runtime(i)
return nil
}