zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.33k stars 283 forks source link

Illegal offset: should be position xor uri #219

Closed aditya-K2 closed 1 year ago

aditya-K2 commented 1 year ago

Hi, I have a wrapper over the client.PlayOpt function that takes a context and a uri

func PlaySongWithContextURI(context, uri *spotify.URI) error {
    return Client.PlayOpt(ctx(), &spotify.PlayOptions{
        PlaybackContext: context,
        PlaybackOffset:  &spotify.PlaybackOffset{URI: uri},
    })
}

This function upon providing a context and uri gives the following error

package main

import (
    "fmt"

    "github.com/zmb3/spotify/v2"
)

func main() {
    albumUri := spotify.URI("spotify:album:6E2VHYLPTtFOtdRcpt6YwP")
    trackUri := spotify.URI("spotify:track:4ME7UM3yALUZOxz5btBPc8")
    fmt.Println(PlaySongWithContextURI(&albumUri, &trackUri))
}
$ go run main.go 
Illegal offset: should be position XOR uri

The same request on spotify api dashboard runs perfectly fine

Apr12Wed065228AM

I think https://github.com/zmb3/spotify/commit/9d29507c9a13689c913905f76e69974e8b234072 introduced a bug where the position isn't omitted when empty.

bytes, err := json.Marshal(&PlaybackOptions{uri: "uri"})
fmt.Println(string(bytes))
// output: {"position":0,"uri":"uri"}

Upon changing back PlaybackOffSet.Position the error is resolved

type PlaybackOffset struct {
    // Position is zero based and can’t be negative.
    Position int `json:"position,omitempty"`
    // URI is a string representing the uri of the item to start at.
    URI URI `json:"uri,omitempty"`
}
$ go run main.go
<nil>            

https://github.com/zmb3/spotify/commit/9d29507c9a13689c913905f76e69974e8b234072 removes the omitempty tag because the Position was ignored when it became zero. Using an integer pointer would fix this problem. As in

type PlaybackOffset struct {
    // Position is zero based and can’t be negative.
    Position *int `json:"position,omitempty"`
    // URI is a string representing the uri of the item to start at.
    URI URI `json:"uri,omitempty"`
}
$ go run main.go
<nil>