Search code examples
gostructinterfacedryembedding

Wrapping multiple implementations in Go


I have an application that has multiple concurrent implementations of the same API (e.g. one backed by a SQL database and another by a dataset stored in an XML file). What I'd really like to do is to define a parent type for each type of thing in the API that

  1. holds the member variables that are common to all of the implementations and

  2. defines the methods that all implementations must have.

So, in (invalid) Go, I want to do something like:

type Person interface {
    Name string
    Title string
    Position string
    Boss() *Person
}

type Person_XML struct {
    Person
}

func (p *Person_XML) Boss() (*Person, error) {
    // Poke around an XML document and answer the question
    return boss, nil
}

type Person_SQL {
    Person
}

func (p *Person_SQL) Boss() (*Person, error) {
    // Do a DB query and construct the record for the boss
    return boss, nil
}

But, of course, that's not legal since only structs have member variables and only interfaces have member functions. I could do this with just interfaces like this:

type Person interface {
    Name() string
    Title() string
    Position() string
    Boss() Person
}

type Person_XML struct {
    NameValue string
    TitleValue string
    PositionValue string
    Person
}

func (p *Person_XML) Name() string {
    return p.NameValue
}

func (p *Person_XML) Title() string {
    return p.TitleValue
}

func (p *Person_XML) Position() string {
    return p.PositionValue
}

func (p *Person_XML) Boss() (Person, error) {
    // Poke around an XML document and answer the question
    return boss, nil
}

and similarly for other implementations. Is there an alternative that doesn't force me to turn member variables into member functions? What's best practice for such a use case?


Solution

  • Best practice would be to provide an interface:

    type Person interface {
        PersonName() string
        PersonTitle() string
        PersonPosition() string
        Boss() (Person, error)
    }
    

    And also provide a struct which contains the common fields and the methods to get them:

    type BasePerson struct {
        Name     string
        Title    string
        Position string
    }
    
    func (p *BasePerson) PersonName() string     { return p.Name }
    func (p *BasePerson) PersonTitle() string    { return p.Title }
    func (p *BasePerson) PersonPosition() string { return p.Position }
    

    (Note: *BasePerson itself does not implement Person as it doesn't have a Boss() method.)

    Any type that embeds *BasePerson will automatically have its methods promoted, and so to implement Person, only the Boss() method will need to be added.

    For example:

    type PersonXML struct {
        *BasePerson
    }
    
    func (p *PersonXML) Boss() (Person, error) {
        // Poke around an XML document and answer the question
        var boss *PersonXML
        return boss, nil
    }
    

    *PersonXML does implement Person.

    Example using it:

    var p Person
    p = &PersonXML{
        BasePerson: &BasePerson{
            Name:     "Bob",
            Title:    "sysadmin",
            Position: "leader",
        },
    }
    fmt.Println(p.PersonName())
    

    Output (try it on the Go Playground):

    Bob
    

    To create the PersonSQL type, you again only have to add the Boss() method if you embed *BasePerson:

    type PersonSQL struct {
        *BasePerson
    }
    
    func (p *PersonSQL) Boss() (Person, error) {
        // Do a DB query and construct the record for the boss
        var boss *PersonSQL
        return boss, nil
    }
    

    *PersonSQL again does implement Person.