1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-12 13:10:39 +00:00

Support etcd

This commit is contained in:
Ming Deng
2020-08-26 03:46:22 +00:00
parent 5b35bf6065
commit c2361170b3
20 changed files with 581 additions and 257 deletions

View File

@ -15,6 +15,7 @@
package config
import (
"context"
"errors"
"testing"
@ -59,7 +60,7 @@ func TestBaseConfiger_DefaultStrings(t *testing.T) {
func newBaseConfier(str1 string) *BaseConfiger {
return &BaseConfiger{
reader: func(key string) (string, error) {
reader: func(ctx context.Context, key string) (string, error) {
if key == "key1" {
return str1, nil
} else {

View File

@ -41,6 +41,7 @@
package config
import (
"context"
"errors"
"fmt"
"os"
@ -56,9 +57,9 @@ type Configer interface {
Set(key, val string) error
// support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
String(key string) string
String(key string) (string, error)
// get string slice
Strings(key string) []string
Strings(key string) ([]string, error)
Int(key string) (int, error)
Int64(key string) (int64, error)
Bool(key string) (bool, error)
@ -72,11 +73,13 @@ type Configer interface {
DefaultBool(key string, defaultVal bool) bool
DefaultFloat(key string, defaultVal float64) float64
DIY(key string) (interface{}, error)
GetSection(section string) (map[string]string, error)
Unmarshaler(obj interface{}) error
GetSection(section string) (map[string]string, error)
GetSectionWithCtx(ctx context.Context, section string) (map[string]string, error)
Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...DecodeOption) error
Sub(key string) (Configer, error)
OnChange(fn func(cfg Configer))
OnChange(ctx context.Context, key string, fn func(value string))
// GetByPrefix(prefix string) ([]byte, error)
// GetSerializer() Serializer
SaveConfigFile(filename string) error
@ -84,11 +87,17 @@ type Configer interface {
type BaseConfiger struct {
// The reader should support key like "a.b.c"
reader func(key string) (string, error)
reader func(ctx context.Context, key string) (string, error)
}
func NewBaseConfiger(reader func(ctx context.Context, key string) (string, error)) BaseConfiger {
return BaseConfiger{
reader: reader,
}
}
func (c *BaseConfiger) Int(key string) (int, error) {
res, err := c.reader(key)
res, err := c.reader(context.TODO(), key)
if err != nil {
return 0, err
}
@ -96,7 +105,7 @@ func (c *BaseConfiger) Int(key string) (int, error) {
}
func (c *BaseConfiger) Int64(key string) (int64, error) {
res, err := c.reader(key)
res, err := c.reader(context.TODO(), key)
if err != nil {
return 0, err
}
@ -104,30 +113,34 @@ func (c *BaseConfiger) Int64(key string) (int64, error) {
}
func (c *BaseConfiger) Bool(key string) (bool, error) {
res, err := c.reader(key)
res, err := c.reader(context.TODO(), key)
if err != nil {
return false, err
}
return strconv.ParseBool(res)
return ParseBool(res)
}
func (c *BaseConfiger) Float(key string) (float64, error) {
res, err := c.reader(key)
res, err := c.reader(context.TODO(), key)
if err != nil {
return 0, err
}
return strconv.ParseFloat(res, 64)
}
// DefaultString returns the string value for a given key.
// if err != nil or value is empty return defaultval
func (c *BaseConfiger) DefaultString(key string, defaultVal string) string {
if res := c.String(key); res != "" {
if res, err := c.String(key); res != "" && err != nil {
return res
}
return defaultVal
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *BaseConfiger) DefaultStrings(key string, defaultVal []string) []string {
if res := c.Strings(key); len(res) > 0 {
if res, err := c.Strings(key); len(res) > 0 && err != nil {
return res
}
return defaultVal
@ -160,21 +173,27 @@ func (c *BaseConfiger) DefaultFloat(key string, defaultVal float64) float64 {
return defaultVal
}
func (c *BaseConfiger) String(key string) string {
res, _ := c.reader(key)
return res
func (c *BaseConfiger) GetSectionWithCtx(ctx context.Context, section string) (map[string]string, error) {
// TODO
return nil, nil
}
func (c *BaseConfiger) Strings(key string) []string {
res, err := c.reader(key)
func (c *BaseConfiger) String(key string) (string, error) {
return c.reader(context.TODO(), key)
}
// Strings returns the []string value for a given key.
// Return nil if config value does not exist or is empty.
func (c *BaseConfiger) Strings(key string) ([]string, error) {
res, err := c.String(key)
if err != nil || res == "" {
return nil
return nil, err
}
return strings.Split(res, ";")
return strings.Split(res, ";"), nil
}
// TODO remove this before release v2.0.0
func (c *BaseConfiger) Unmarshaler(obj interface{}) error {
func (c *BaseConfiger) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...DecodeOption) error {
return errors.New("unsupported operation")
}
@ -184,7 +203,7 @@ func (c *BaseConfiger) Sub(key string) (Configer, error) {
}
// TODO remove this before release v2.0.0
func (c *BaseConfiger) OnChange(fn func(cfg Configer)) {
func (c *BaseConfiger) OnChange(ctx context.Context, key string, fn func(value string)) {
// do nothing
}
@ -361,3 +380,8 @@ func ToString(x interface{}) string {
// Fallback to fmt package for anything else like numeric types
return fmt.Sprint(x)
}
type DecodeOption func(options decodeOptions)
type decodeOptions struct {
}

View File

@ -0,0 +1,219 @@
// Copyright 2020
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package etcd
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/coreos/etcd/clientv3"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"google.golang.org/grpc"
"github.com/astaxie/beego/pkg/infrastructure/config"
"github.com/astaxie/beego/pkg/infrastructure/logs"
)
const etcdOpts = "etcdOpts"
type EtcdConfiger struct {
prefix string
client *clientv3.Client
config.BaseConfiger
}
func newEtcdConfiger(client *clientv3.Client, prefix string) *EtcdConfiger {
res := &EtcdConfiger{
client: client,
prefix: prefix,
}
res.BaseConfiger = config.NewBaseConfiger(res.reader)
return res
}
// reader is an general implementation that read config from etcd.
func (e *EtcdConfiger) reader(ctx context.Context, key string) (string, error) {
resp, err := get(e.client, ctx, e.prefix+key)
if err != nil {
return "", err
}
if resp.Count > 0 {
return string(resp.Kvs[0].Value), nil
}
return "", nil
}
// Set do nothing and return an error
// I think write data to remote config center is not a good practice
func (e *EtcdConfiger) Set(key, val string) error {
return errors.New("Unsupported operation")
}
// DIY return the original response from etcd
// be careful when you decide to use this
func (e *EtcdConfiger) DIY(key string) (interface{}, error) {
return get(e.client, context.TODO(), key)
}
// GetSection in this implementation, we use section as prefix
func (e *EtcdConfiger) GetSection(section string) (map[string]string, error) {
return e.GetSectionWithCtx(context.Background(), section)
}
func (e *EtcdConfiger) GetSectionWithCtx(ctx context.Context, section string) (map[string]string, error) {
var (
resp *clientv3.GetResponse
err error
)
if opts, ok := ctx.Value(etcdOpts).([]clientv3.OpOption); ok {
opts = append(opts, clientv3.WithPrefix())
resp, err = e.client.Get(context.TODO(), e.prefix+section, opts...)
} else {
resp, err = e.client.Get(context.TODO(), e.prefix+section, clientv3.WithPrefix())
}
if err != nil {
return nil, errors.WithMessage(err, "GetSection failed")
}
res := make(map[string]string, len(resp.Kvs))
for _, kv := range resp.Kvs {
res[string(kv.Key)] = string(kv.Value)
}
return res, nil
}
func (e *EtcdConfiger) SaveConfigFile(filename string) error {
return errors.New("Unsupported operation")
}
// Unmarshaler is not very powerful because we lost the type information when we get configuration from etcd
// for example, when we got "5", we are not sure whether it's int 5, or it's string "5"
// TODO(support more complicated decoder)
func (e *EtcdConfiger) Unmarshaler(ctx context.Context, prefix string, obj interface{}, opt ...config.DecodeOption) error {
res, err := e.GetSectionWithCtx(ctx, prefix)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("could not read config with prefix: %s", prefix))
}
prefixLen := len(e.prefix + prefix)
m := make(map[string]string, len(res))
for k, v := range res {
m[k[prefixLen:]] = v
}
return mapstructure.Decode(m, obj)
}
// Sub return an sub configer.
func (e *EtcdConfiger) Sub(key string) (config.Configer, error) {
return newEtcdConfiger(e.client, e.prefix+key), nil
}
// TODO remove this before release v2.0.0
func (e *EtcdConfiger) OnChange(ctx context.Context, key string, fn func(value string)) {
buildOptsFunc := func() []clientv3.OpOption {
if opts, ok := ctx.Value(etcdOpts).([]clientv3.OpOption); ok {
opts = append(opts, clientv3.WithCreatedNotify())
return opts
}
return []clientv3.OpOption{}
}
rch := e.client.Watch(ctx, e.prefix+key, buildOptsFunc()...)
go func() {
for {
for resp := range rch {
if err := resp.Err(); err != nil {
logs.Error("listen to key but got error callback", err)
break
}
for _, e := range resp.Events {
if e.Kv == nil {
continue
}
fn(string(e.Kv.Value))
}
}
time.Sleep(time.Second)
rch = e.client.Watch(ctx, e.prefix+key, buildOptsFunc()...)
}
}()
}
type EtcdConfigerProvider struct {
}
// Parse = ParseData([]byte(key))
// key must be json
func (provider *EtcdConfigerProvider) Parse(key string) (config.Configer, error) {
return provider.ParseData([]byte(key))
}
// ParseData try to parse key as clientv3.Config, using this to build etcdClient
func (provider *EtcdConfigerProvider) ParseData(data []byte) (config.Configer, error) {
cfg := &clientv3.Config{}
err := json.Unmarshal(data, cfg)
if err != nil {
return nil, errors.WithMessage(err, "parse data to etcd config failed, please check your input")
}
cfg.DialOptions = []grpc.DialOption{
grpc.WithBlock(),
grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
}
client, err := clientv3.New(*cfg)
if err != nil {
return nil, errors.WithMessage(err, "create etcd client failed")
}
return newEtcdConfiger(client, ""), nil
}
func get(client *clientv3.Client, ctx context.Context, key string) (*clientv3.GetResponse, error) {
var (
resp *clientv3.GetResponse
err error
)
if opts, ok := ctx.Value(etcdOpts).([]clientv3.OpOption); ok {
resp, err = client.Get(ctx, key, opts...)
} else {
resp, err = client.Get(ctx, key)
}
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("read config from etcd with key %s failed", key))
}
return resp, err
}
func WithEtcdOption(ctx context.Context, opts ...clientv3.OpOption) context.Context {
return context.WithValue(ctx, etcdOpts, opts)
}
func init() {
config.Register("json", &EtcdConfigerProvider{})
}

View File

@ -0,0 +1,123 @@
// Copyright 2020
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package etcd
import (
"context"
"encoding/json"
"os"
"testing"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/stretchr/testify/assert"
)
func TestWithEtcdOption(t *testing.T) {
ctx := WithEtcdOption(context.Background(), clientv3.WithPrefix())
assert.NotNil(t, ctx.Value(etcdOpts))
}
func TestEtcdConfigerProvider_Parse(t *testing.T) {
provider := &EtcdConfigerProvider{}
cfger, err := provider.Parse(readEtcdConfig())
assert.Nil(t, err)
assert.NotNil(t, cfger)
}
func TestEtcdConfiger(t *testing.T) {
provider := &EtcdConfigerProvider{}
cfger, _ := provider.Parse(readEtcdConfig())
subCfger, err := cfger.Sub("sub.")
assert.Nil(t, err)
assert.NotNil(t, subCfger)
subSubCfger, err := subCfger.Sub("sub.")
assert.NotNil(t, subSubCfger)
assert.Nil(t, err)
str, err := subSubCfger.String("key1")
assert.Nil(t, err)
assert.Equal(t, "sub.sub.key", str)
// we cannot test it
subSubCfger.OnChange(context.Background(), "watch", func(value string) {
// do nothing
})
defStr := cfger.DefaultString("not_exit", "default value")
assert.Equal(t, "default value", defStr)
defInt64 := cfger.DefaultInt64("not_exit", -1)
assert.Equal(t, int64(-1), defInt64)
defInt := cfger.DefaultInt("not_exit", -2)
assert.Equal(t, -2, defInt)
defFlt := cfger.DefaultFloat("not_exit", 12.3)
assert.Equal(t, 12.3, defFlt)
defBl := cfger.DefaultBool("not_exit", true)
assert.True(t, defBl)
defStrs := cfger.DefaultStrings("not_exit", []string{"hello"})
assert.Equal(t, []string{"hello"}, defStrs)
fl, err := cfger.Float("current.float")
assert.Nil(t, err)
assert.Equal(t, 1.23, fl)
bl, err := cfger.Bool("current.bool")
assert.Nil(t, err)
assert.True(t, bl)
it, err := cfger.Int("current.int")
assert.Nil(t, err)
assert.Equal(t, 11, it)
str, err = cfger.String("current.string")
assert.Nil(t, err)
assert.Equal(t, "hello", str)
tn := &TestEntity{}
err = cfger.Unmarshaler(context.Background(), "current.serialize.", tn)
assert.Nil(t, err)
assert.Equal(t, "test", tn.Name)
}
type TestEntity struct {
Name string `yaml:"name"`
Sub SubEntity `yaml:"sub"`
}
type SubEntity struct {
SubName string `yaml:"subName"`
}
func readEtcdConfig() string {
addr := os.Getenv("ETCD_ADDR")
if addr == "" {
addr = "localhost:2379"
}
obj := clientv3.Config{
Endpoints: []string{addr},
DialTimeout: 3 * time.Second,
}
cfg, _ := json.Marshal(obj)
return string(cfg)
}

View File

@ -15,6 +15,7 @@
package config
import (
"context"
"errors"
"strconv"
"strings"
@ -34,34 +35,6 @@ func (c *fakeConfigContainer) Set(key, val string) error {
return nil
}
func (c *fakeConfigContainer) String(key string) string {
return c.getData(key)
}
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
v := c.String(key)
if v == "" {
return defaultval
}
return v
}
func (c *fakeConfigContainer) Strings(key string) []string {
v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
}
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v := c.Strings(key)
if v == nil {
return defaultval
}
return v
}
func (c *fakeConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.getData(key))
}
@ -129,7 +102,11 @@ var _ Configer = new(fakeConfigContainer)
// NewFakeConfig return a fake Configer
func NewFakeConfig() Configer {
return &fakeConfigContainer{
res := &fakeConfigContainer{
data: make(map[string]string),
}
res.BaseConfiger = NewBaseConfiger(func(ctx context.Context, key string) (string, error) {
return res.getData(key), nil
})
return res
}

View File

@ -17,13 +17,14 @@ package config
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
)
@ -65,6 +66,9 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
keyComment: make(map[string]string),
RWMutex: sync.RWMutex{},
}
cfg.BaseConfiger = NewBaseConfiger(func(ctx context.Context, key string) (string, error) {
return cfg.getdata(key)
})
cfg.Lock()
defer cfg.Unlock()
@ -90,7 +94,7 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
break
}
//It might be a good idea to throw a error on all unknonw errors?
// It might be a good idea to throw a error on all unknonw errors?
if _, ok := err.(*os.PathError); ok {
return nil, err
}
@ -232,101 +236,6 @@ type IniConfigContainer struct {
sync.RWMutex
}
// Bool returns the boolean value for a given key.
func (c *IniConfigContainer) Bool(key string) (bool, error) {
return ParseBool(c.getdata(key))
}
// DefaultBool returns the boolean value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
v, err := c.Bool(key)
if err != nil {
return defaultval
}
return v
}
// Int returns the integer value for a given key.
func (c *IniConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.getdata(key))
}
// DefaultInt returns the integer value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
v, err := c.Int(key)
if err != nil {
return defaultval
}
return v
}
// Int64 returns the int64 value for a given key.
func (c *IniConfigContainer) Int64(key string) (int64, error) {
return strconv.ParseInt(c.getdata(key), 10, 64)
}
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
v, err := c.Int64(key)
if err != nil {
return defaultval
}
return v
}
// Float returns the float value for a given key.
func (c *IniConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.getdata(key), 64)
}
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
v, err := c.Float(key)
if err != nil {
return defaultval
}
return v
}
// String returns the string value for a given key.
func (c *IniConfigContainer) String(key string) string {
return c.getdata(key)
}
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultString(key string, defaultval string) string {
v := c.String(key)
if v == "" {
return defaultval
}
return v
}
// Strings returns the []string value for a given key.
// Return nil if config value does not exist or is empty.
func (c *IniConfigContainer) Strings(key string) []string {
v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v := c.Strings(key)
if v == nil {
return defaultval
}
return v
}
// GetSection returns map for the given section
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
@ -474,9 +383,9 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
}
// section.key or key
func (c *IniConfigContainer) getdata(key string) string {
func (c *IniConfigContainer) getdata(key string) (string, error) {
if len(key) == 0 {
return ""
return "", errors.New("the key is empty")
}
c.RLock()
defer c.RUnlock()
@ -494,10 +403,10 @@ func (c *IniConfigContainer) getdata(key string) string {
}
if v, ok := c.data[section]; ok {
if vv, ok := v[k]; ok {
return vv
return vv, nil
}
}
return ""
return "", errors.New(fmt.Sprintf("config not found: %s", key))
}
func init() {

View File

@ -109,9 +109,9 @@ password = ${GOPATH}
case bool:
value, err = iniconf.Bool(k)
case []string:
value = iniconf.Strings(k)
value, err = iniconf.Strings(k)
case string:
value = iniconf.String(k)
value, err = iniconf.String(k)
default:
value, err = iniconf.DIY(k)
}
@ -125,7 +125,8 @@ password = ${GOPATH}
if err = iniconf.Set("name", "astaxie"); err != nil {
t.Fatal(err)
}
if iniconf.String("name") != "astaxie" {
res, _ := iniconf.String("name")
if res != "astaxie" {
t.Fatal("get name error")
}

View File

@ -158,42 +158,14 @@ func (c *JSONConfigContainer) DefaultFloat(key string, defaultval float64) float
}
// String returns the string value for a given key.
func (c *JSONConfigContainer) String(key string) string {
func (c *JSONConfigContainer) String(key string) (string, error) {
val := c.getData(key)
if val != nil {
if v, ok := val.(string); ok {
return v
return v, nil
}
}
return ""
}
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultString(key string, defaultval string) string {
// TODO FIXME should not use "" to replace non existence
if v := c.String(key); v != "" {
return v
}
return defaultval
}
// Strings returns the []string value for a given key.
func (c *JSONConfigContainer) Strings(key string) []string {
stringVal := c.String(key)
if stringVal == "" {
return nil
}
return strings.Split(c.String(key), ";")
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); v != nil {
return v
}
return defaultval
return "", errors.New(fmt.Sprintf("config not found or is not string, key: %s", key))
}
// GetSection returns map for the given section

View File

@ -163,9 +163,9 @@ func TestJson(t *testing.T) {
case bool:
value, err = jsonconf.Bool(k)
case []string:
value = jsonconf.Strings(k)
value, err = jsonconf.Strings(k)
case string:
value = jsonconf.String(k)
value, err = jsonconf.String(k)
default:
value, err = jsonconf.DIY(k)
}
@ -179,7 +179,9 @@ func TestJson(t *testing.T) {
if err = jsonconf.Set("name", "astaxie"); err != nil {
t.Fatal(err)
}
if jsonconf.String("name") != "astaxie" {
res, _ := jsonconf.String("name")
if res != "astaxie" {
t.Fatal("get name error")
}
@ -210,7 +212,7 @@ func TestJson(t *testing.T) {
t.Error("unknown keys should return an error when expecting an interface{}")
}
if val := jsonconf.String("unknown"); val != "" {
if val, _ := jsonconf.String("unknown"); val != "" {
t.Error("unknown keys should return an empty string when expecting a String")
}

View File

@ -26,7 +26,7 @@
//
// cnf, err := config.NewConfig("xml", "config.xml")
//
//More docs http://beego.me/docs/module/config.md
// More docs http://beego.me/docs/module/config.md
package xml
import (
@ -36,11 +36,11 @@ import (
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"github.com/astaxie/beego/pkg/infrastructure/config"
"github.com/beego/x2j"
"github.com/astaxie/beego/pkg/infrastructure/config"
)
// Config is a xml config parser and implements Config interface.
@ -144,37 +144,18 @@ func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
}
// String returns the string value for a given key.
func (c *ConfigContainer) String(key string) string {
func (c *ConfigContainer) String(key string) (string, error) {
if v, ok := c.data[key].(string); ok {
return v
return v, nil
}
return ""
return "", errors.New(fmt.Sprintf("configuration not found or not string, key: %s", key))
}
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
v := c.String(key)
if v == "" {
return defaultval
}
return v
}
// Strings returns the []string value for a given key.
func (c *ConfigContainer) Strings(key string) []string {
v := c.String(key)
if v == "" {
return nil
}
return strings.Split(v, ";")
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v := c.Strings(key)
if v == nil {
v, err := c.String(key)
if v == "" || err != nil {
return defaultval
}
return v

View File

@ -25,7 +25,7 @@ import (
func TestXML(t *testing.T) {
var (
//xml parse should incluce in <config></config> tags
// xml parse should incluce in <config></config> tags
xmlcontext = `<?xml version="1.0" encoding="UTF-8"?>
<config>
<appname>beeapi</appname>
@ -102,9 +102,9 @@ func TestXML(t *testing.T) {
case bool:
value, err = xmlconf.Bool(k)
case []string:
value = xmlconf.Strings(k)
value, err = xmlconf.Strings(k)
case string:
value = xmlconf.String(k)
value, err = xmlconf.String(k)
default:
value, err = xmlconf.DIY(k)
}
@ -119,7 +119,9 @@ func TestXML(t *testing.T) {
if err = xmlconf.Set("name", "astaxie"); err != nil {
t.Fatal(err)
}
if xmlconf.String("name") != "astaxie" {
res, _ := xmlconf.String("name")
if res != "astaxie" {
t.Fatal("get name error")
}
}

View File

@ -26,7 +26,7 @@
//
// cnf, err := config.NewConfig("yaml", "config.yaml")
//
//More docs http://beego.me/docs/module/config.md
// More docs http://beego.me/docs/module/config.md
package yaml
import (
@ -40,8 +40,9 @@ import (
"strings"
"sync"
"github.com/astaxie/beego/pkg/infrastructure/config"
"github.com/beego/goyaml2"
"github.com/astaxie/beego/pkg/infrastructure/config"
)
// Config is a yaml config parser and implements Config interface.
@ -209,39 +210,41 @@ func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
}
// String returns the string value for a given key.
func (c *ConfigContainer) String(key string) string {
func (c *ConfigContainer) String(key string) (string, error) {
if v, err := c.getData(key); err == nil {
if vv, ok := v.(string); ok {
return vv
return vv, nil
} else {
return "", errors.New(fmt.Sprintf("the value is not string, key: %s, value: %v", key, v))
}
}
return ""
return "", errors.New(fmt.Sprintf("configuration not found, key: %s", key))
}
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
v := c.String(key)
if v == "" {
v, err := c.String(key)
if v == "" || err != nil {
return defaultval
}
return v
}
// Strings returns the []string value for a given key.
func (c *ConfigContainer) Strings(key string) []string {
v := c.String(key)
if v == "" {
return nil
func (c *ConfigContainer) Strings(key string) ([]string, error) {
v, err := c.String(key)
if v == "" || err != nil {
return nil, err
}
return strings.Split(v, ";")
return strings.Split(v, ";"), nil
}
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v := c.Strings(key)
if v == nil {
v, err := c.Strings(key)
if v == nil || err != nil {
return defaultval
}
return v

View File

@ -70,7 +70,8 @@ func TestYaml(t *testing.T) {
t.Fatal(err)
}
if yamlconf.String("appname") != "beeapi" {
res, _ := yamlconf.String("appname")
if res != "beeapi" {
t.Fatal("appname not equal to beeapi")
}
@ -91,9 +92,9 @@ func TestYaml(t *testing.T) {
case bool:
value, err = yamlconf.Bool(k)
case []string:
value = yamlconf.Strings(k)
value, err = yamlconf.Strings(k)
case string:
value = yamlconf.String(k)
value, err = yamlconf.String(k)
default:
value, err = yamlconf.DIY(k)
}
@ -108,7 +109,8 @@ func TestYaml(t *testing.T) {
if err = yamlconf.Set("name", "astaxie"); err != nil {
t.Fatal(err)
}
if yamlconf.String("name") != "astaxie" {
res, _ = yamlconf.String("name")
if res != "astaxie" {
t.Fatal("get name error")
}