aboutsummaryrefslogblamecommitdiff
path: root/connector/config.go
blob: e0fcf171b1ba30da6d2d5b0a49fd926f87411558 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                 
             
                 
                 



                                    
                                                                             
                               
                              
         



                                                                 

 
                                                                    


                                       



















                                                                         
 
package connector

import (
	"fmt"
	"strconv"
	"strings"
)

type Configuration map[string]string

func (c Configuration) GetString(k string, deflt ...string) (string, error) {
	if ss, ok := c[k]; ok {
		return ss, nil
	}
	if len(deflt) > 0 {
		return deflt[0], nil
	}
	return "", fmt.Errorf("Missing configuration key: %s", k)
}

func (c Configuration) GetInt(k string, deflt ...int) (int, error) {
	if ss, ok := c[k]; ok {
		return strconv.Atoi(ss)
	}
	if len(deflt) > 0 {
		return deflt[0], nil
	}
	return 0, fmt.Errorf("Missing configuration key: %s", k)
}

func (c Configuration) GetBool(k string, deflt ...bool) (bool, error) {
	if ss, ok := c[k]; ok {
		if strings.EqualFold(ss, "true") {
			return true, nil
		} else if strings.EqualFold(ss, "false") {
			return false, nil
		} else {
			return false, fmt.Errorf("Invalid value: %s", ss)
		}
	}
	if len(deflt) > 0 {
		return deflt[0], nil
	}
	return false, fmt.Errorf("Missing configuration key: %s", k)
}