feat: go greenlight api wip

This commit is contained in:
2026-03-11 15:50:38 +01:00
parent 2ce943f94e
commit abf2db2798
19 changed files with 469 additions and 0 deletions
@@ -0,0 +1,15 @@
package data
import (
"time"
)
type Movie struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"-"` // Use the - directive
Title string `json:"title"`
Year int32 `json:"year,omitempty"` // Add the omitempty directive
Runtime Runtime `json:"runtime,omitempty"` // Add the omitempty directive
Genres []string `json:"genres,omitempty"` // Add the omitempty directive
Version int32 `json:"version"`
}
@@ -0,0 +1,25 @@
package data
import (
"fmt"
"strconv"
)
// Declare a custom Runtime type, which has the underlying type int32 (the same as our
// Movie struct field).
type Runtime int32
// 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 a string in the format "<runtime> mins").
func (r Runtime) MarshalJSON() ([]byte, error) {
// Generate a string containing the movie runtime in the required format.
jsonValue := fmt.Sprintf("%d mins", r)
// Use the strconv.Quote() function on the string to wrap it in double quotes. It
// needs to be surrounded by double quotes in order to be a valid *JSON string*.
quotedJSONValue := strconv.Quote(jsonValue)
// Convert the quoted string value to a byte slice and return it.
return []byte(quotedJSONValue), nil
}