Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unmarshal numeric fields regardless of them being int or float #251

Merged
merged 4 commits into from
Mar 18, 2024

Conversation

wernerdweight
Copy link
Contributor

This fixes #243. I also updated the test data so that it contains float values.

@kreudom
Copy link

kreudom commented Mar 2, 2024

This does not work for me for the FullTrack. The fields stay zero.

I think the problem is here:

spotify/track.go

Lines 141 to 148 in f5f7cf5

var v struct {
SimpleTrack
Popularity float64 `json:"popularity"`
IsPlayable *bool `json:"is_playable"`
LinkedFrom *LinkedFromInfo `json:"linked_from"`
}
if err := json.Unmarshal(data, &v); err != nil {

Because SimpleTrack implements UnmarshalJSON, the struct v also implements it. Therefore json.Unmarshal directly calls UnmarshalJSON and ignores the other fields.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we duplicate the test data, rather than modifying the existing tests, to ensure we have a mixture of cases with floats and with integers, it looks like Spotify can't make their mind up and it'd be good to know we can handle both!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

spotify.go Outdated
return err
}

i.Height = int(v.Height)
Copy link
Collaborator

@strideynet strideynet Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if longterm it makes sense for us to return these fields as floats rather than casting them back to ints. It'd be a breaking change though so perhaps worth bundling with some other fixes. I'm also a little curious if it'd make more sense to have a custom type to avoid all these unmarshallers which could be easily forgotten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept it int for now, but removed the unmarshallers in favour of a custom type (more below)

@strideynet
Copy link
Collaborator

Thanks for raising this - could you address my remark re: the test data ? I think it's important to prove we can handle both ints and floats. Then I'm happy to move forward with this.

@wernerdweight
Copy link
Contributor Author

wernerdweight commented Mar 8, 2024

@strideynet, I rewrote it with the custom type to avoid multiple unmarshallers. It's much simpler and flexible now. That said, it introduces a breaking change for comparisons. So if you, for some reason, compare one of the fields (e.g. the follower count) to an integer variable, it will break, as the custom type is not comparable to the int.

playlist.Followers > 100 // still works
threshold := 100
playlist.Followers > threshold // doesn't work (mismatched types Numeric and int)

I still think this is a better solution than chaining the unmarshallers on top of each other, but since this is a bc-break, we/you could consider switching to floats (which would actually be a small change now that there's a custom type). Thanks

EDIT: it's a bc-break invoked by a bc-break by Spotify, so maybe not a big deal (bc-break vs. doesn't work).

@strideynet
Copy link
Collaborator

strideynet commented Mar 18, 2024

I still think this is a better solution than chaining the unmarshallers on top of each other, but since this is a bc-break, we/you could consider switching to floats (which would actually be a small change now that there's a custom type).

Thanks - I think I'm happy enough with this hack for now. I'll cut a release with a breaking change warning.

@strideynet strideynet merged commit ce25abf into zmb3:master Mar 18, 2024
2 checks passed
jaredallard referenced this pull request in jaredallard/miku Apr 19, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/zmb3/spotify/v2](https://togithub.com/zmb3/spotify) |
`v2.4.1` -> `v2.4.2` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fzmb3%2fspotify%2fv2/v2.4.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fzmb3%2fspotify%2fv2/v2.4.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fzmb3%2fspotify%2fv2/v2.4.1/v2.4.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fzmb3%2fspotify%2fv2/v2.4.1/v2.4.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>zmb3/spotify (github.com/zmb3/spotify/v2)</summary>

### [`v2.4.2`](https://togithub.com/zmb3/spotify/releases/tag/v2.4.2)

[Compare
Source](https://togithub.com/zmb3/spotify/compare/v2.4.1...v2.4.2)

#### What's Changed

- Update the link to information on rate limits by
[@&#8203;oifj34f34f](https://togithub.com/oifj34f34f) in
[https://github.com/zmb3/spotify/pull/245](https://togithub.com/zmb3/spotify/pull/245)
- Enable race detection in CI testing by
[@&#8203;strideynet](https://togithub.com/strideynet) in
[https://github.com/zmb3/spotify/pull/252](https://togithub.com/zmb3/spotify/pull/252)
- Update CI test steps by
[@&#8203;strideynet](https://togithub.com/strideynet) in
[https://github.com/zmb3/spotify/pull/253](https://togithub.com/zmb3/spotify/pull/253)
- Update Lint CI action by
[@&#8203;strideynet](https://togithub.com/strideynet) in
[https://github.com/zmb3/spotify/pull/254](https://togithub.com/zmb3/spotify/pull/254)
- build(deps): bump google.golang.org/protobuf from 1.27.1 to 1.33.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/zmb3/spotify/pull/255](https://togithub.com/zmb3/spotify/pull/255)
- fix: unmarshal numeric fields regardless of them being int or float by
[@&#8203;wernerdweight](https://togithub.com/wernerdweight) in
[https://github.com/zmb3/spotify/pull/251](https://togithub.com/zmb3/spotify/pull/251)
- build(deps): bump golang.org/x/net from 0.17.0 to 0.23.0 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/zmb3/spotify/pull/259](https://togithub.com/zmb3/spotify/pull/259)

#### New Contributors

- [@&#8203;oifj34f34f](https://togithub.com/oifj34f34f) made their first
contribution in
[https://github.com/zmb3/spotify/pull/245](https://togithub.com/zmb3/spotify/pull/245)
- [@&#8203;wernerdweight](https://togithub.com/wernerdweight) made their
first contribution in
[https://github.com/zmb3/spotify/pull/251](https://togithub.com/zmb3/spotify/pull/251)

**Full Changelog**:
zmb3/spotify@v2.4.1...v2.4.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/jaredallard/miku).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMDEuNCIsInVwZGF0ZWRJblZlciI6IjM3LjMwMS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unmarshal fail. 【curl --request GET \ --url https://api.spotify.com/v1/artists/0TnOYISbd1XYRBk9myaseg】
3 participants