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

uing pkg module

This commit is contained in:
Ming Deng
2020-07-29 21:56:19 +08:00
parent 7312197732
commit aa06a10493
75 changed files with 192 additions and 181 deletions

View File

@ -21,7 +21,7 @@ import (
"os"
"strings"
"github.com/astaxie/beego/utils"
"github.com/astaxie/beego/pkg/utils"
)
var env *utils.BeeMap

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package config
package ini
import (
"bufio"
@ -26,6 +26,8 @@ import (
"strconv"
"strings"
"sync"
"github.com/astaxie/beego/pkg/config"
)
var (
@ -45,7 +47,7 @@ type IniConfig struct {
}
// Parse creates a new Config and parses the file configuration from the named file.
func (ini *IniConfig) Parse(name string) (Configer, error) {
func (ini *IniConfig) Parse(name string) (config.Configer, error) {
return ini.parseFile(name)
}
@ -195,7 +197,7 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
val = bytes.Trim(val, `"`)
}
cfg.data[section][key] = ExpandValueEnv(string(val))
cfg.data[section][key] = config.ExpandValueEnv(string(val))
if comment.Len() > 0 {
cfg.keyComment[section+"."+key] = comment.String()
comment.Reset()
@ -208,7 +210,7 @@ func (ini *IniConfig) parseData(dir string, data []byte) (*IniConfigContainer, e
// ParseData parse ini the data
// When include other.conf,other.conf is either absolute directory
// or under beego in default temporary directory(/tmp/beego[-username]).
func (ini *IniConfig) ParseData(data []byte) (Configer, error) {
func (ini *IniConfig) ParseData(data []byte) (config.Configer, error) {
dir := "beego"
currentUser, err := user.Current()
if err == nil {
@ -233,7 +235,7 @@ type IniConfigContainer struct {
// Bool returns the boolean value for a given key.
func (c *IniConfigContainer) Bool(key string) (bool, error) {
return ParseBool(c.getdata(key))
return config.ParseBool(c.getdata(key))
}
// DefaultBool returns the boolean value for a given key.
@ -500,5 +502,5 @@ func (c *IniConfigContainer) getdata(key string) string {
}
func init() {
Register("ini", &IniConfig{})
config.Register("ini", &IniConfig{})
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package config
package ini
import (
"fmt"
@ -20,6 +20,8 @@ import (
"os"
"strings"
"testing"
"github.com/astaxie/beego/pkg/config"
)
func TestIni(t *testing.T) {
@ -92,7 +94,7 @@ password = ${GOPATH}
}
f.Close()
defer os.Remove("testini.conf")
iniconf, err := NewConfig("ini", "testini.conf")
iniconf, err := config.NewConfig("ini", "testini.conf")
if err != nil {
t.Fatal(err)
}
@ -165,7 +167,7 @@ httpport=8080
name=mysql
`
)
cfg, err := NewConfigData("ini", []byte(inicontext))
cfg, err := config.NewConfigData("ini", []byte(inicontext))
if err != nil {
t.Fatal(err)
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package config
package json
import (
"encoding/json"
@ -23,6 +23,8 @@ import (
"strconv"
"strings"
"sync"
"github.com/astaxie/beego/pkg/config"
)
// JSONConfig is a json config parser and implements Config interface.
@ -30,7 +32,7 @@ type JSONConfig struct {
}
// Parse returns a ConfigContainer with parsed json config map.
func (js *JSONConfig) Parse(filename string) (Configer, error) {
func (js *JSONConfig) Parse(filename string) (config.Configer, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
@ -45,7 +47,7 @@ func (js *JSONConfig) Parse(filename string) (Configer, error) {
}
// ParseData returns a ConfigContainer with json string
func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
func (js *JSONConfig) ParseData(data []byte) (config.Configer, error) {
x := &JSONConfigContainer{
data: make(map[string]interface{}),
}
@ -59,7 +61,7 @@ func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
x.data["rootArray"] = wrappingArray
}
x.data = ExpandValueEnvForMap(x.data)
x.data = config.ExpandValueEnvForMap(x.data)
return x, nil
}
@ -75,7 +77,7 @@ type JSONConfigContainer struct {
func (c *JSONConfigContainer) Bool(key string) (bool, error) {
val := c.getData(key)
if val != nil {
return ParseBool(val)
return config.ParseBool(val)
}
return false, fmt.Errorf("not exist key: %q", key)
}
@ -265,5 +267,5 @@ func (c *JSONConfigContainer) getData(key string) interface{} {
}
func init() {
Register("json", &JSONConfig{})
config.Register("json", &JSONConfig{})
}

View File

@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package config
package json
import (
"fmt"
"os"
"testing"
"github.com/astaxie/beego/pkg/config"
)
func TestJsonStartsWithArray(t *testing.T) {
@ -43,7 +45,7 @@ func TestJsonStartsWithArray(t *testing.T) {
}
f.Close()
defer os.Remove("testjsonWithArray.conf")
jsonconf, err := NewConfig("json", "testjsonWithArray.conf")
jsonconf, err := config.NewConfig("json", "testjsonWithArray.conf")
if err != nil {
t.Fatal(err)
}
@ -143,7 +145,7 @@ func TestJson(t *testing.T) {
}
f.Close()
defer os.Remove("testjson.conf")
jsonconf, err := NewConfig("json", "testjson.conf")
jsonconf, err := config.NewConfig("json", "testjson.conf")
if err != nil {
t.Fatal(err)
}

View File

@ -39,7 +39,7 @@ import (
"strings"
"sync"
"github.com/astaxie/beego/config"
"github.com/astaxie/beego/pkg/config"
"github.com/beego/x2j"
)

View File

@ -19,7 +19,7 @@ import (
"os"
"testing"
"github.com/astaxie/beego/config"
"github.com/astaxie/beego/pkg/config"
)
func TestXML(t *testing.T) {

View File

@ -40,7 +40,7 @@ import (
"strings"
"sync"
"github.com/astaxie/beego/config"
"github.com/astaxie/beego/pkg/config"
"github.com/beego/goyaml2"
)

View File

@ -19,7 +19,7 @@ import (
"os"
"testing"
"github.com/astaxie/beego/config"
"github.com/astaxie/beego/pkg/config"
)
func TestYaml(t *testing.T) {