Skip to content

Instantly share code, notes, and snippets.

@augier
Last active October 13, 2016 13:31
Show Gist options
  • Select an option

  • Save augier/46724dc287142530dada8a5ae0640e14 to your computer and use it in GitHub Desktop.

Select an option

Save augier/46724dc287142530dada8a5ae0640e14 to your computer and use it in GitHub Desktop.
An example of inheritance in golang
package main
import (
"github.com/connectedventures/f8-pkg/odds"
)
// EventParser is the interface for all parsers
type EventParser interface {
GetEvent() (*odds.Event, error)
constructFacets() odds.Facets
constructMeta() odds.Meta
}
// GenericEventParser is the generic implementation of the EventParser which
// should be used unless any specific overrides of logic are required for a
// sport
type GenericEventParser struct {
Sport odds.Sport
}
// GetEvent parses an odds.Event from the raw data
func (gep *GenericEventParser) GetEvent() (*odds.Event, error) {
return &odds.Event{
Facets: gep.constructFacets(),
Meta: gep.constructMeta(),
}, nil
}
// constructFacets builds the facets for an event
func (gep *GenericEventParser) constructFacets() odds.Facets {
return odds.Facets{
"competitor:competitorA": 1,
"competitor:competitorB": 1,
"competition:competition": 1,
}
}
// constructMeta builds the meta for an event
func (gep *GenericEventParser) constructMeta() odds.Meta {
return odds.Meta{
"event": "event name",
}
}
// FootballEventParser is the implementation of EventParser which is used for
// football only. It overrides some of the logic contained in the
// GenericEventParser
type FootballEventParser struct {
Parser GenericEventParser
}
// GetEvent parses an odds.Event from the raw data
func (fep *FootballEventParser) GetEvent() (*odds.Event, error) {
return &odds.Event{
Facets: fep.constructFacets(),
Meta: fep.constructMeta(),
}, nil
}
// constructFacets builds the facets for a football event
func (fep *FootballEventParser) constructFacets() odds.Facets {
return fep.Parser.constructFacets()
}
// constructMeta builds the meta for a football event
func (fep *FootballEventParser) constructMeta() odds.Meta {
meta := fep.Parser.constructMeta()
meta["home"] = "team a"
meta["away"] = "team b"
return meta
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment