1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 19:41:00 +00:00

golint config

This commit is contained in:
astaxie 2015-09-10 14:53:19 +08:00
parent d7aaf2ebeb
commit bdd6a6ae40
6 changed files with 194 additions and 190 deletions

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package config is used to parse config
// Usage: // Usage:
// import( // import(
// "github.com/astaxie/beego/config" // "github.com/astaxie/beego/config"
@ -45,8 +46,8 @@ import (
"fmt" "fmt"
) )
// ConfigContainer defines how to get and set value from configuration raw data. // Configer defines how to get and set value from configuration raw data.
type ConfigContainer interface { type Configer interface {
Set(key, val string) error // support section::key type in given key when using ini type. Set(key, val string) error // support section::key type in given key when using ini type.
String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
Strings(key string) []string //get string slice Strings(key string) []string //get string slice
@ -65,10 +66,10 @@ type ConfigContainer interface {
SaveConfigFile(filename string) error SaveConfigFile(filename string) error
} }
// Config is the adapter interface for parsing config file to get raw data to ConfigContainer. // Config is the adapter interface for parsing config file to get raw data to Configer.
type Config interface { type Config interface {
Parse(key string) (ConfigContainer, error) Parse(key string) (Configer, error)
ParseData(data []byte) (ConfigContainer, error) ParseData(data []byte) (Configer, error)
} }
var adapters = make(map[string]Config) var adapters = make(map[string]Config)
@ -86,9 +87,9 @@ func Register(name string, adapter Config) {
adapters[name] = adapter adapters[name] = adapter
} }
// adapterName is ini/json/xml/yaml. // NewConfig adapterName is ini/json/xml/yaml.
// filename is the config file path. // filename is the config file path.
func NewConfig(adapterName, filename string) (ConfigContainer, error) { func NewConfig(adapterName, filename string) (Configer, error) {
adapter, ok := adapters[adapterName] adapter, ok := adapters[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName) return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName)
@ -96,9 +97,9 @@ func NewConfig(adapterName, filename string) (ConfigContainer, error) {
return adapter.Parse(filename) return adapter.Parse(filename)
} }
// adapterName is ini/json/xml/yaml. // NewConfigData adapterName is ini/json/xml/yaml.
// data is the config data. // data is the config data.
func NewConfigData(adapterName string, data []byte) (ConfigContainer, error) { func NewConfigData(adapterName string, data []byte) (Configer, error) {
adapter, ok := adapters[adapterName] adapter, ok := adapters[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName) return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName)

View File

@ -38,11 +38,11 @@ func (c *fakeConfigContainer) String(key string) string {
} }
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string { func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.getData(key); v == "" { v := c.getData(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Strings(key string) []string { func (c *fakeConfigContainer) Strings(key string) []string {
@ -50,11 +50,11 @@ func (c *fakeConfigContainer) Strings(key string) []string {
} }
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if len(v) == 0 {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Int(key string) (int, error) { func (c *fakeConfigContainer) Int(key string) (int, error) {
@ -62,11 +62,11 @@ func (c *fakeConfigContainer) Int(key string) (int, error) {
} }
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int { func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Int64(key string) (int64, error) { func (c *fakeConfigContainer) Int64(key string) (int64, error) {
@ -74,11 +74,11 @@ func (c *fakeConfigContainer) Int64(key string) (int64, error) {
} }
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Bool(key string) (bool, error) { func (c *fakeConfigContainer) Bool(key string) (bool, error) {
@ -86,11 +86,11 @@ func (c *fakeConfigContainer) Bool(key string) (bool, error) {
} }
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) Float(key string) (float64, error) { func (c *fakeConfigContainer) Float(key string) (float64, error) {
@ -98,11 +98,11 @@ func (c *fakeConfigContainer) Float(key string) (float64, error) {
} }
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) { func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
@ -120,9 +120,10 @@ func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
return errors.New("not implement in the fakeConfigContainer") return errors.New("not implement in the fakeConfigContainer")
} }
var _ ConfigContainer = new(fakeConfigContainer) var _ Configer = new(fakeConfigContainer)
func NewFakeConfig() ConfigContainer { // NewFakeConfig return a fake Congiger
func NewFakeConfig() Configer {
return &fakeConfigContainer{ return &fakeConfigContainer{
data: make(map[string]string), data: make(map[string]string),
} }

View File

@ -31,7 +31,7 @@ import (
) )
var ( var (
DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section, defaultSection = "default" // default section means if some ini items not in a section, make them in default section,
bNumComment = []byte{'#'} // number signal bNumComment = []byte{'#'} // number signal
bSemComment = []byte{';'} // semicolon signal bSemComment = []byte{';'} // semicolon signal
bEmpty = []byte{} bEmpty = []byte{}
@ -46,8 +46,8 @@ var (
type IniConfig struct { type IniConfig struct {
} }
// ParseFile creates a new Config and parses the file configuration from the named file. // Parse creates a new Config and parses the file configuration from the named file.
func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { func (ini *IniConfig) Parse(name string) (Configer, error) {
return ini.parseFile(name) return ini.parseFile(name)
} }
@ -77,7 +77,7 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
buf.ReadByte() buf.ReadByte()
} }
} }
section := DEFAULT_SECTION section := defaultSection
for { for {
line, _, err := buf.ReadLine() line, _, err := buf.ReadLine()
if err == io.EOF { if err == io.EOF {
@ -171,7 +171,8 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
return cfg, nil return cfg, nil
} }
func (ini *IniConfig) ParseData(data []byte) (ConfigContainer, error) { // ParseData parse ini the data
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
// Save memory data to temporary file // Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm) os.MkdirAll(path.Dir(tmpName), os.ModePerm)
@ -181,7 +182,7 @@ func (ini *IniConfig) ParseData(data []byte) (ConfigContainer, error) {
return ini.Parse(tmpName) return ini.Parse(tmpName)
} }
// A Config represents the ini configuration. // IniConfigContainer A Config represents the ini configuration.
// When set and get value, support key as section:name type. // When set and get value, support key as section:name type.
type IniConfigContainer struct { type IniConfigContainer struct {
filename string filename string
@ -199,11 +200,11 @@ func (c *IniConfigContainer) Bool(key string) (bool, error) {
// DefaultBool returns the boolean value for a given key. // DefaultBool returns the boolean value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
@ -214,11 +215,11 @@ func (c *IniConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int { func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
@ -229,11 +230,11 @@ func (c *IniConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
@ -244,11 +245,11 @@ func (c *IniConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// String returns the string value for a given key. // String returns the string value for a given key.
@ -259,11 +260,11 @@ func (c *IniConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultString(key string, defaultval string) string { func (c *IniConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { v := c.String(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
@ -274,20 +275,19 @@ func (c *IniConfigContainer) Strings(key string) []string {
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if len(v) == 0 {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) { func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v, nil return v, nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("not exist setction")
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
@ -301,7 +301,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
// Save default section at first place // Save default section at first place
if dt, ok := c.data[DEFAULT_SECTION]; ok { if dt, ok := c.data[defaultSection]; ok {
for key, val := range dt { for key, val := range dt {
if key != " " { if key != " " {
// Write key comments. // Write key comments.
@ -325,7 +325,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
} }
// Save named sections // Save named sections
for section, dt := range c.data { for section, dt := range c.data {
if section != DEFAULT_SECTION { if section != defaultSection {
// Write section comments. // Write section comments.
if v, ok := c.sectionComment[section]; ok { if v, ok := c.sectionComment[section]; ok {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil {
@ -367,7 +367,7 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
return nil return nil
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
// if write to one section, the key need be "section::key". // if write to one section, the key need be "section::key".
// if the section is not existed, it panics. // if the section is not existed, it panics.
func (c *IniConfigContainer) Set(key, value string) error { func (c *IniConfigContainer) Set(key, value string) error {
@ -379,14 +379,14 @@ func (c *IniConfigContainer) Set(key, value string) error {
var ( var (
section, k string section, k string
sectionKey []string = strings.Split(key, "::") sectionKey = strings.Split(key, "::")
) )
if len(sectionKey) >= 2 { if len(sectionKey) >= 2 {
section = sectionKey[0] section = sectionKey[0]
k = sectionKey[1] k = sectionKey[1]
} else { } else {
section = DEFAULT_SECTION section = defaultSection
k = sectionKey[0] k = sectionKey[0]
} }
@ -415,13 +415,13 @@ func (c *IniConfigContainer) getdata(key string) string {
var ( var (
section, k string section, k string
sectionKey []string = strings.Split(strings.ToLower(key), "::") sectionKey = strings.Split(strings.ToLower(key), "::")
) )
if len(sectionKey) >= 2 { if len(sectionKey) >= 2 {
section = sectionKey[0] section = sectionKey[0]
k = sectionKey[1] k = sectionKey[1]
} else { } else {
section = DEFAULT_SECTION section = defaultSection
k = sectionKey[0] k = sectionKey[0]
} }
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {

View File

@ -23,12 +23,12 @@ import (
"sync" "sync"
) )
// JsonConfig is a json config parser and implements Config interface. // JSONConfig is a json config parser and implements Config interface.
type JsonConfig struct { type JSONConfig struct {
} }
// Parse returns a ConfigContainer with parsed json config map. // Parse returns a ConfigContainer with parsed json config map.
func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { func (js *JSONConfig) Parse(filename string) (Configer, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -43,8 +43,8 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
} }
// ParseData returns a ConfigContainer with json string // ParseData returns a ConfigContainer with json string
func (js *JsonConfig) ParseData(data []byte) (ConfigContainer, error) { func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
x := &JsonConfigContainer{ x := &JSONConfigContainer{
data: make(map[string]interface{}), data: make(map[string]interface{}),
} }
err := json.Unmarshal(data, &x.data) err := json.Unmarshal(data, &x.data)
@ -59,15 +59,15 @@ func (js *JsonConfig) ParseData(data []byte) (ConfigContainer, error) {
return x, nil return x, nil
} }
// A Config represents the json configuration. // JSONConfigContainer A Config represents the json configuration.
// Only when get value, support key as section:name type. // Only when get value, support key as section:name type.
type JsonConfigContainer struct { type JSONConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.RWMutex sync.RWMutex
} }
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *JsonConfigContainer) Bool(key string) (bool, error) { func (c *JSONConfigContainer) Bool(key string) (bool, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(bool); ok { if v, ok := val.(bool); ok {
@ -80,7 +80,7 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
// DefaultBool return the bool value if has no error // DefaultBool return the bool value if has no error
// otherwise return the defaultval // otherwise return the defaultval
func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *JSONConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err == nil { if v, err := c.Bool(key); err == nil {
return v return v
} }
@ -88,7 +88,7 @@ func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool {
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *JsonConfigContainer) Int(key string) (int, error) { func (c *JSONConfigContainer) Int(key string) (int, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(float64); ok { if v, ok := val.(float64); ok {
@ -101,7 +101,7 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int { func (c *JSONConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err == nil { if v, err := c.Int(key); err == nil {
return v return v
} }
@ -109,7 +109,7 @@ func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int {
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *JsonConfigContainer) Int64(key string) (int64, error) { func (c *JSONConfigContainer) Int64(key string) (int64, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(float64); ok { if v, ok := val.(float64); ok {
@ -122,7 +122,7 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *JSONConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err == nil { if v, err := c.Int64(key); err == nil {
return v return v
} }
@ -130,7 +130,7 @@ func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *JsonConfigContainer) Float(key string) (float64, error) { func (c *JSONConfigContainer) Float(key string) (float64, error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(float64); ok { if v, ok := val.(float64); ok {
@ -143,7 +143,7 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *JSONConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err == nil { if v, err := c.Float(key); err == nil {
return v return v
} }
@ -151,7 +151,7 @@ func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *JsonConfigContainer) String(key string) string { func (c *JSONConfigContainer) String(key string) string {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
if v, ok := val.(string); ok { if v, ok := val.(string); ok {
@ -163,7 +163,7 @@ func (c *JsonConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultString(key string, defaultval string) string { func (c *JSONConfigContainer) DefaultString(key string, defaultval string) string {
// TODO FIXME should not use "" to replace non existance // TODO FIXME should not use "" to replace non existance
if v := c.String(key); v != "" { if v := c.String(key); v != "" {
return v return v
@ -172,7 +172,7 @@ func (c *JsonConfigContainer) DefaultString(key string, defaultval string) strin
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *JsonConfigContainer) Strings(key string) []string { func (c *JSONConfigContainer) Strings(key string) []string {
stringVal := c.String(key) stringVal := c.String(key)
if stringVal == "" { if stringVal == "" {
return []string{} return []string{}
@ -182,7 +182,7 @@ func (c *JsonConfigContainer) Strings(key string) []string {
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) > 0 { if v := c.Strings(key); len(v) > 0 {
return v return v
} }
@ -190,7 +190,7 @@ func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *JsonConfigContainer) GetSection(section string) (map[string]string, error) { func (c *JSONConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v.(map[string]string), nil return v.(map[string]string), nil
} }
@ -198,7 +198,7 @@ func (c *JsonConfigContainer) GetSection(section string) (map[string]string, err
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) { func (c *JSONConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename. // Write configuration file by filename.
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -214,7 +214,7 @@ func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) {
} }
// Set writes a new value for key. // Set writes a new value for key.
func (c *JsonConfigContainer) Set(key, val string) error { func (c *JSONConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.data[key] = val c.data[key] = val
@ -222,7 +222,7 @@ func (c *JsonConfigContainer) Set(key, val string) error {
} }
// DIY returns the raw value by a given key. // DIY returns the raw value by a given key.
func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { func (c *JSONConfigContainer) DIY(key string) (v interface{}, err error) {
val := c.getData(key) val := c.getData(key)
if val != nil { if val != nil {
return val, nil return val, nil
@ -231,7 +231,7 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
} }
// section.key or key // section.key or key
func (c *JsonConfigContainer) getData(key string) interface{} { func (c *JSONConfigContainer) getData(key string) interface{} {
if len(key) == 0 { if len(key) == 0 {
return nil return nil
} }
@ -261,5 +261,5 @@ func (c *JsonConfigContainer) getData(key string) interface{} {
} }
func init() { func init() {
Register("json", &JsonConfig{}) Register("json", &JSONConfig{})
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// package xml for config provider // Package xml for config provider
// //
// depend on github.com/beego/x2j // depend on github.com/beego/x2j
// //
@ -45,20 +45,20 @@ import (
"github.com/beego/x2j" "github.com/beego/x2j"
) )
// XmlConfig is a xml config parser and implements Config interface. // Config is a xml config parser and implements Config interface.
// xml configurations should be included in <config></config> tag. // xml configurations should be included in <config></config> tag.
// only support key/value pair as <key>value</key> as each item. // only support key/value pair as <key>value</key> as each item.
type XMLConfig struct{} type Config struct{}
// Parse returns a ConfigContainer with parsed xml config map. // Parse returns a ConfigContainer with parsed xml config map.
func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { func (xc *Config) Parse(filename string) (config.Configer, error) {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close() defer file.Close()
x := &XMLConfigContainer{data: make(map[string]interface{})} x := &ConfigContainer{data: make(map[string]interface{})}
content, err := ioutil.ReadAll(file) content, err := ioutil.ReadAll(file)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,84 +73,86 @@ func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) {
return x, nil return x, nil
} }
func (x *XMLConfig) ParseData(data []byte) (config.ConfigContainer, error) { // ParseData xml data
func (xc *Config) ParseData(data []byte) (config.Configer, error) {
// Save memory data to temporary file // Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm) os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil { if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err return nil, err
} }
return x.Parse(tmpName) return xc.Parse(tmpName)
} }
// A Config represents the xml configuration. // ConfigContainer A Config represents the xml configuration.
type XMLConfigContainer struct { type ConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.Mutex sync.Mutex
} }
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *XMLConfigContainer) Bool(key string) (bool, error) { func (c *ConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.data[key].(string)) return strconv.ParseBool(c.data[key].(string))
} }
// DefaultBool return the bool value if has no error // DefaultBool return the bool value if has no error
// otherwise return the defaultval // otherwise return the defaultval
func (c *XMLConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *XMLConfigContainer) Int(key string) (int, error) { func (c *ConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.data[key].(string)) return strconv.Atoi(c.data[key].(string))
} }
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultInt(key string, defaultval int) int { func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *XMLConfigContainer) Int64(key string) (int64, error) { func (c *ConfigContainer) Int64(key string) (int64, error) {
return strconv.ParseInt(c.data[key].(string), 10, 64) return strconv.ParseInt(c.data[key].(string), 10, 64)
} }
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *XMLConfigContainer) Float(key string) (float64, error) { func (c *ConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.data[key].(string), 64) return strconv.ParseFloat(c.data[key].(string), 64)
} }
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *XMLConfigContainer) String(key string) string { func (c *ConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok { if v, ok := c.data[key].(string); ok {
return v return v
} }
@ -159,40 +161,39 @@ func (c *XMLConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultString(key string, defaultval string) string { func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { v := c.String(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *XMLConfigContainer) Strings(key string) []string { func (c *ConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *XMLConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if len(v) == 0 {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *XMLConfigContainer) GetSection(section string) (map[string]string, error) { func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v.(map[string]string), nil return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("not exist setction")
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
func (c *XMLConfigContainer) SaveConfigFile(filename string) (err error) { func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename. // Write configuration file by filename.
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -207,8 +208,8 @@ func (c *XMLConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
func (c *XMLConfigContainer) Set(key, val string) error { func (c *ConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.data[key] = val c.data[key] = val
@ -216,7 +217,7 @@ func (c *XMLConfigContainer) Set(key, val string) error {
} }
// DIY returns the raw value by a given key. // DIY returns the raw value by a given key.
func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok { if v, ok := c.data[key]; ok {
return v, nil return v, nil
} }
@ -224,5 +225,5 @@ func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) {
} }
func init() { func init() {
config.Register("xml", &XMLConfig{}) config.Register("xml", &Config{})
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// package yaml for config provider // Package yaml for config provider
// //
// depend on github.com/beego/goyaml2 // depend on github.com/beego/goyaml2
// //
@ -46,22 +46,23 @@ import (
"github.com/beego/goyaml2" "github.com/beego/goyaml2"
) )
// YAMLConfig is a yaml config parser and implements Config interface. // Config is a yaml config parser and implements Config interface.
type YAMLConfig struct{} type Config struct{}
// Parse returns a ConfigContainer with parsed yaml config map. // Parse returns a ConfigContainer with parsed yaml config map.
func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err error) { func (yaml *Config) Parse(filename string) (y config.Configer, err error) {
cnf, err := ReadYmlReader(filename) cnf, err := ReadYmlReader(filename)
if err != nil { if err != nil {
return return
} }
y = &YAMLConfigContainer{ y = &ConfigContainer{
data: cnf, data: cnf,
} }
return return
} }
func (yaml *YAMLConfig) ParseData(data []byte) (config.ConfigContainer, error) { // ParseData parse yaml data
func (yaml *Config) ParseData(data []byte) (config.Configer, error) {
// Save memory data to temporary file // Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond())) tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm) os.MkdirAll(path.Dir(tmpName), os.ModePerm)
@ -71,7 +72,7 @@ func (yaml *YAMLConfig) ParseData(data []byte) (config.ConfigContainer, error) {
return yaml.Parse(tmpName) return yaml.Parse(tmpName)
} }
// Read yaml file to map. // ReadYmlReader Read yaml file to map.
// if json like, use json package, unless goyaml2 package. // if json like, use json package, unless goyaml2 package.
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
f, err := os.Open(path) f, err := os.Open(path)
@ -112,14 +113,14 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
return return
} }
// A Config represents the yaml configuration. // ConfigContainer A Config represents the yaml configuration.
type YAMLConfigContainer struct { type ConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.Mutex sync.Mutex
} }
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *YAMLConfigContainer) Bool(key string) (bool, error) { func (c *ConfigContainer) Bool(key string) (bool, error) {
if v, ok := c.data[key].(bool); ok { if v, ok := c.data[key].(bool); ok {
return v, nil return v, nil
} }
@ -128,16 +129,16 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
// DefaultBool return the bool value if has no error // DefaultBool return the bool value if has no error
// otherwise return the defaultval // otherwise return the defaultval
func (c *YAMLConfigContainer) DefaultBool(key string, defaultval bool) bool { func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil { v, err := c.Bool(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *YAMLConfigContainer) Int(key string) (int, error) { func (c *ConfigContainer) Int(key string) (int, error) {
if v, ok := c.data[key].(int64); ok { if v, ok := c.data[key].(int64); ok {
return int(v), nil return int(v), nil
} }
@ -146,16 +147,16 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) {
// DefaultInt returns the integer value for a given key. // DefaultInt returns the integer value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultInt(key string, defaultval int) int { func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil { v, err := c.Int(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *YAMLConfigContainer) Int64(key string) (int64, error) { func (c *ConfigContainer) Int64(key string) (int64, error) {
if v, ok := c.data[key].(int64); ok { if v, ok := c.data[key].(int64); ok {
return v, nil return v, nil
} }
@ -164,16 +165,16 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 { func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil { v, err := c.Int64(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *YAMLConfigContainer) Float(key string) (float64, error) { func (c *ConfigContainer) Float(key string) (float64, error) {
if v, ok := c.data[key].(float64); ok { if v, ok := c.data[key].(float64); ok {
return v, nil return v, nil
} }
@ -182,16 +183,16 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) {
// DefaultFloat returns the float64 value for a given key. // DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 { func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil { v, err := c.Float(key)
if err != nil {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *YAMLConfigContainer) String(key string) string { func (c *ConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok { if v, ok := c.data[key].(string); ok {
return v return v
} }
@ -200,40 +201,40 @@ func (c *YAMLConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key. // DefaultString returns the string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultString(key string, defaultval string) string { func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" { v := c.String(key)
if v == "" {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *YAMLConfigContainer) Strings(key string) []string { func (c *ConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key. // DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval // if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultStrings(key string, defaultval []string) []string { func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 { v := c.Strings(key)
if len(v) == 0 {
return defaultval return defaultval
} else {
return v
} }
return v
} }
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *YAMLConfigContainer) GetSection(section string) (map[string]string, error) { func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { v, ok := c.data[section]
if ok {
return v.(map[string]string), nil return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
} }
return nil, errors.New("not exist setction")
} }
// SaveConfigFile save the config into file // SaveConfigFile save the config into file
func (c *YAMLConfigContainer) SaveConfigFile(filename string) (err error) { func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename. // Write configuration file by filename.
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {
@ -244,8 +245,8 @@ func (c *YAMLConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
// WriteValue writes a new value for key. // Set writes a new value for key.
func (c *YAMLConfigContainer) Set(key, val string) error { func (c *ConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.data[key] = val c.data[key] = val
@ -253,7 +254,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error {
} }
// DIY returns the raw value by a given key. // DIY returns the raw value by a given key.
func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) { func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok { if v, ok := c.data[key]; ok {
return v, nil return v, nil
} }
@ -261,5 +262,5 @@ func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) {
} }
func init() { func init() {
config.Register("yaml", &YAMLConfig{}) config.Register("yaml", &Config{})
} }