Updating createMovieHandler accepting data.Runtime for the Runtime field. Updating internal/data/runtime.go with the json.Unmarshaler interface to transform our client's runtime string field '%d mins' into a Runtime type format.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 58s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 58s
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ErrInvalidRuntimeFormat : Define an error that our UnmarshalJSON() method can return if we're unable to parse or convert the JSON string successfully
|
||||
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")
|
||||
|
||||
// Runtime
|
||||
/*
|
||||
Declare a custom Runtime type, which has the underlying type int32
|
||||
@@ -28,3 +33,32 @@ func (r Runtime) MarshalJSON() ([]byte, error) {
|
||||
// Convert the quoted string value to a byte slice and return it.
|
||||
return []byte(quotedJSONValue), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON
|
||||
// Implement a UnmarshalJSON() method on the Runtime type so that it satisfies the json.Unmarshaler interface. IMPORTANT: Because UnmarshalJSON() needs to modify the receiver (our Runtime type), we must use a pointer receiver for this to work correctly. Otherwise, we will only be modifying a copy (which is then discarded when this method returns).
|
||||
func (r *Runtime) UnmarshalJSON(jsonValue []byte) error {
|
||||
// We expect that the incoming JSON value will be a string in the format "<runtimes> mins", and the first thing we need to do is remove the surrounding double-quotes from this string. If we can't unquote it, then we return the ErrInvalidRuntimeFormat error.
|
||||
unquotedJSONValue, err := strconv.Unquote(string(jsonValue))
|
||||
if err != nil {
|
||||
return ErrInvalidRuntimeFormat
|
||||
}
|
||||
|
||||
// Split the string to isolate the part containing the number.
|
||||
parts := strings.Split(unquotedJSONValue, " ")
|
||||
|
||||
// Sanity check the parts of the string to make sure it was in the expected format. If it isn't, we return the ErrInvalidRuntimeFormat error again.
|
||||
if len(parts) != 2 || parts[1] != "mins" {
|
||||
return ErrInvalidRuntimeFormat
|
||||
}
|
||||
|
||||
// Otherwise, parse the string containing the number into an int32. Again, if this fails, return the ErrInvalidRuntimeFormat error.
|
||||
i, err := strconv.ParseInt(parts[0], 10, 32)
|
||||
if err != nil {
|
||||
return ErrInvalidRuntimeFormat
|
||||
}
|
||||
|
||||
// Convert the int32 to a Runtime type and assign this to the receiver. Note that we use the * operator to deference the receiver (which is a pointer to a Runtime type) in order to set the underlying value of the pointer.
|
||||
*r = Runtime(i)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user