bhutch29 / abv

Bar inventory tracking system
MIT License
6 stars 2 forks source link

Support for beer name nicknames #45

Closed bhutch29 closed 6 years ago

ryanavella commented 6 years ago

I think we can avoid code duplication by having a method which initializes nicknames for both breweries and beer names. Maybe something like this:

var brandNicks map[string]string
var beerNicks map[string]string

func init() {
    brandNicks = csvToMap("brandnicks.utf8")
    beerNicks = csvToMap("beernicks.utf8")
}

func csvToMap(filename string) (nickname map[string]string) {
    file, err := os.Open(filename)
    if err != nil {
        return nickname // empty map
    }
    nickname = make(map[string]string)
    r := csv.NewReader(file)
    _, err = r.Read() // discard headers
    for {
        items, err := r.Read()
        if err == io.EOF {
            break
        } else if err != nil {
            log.Fatal(err)
        }
        long := items[0]
        short := items[1]
        nickname[long] = short
    }
    return nickname
}
bhutch29 commented 6 years ago

I just found the package viper and will be using it for reading configuration information from files. It supports TOML and YAML syntax, which support maps. We may want to consider moving nickname stuff into the same config file...

ryanavella commented 6 years ago

I'm all for it, that would at least tidy up our current mess of user-supplied files, databases, and environment variables.