You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.5 KiB
Go

package data
import (
"errors"
"fmt"
"strconv"
"strings"
)
// 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")
// Declare a custom Runtime type, which has the underlying int32 type
type Runtime int32
// The rule about pointers vs. values for receivers is that value methods
// can be invoked on pointers and values, but pointer methods can only
// be invoked on pointers.
// Implements json.Marshaler interface.
func (r Runtime) MarshalJSON() ([]byte, error) {
// Generate a string containing the movie runtime
jsonValue := fmt.Sprintf("%d mins", r)
//Use strconv.Quote() function on the string to wrap it in double quotes
// necessary to be valid json
quotedJSONValue := strconv.Quote(jsonValue)
// convert the string to byte slice and return it
return []byte(quotedJSONValue), nil
}
// 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
// "<runtime> 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
}