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

Can't figure out how to use Relationships #7

Closed
Cretezy opened this issue Jul 14, 2016 · 2 comments
Closed

Can't figure out how to use Relationships #7

Cretezy opened this issue Jul 14, 2016 · 2 comments

Comments

@Cretezy
Copy link

Cretezy commented Jul 14, 2016

Hey!

I've been working on this for a few days and can't really figure it out.

In general, my goal is to give gomusicbrainz a MBID and get returned the URL relationships (Wikipedia, official website, Spotify, etc..).

For instance, this is my best try:

    // Grab Musicbrainz artist
    musicbrainzArtist, err := musicbrainzClient.LookupArtist(
        gomusicbrainz.MBID(artist.MusicbrainzID), "url-rels",
    )
    if err != nil {
        log.Warn(err)
        continue
    }
    // List all url 
    for _, url := range musicbrainzArtist.Relations["url"] {
        fmt.Println(gomusicbrainz.URLRelation(url).Target) // error, don't know what to do here
    }

My question is: How do I convert a gomusicbrainz.Relation to gomusicbrainz.URLRelation?

@michiwend
Copy link
Owner

michiwend commented Aug 15, 2016

Hej and sorry for my late response.

The problem in your example is, you are trying to cast an interface type to a struct. gomusicbrainz.Relation is an interface type that only requires the TypeOf() method to be implemented on the actual type.

type Relation interface {
    TypeOf() string
}

What you want to do is a type assertion. Whenever you have an interface and want to access it's 'underlying' type, you have to assert the actual type.

Here is a small example that requests an artist and prints it's URL relations

package main

import (
    "fmt"
    "gomusicbrainz"
)

func main() {

    client, _ := gomusicbrainz.NewWS2Client(
        "https://musicbrainz.org/ws/2",
        "A GoMusicBrainz example",
        "0",
        "http://github.com/michiwend/gomusicbrainz")

    artist, err := client.LookupArtist(
        gomusicbrainz.MBID("fe1a873d-2000-4789-a895-4187fe756203"),
        "url-rels")

    if err != nil {
        fmt.Println(err)
        return
    }

    if urlRels, ok := artist.Relations["url"]; ok {
        for _, rel := range urlRels {

            // type assertion
            urlRelation, ok := rel.(*gomusicbrainz.URLRelation)
            if !ok {
                fmt.Println("should not have happend!")
                continue
            }

            fmt.Printf("%s: %s\n", urlRelation.Type, urlRelation.Target)
        }
    }
}

@Cretezy
Copy link
Author

Cretezy commented Aug 15, 2016

Thank you! A little late and figured out another solution, but thanks for enlightening me!

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

No branches or pull requests

2 participants