loader.go 837 字节
package constant

import (
	"github.com/astaxie/beego"
)

var config = NewConfigLoader()

type ConfigLoader struct {
	History     map[string]interface{}
	HistorySort []string
}

func NewConfigLoader() *ConfigLoader {
	return &ConfigLoader{
		History:     make(map[string]interface{}),
		HistorySort: make([]string, 0),
	}
}

func (c *ConfigLoader) String(key string) (ret string, ok bool) {
	if ret = beego.AppConfig.String(key); len(ret) != 0 {
		ok = true
		c.SetHistory(key, ret)
		return
	}
	return
}

func (c *ConfigLoader) StringDefault(key string, defaultValue string) string {
	if ret, ok := c.String(key); ok {
		return ret
	}
	return defaultValue
}

func (c *ConfigLoader) SetHistory(key string, value interface{}) {
	if _, ok := c.History[key]; !ok {
		c.History[key] = value
		c.HistorySort = append(c.HistorySort, key)
	}
}