package movies import ( "encoding/json" "fmt" "strconv" "strings" ) type Movie struct { Title string Director string Poster string Rating Rating OMDB OMDBMovie } type Rating struct { Note float64 Votes int64 } type OMDBMovie struct { Title string Year string Rated string Released string Runtime string Genre string Director string Writer string Actors string Plot string Language string Country string Awards string Poster string Ratings []OMDBRating Metascore string IMDBRating string `json: "imdbRating"` IMDBVotes string `json: "imdbVotes"` IMDBID string `json: "imdbID"` Type string DVD string BoxOffice string Production string Website string Response string } type OMDBRating struct { Source, Value string } func Unmarshal(b []byte) (*Movie, error) { var omdb OMDBMovie if err := json.Unmarshal(b, &omdb); err != nil { return nil, err } note, err := strconv.ParseFloat(omdb.IMDBRating, 64) if err != nil { return nil, fmt.Errorf("cannot convert %q to float64: %w", omdb.IMDBRating, err) } votes, err := strconv.ParseInt(strings.ReplaceAll(omdb.IMDBVotes, ",", ""), 10, 64) if err != nil { return nil, fmt.Errorf("cannot convert %q to int: %w", omdb.IMDBVotes, err) } return &Movie{ Title: omdb.Title, Director: omdb.Director, Poster: omdb.Poster, Rating: Rating{ Note: note, Votes: votes, }, OMDB: omdb, }, nil }