From 040ace30adfb74174df29b327b089b293acce375 Mon Sep 17 00:00:00 2001 From: Maxime Delporte Date: Mon, 27 Oct 2025 20:03:52 +0100 Subject: [PATCH] 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. --- cmd/api/movies.go | 8 ++++---- internal/data/runtime.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cmd/api/movies.go b/cmd/api/movies.go index ed11db6..1c31d78 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -11,10 +11,10 @@ import ( func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { // Declare an anonymous struct to hold the information that we expect to be in the HTTP request body (note that the field names and types in the struct are a subset of the Movie struct that we created earlier). This struct will be our *target decode destination*. var input struct { - Title string `json:"title"` - Year int32 `json:"year"` - Runtime int32 `json:"runtime"` - Genres []string `json:"genres"` + Title string `json:"title"` + Year int32 `json:"year"` + Runtime data.Runtime `json:"runtime"` + Genres []string `json:"genres"` } // Use the new readJSON() helper to decode the request body into the input struct. If this returns an error, we send the client the error message along with a 400 Bad Request status code, just like before. diff --git a/internal/data/runtime.go b/internal/data/runtime.go index 28494a1..9090862 100644 --- a/internal/data/runtime.go +++ b/internal/data/runtime.go @@ -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 " 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 +}