diff --git a/internal/data/movies.go b/internal/data/movies.go index dff76af..06f6b11 100644 --- a/internal/data/movies.go +++ b/internal/data/movies.go @@ -12,7 +12,10 @@ type Movie struct { CreatedAt time.Time `json:"-"` Title string `json:"title"` Year int32 `json:"year,omitempty"` - Runtime int32 `json:"runtime,omitempty,string"` - Genres []string `json:"genres,omitempty"` - Version int32 `json:"version"` + /* + Use the Runtime type instead of int32. Note that the omitempty directive will still work on this: if the Runtime field has the underlying value 0, then it will be considered empty and omitted -- and the MarshalJSON() method we just made won't be called at all. + */ + Runtime Runtime `json:"runtime,omitempty,string"` + Genres []string `json:"genres,omitempty"` + Version int32 `json:"version"` } diff --git a/internal/data/runtime.go b/internal/data/runtime.go index 689a277..28494a1 100644 --- a/internal/data/runtime.go +++ b/internal/data/runtime.go @@ -14,10 +14,7 @@ type Runtime int32 // MarshalJSON /* -Implement a MarshalJSON() method on the Runtime type so that it satisfies -the json.Marshaler interface. This should return the JSON-encoded value -for the movie runtime (in our case, it will return string in the format -" mins"). +Implement a MarshalJSON() method on the Runtime type so that it satisfies the json.Marshaler interface. This should return the JSON-encoded value for the movie runtime (in our case, it will return string in the format " mins"). */ func (r Runtime) MarshalJSON() ([]byte, error) { // Generate a string containing the movie runtime in the required format.