2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// 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
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
"fmt"
|
2016-01-23 06:53:52 +00:00
|
|
|
"io/ioutil"
|
2013-08-21 16:07:33 +00:00
|
|
|
"os"
|
2016-01-24 03:37:43 +00:00
|
|
|
"strings"
|
2013-08-21 16:07:33 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
func TestIni(t *testing.T) {
|
|
|
|
|
|
|
|
var (
|
|
|
|
inicontext = `
|
2013-11-27 15:55:26 +00:00
|
|
|
;comment one
|
|
|
|
#comment two
|
2013-08-21 16:07:33 +00:00
|
|
|
appname = beeapi
|
|
|
|
httpport = 8080
|
|
|
|
mysqlport = 3600
|
|
|
|
PI = 3.1415976
|
|
|
|
runmode = "dev"
|
|
|
|
autorender = false
|
|
|
|
copyrequestbody = true
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
session= on
|
|
|
|
cookieon= off
|
|
|
|
newreg = OFF
|
|
|
|
needlogin = ON
|
|
|
|
enableSession = Y
|
|
|
|
enableCookie = N
|
|
|
|
flag = 1
|
2016-03-29 13:47:33 +00:00
|
|
|
path1 = ${GOPATH}
|
|
|
|
path2 = ${GOPATH||/home/go}
|
2013-11-27 15:55:26 +00:00
|
|
|
[demo]
|
|
|
|
key1="asta"
|
|
|
|
key2 = "xie"
|
2013-11-29 02:17:35 +00:00
|
|
|
CaseInsensitive = true
|
2014-01-15 09:19:03 +00:00
|
|
|
peers = one;two;three
|
2016-03-29 13:47:33 +00:00
|
|
|
password = ${GOPATH}
|
2013-08-21 16:07:33 +00:00
|
|
|
`
|
|
|
|
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
keyValue = map[string]interface{}{
|
|
|
|
"appname": "beeapi",
|
|
|
|
"httpport": 8080,
|
|
|
|
"mysqlport": int64(3600),
|
|
|
|
"pi": 3.1415976,
|
|
|
|
"runmode": "dev",
|
|
|
|
"autorender": false,
|
|
|
|
"copyrequestbody": true,
|
|
|
|
"session": true,
|
|
|
|
"cookieon": false,
|
|
|
|
"newreg": false,
|
|
|
|
"needlogin": true,
|
|
|
|
"enableSession": true,
|
|
|
|
"enableCookie": false,
|
|
|
|
"flag": true,
|
2016-03-14 11:22:00 +00:00
|
|
|
"path1": os.Getenv("GOPATH"),
|
|
|
|
"path2": os.Getenv("GOPATH"),
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
"demo::key1": "asta",
|
|
|
|
"demo::key2": "xie",
|
|
|
|
"demo::CaseInsensitive": true,
|
|
|
|
"demo::peers": []string{"one", "two", "three"},
|
2016-03-14 11:22:00 +00:00
|
|
|
"demo::password": os.Getenv("GOPATH"),
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
"null": "",
|
|
|
|
"demo2::key1": "",
|
|
|
|
"error": "",
|
2016-03-02 14:44:20 +00:00
|
|
|
"emptystrings": []string{},
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
f, err := os.Create("testini.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(inicontext)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
defer os.Remove("testini.conf")
|
|
|
|
iniconf, err := NewConfig("ini", "testini.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
Support Parse Bool with more diffrent values
ParseBool returns the boolean value represented by the string.
It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on,
On,
0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
Any other value returns an error.
2016-01-23 03:02:40 +00:00
|
|
|
for k, v := range keyValue {
|
|
|
|
var err error
|
|
|
|
var value interface{}
|
|
|
|
switch v.(type) {
|
|
|
|
case int:
|
|
|
|
value, err = iniconf.Int(k)
|
|
|
|
case int64:
|
|
|
|
value, err = iniconf.Int64(k)
|
|
|
|
case float64:
|
|
|
|
value, err = iniconf.Float(k)
|
|
|
|
case bool:
|
|
|
|
value, err = iniconf.Bool(k)
|
|
|
|
case []string:
|
|
|
|
value = iniconf.Strings(k)
|
|
|
|
case string:
|
|
|
|
value = iniconf.String(k)
|
|
|
|
default:
|
|
|
|
value, err = iniconf.DIY(k)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get key %q value fail,err %s", k, err)
|
|
|
|
} else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
|
|
|
|
t.Fatalf("get key %q value, want %v got %v .", k, v, value)
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
}
|
|
|
|
if err = iniconf.Set("name", "astaxie"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if iniconf.String("name") != "astaxie" {
|
|
|
|
t.Fatal("get name error")
|
|
|
|
}
|
2014-01-15 09:19:03 +00:00
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
}
|
2016-01-23 06:53:52 +00:00
|
|
|
|
|
|
|
func TestIniSave(t *testing.T) {
|
|
|
|
|
|
|
|
const (
|
|
|
|
inicontext = `
|
|
|
|
app = app
|
|
|
|
;comment one
|
|
|
|
#comment two
|
|
|
|
# comment three
|
|
|
|
appname = beeapi
|
|
|
|
httpport = 8080
|
|
|
|
# DB Info
|
|
|
|
# enable db
|
|
|
|
[dbinfo]
|
|
|
|
# db type name
|
|
|
|
# suport mysql,sqlserver
|
|
|
|
name = mysql
|
|
|
|
`
|
|
|
|
|
2016-01-24 03:37:43 +00:00
|
|
|
saveResult = `
|
|
|
|
app=app
|
2016-01-23 06:53:52 +00:00
|
|
|
#comment one
|
|
|
|
#comment two
|
|
|
|
# comment three
|
|
|
|
appname=beeapi
|
|
|
|
httpport=8080
|
|
|
|
|
|
|
|
# DB Info
|
|
|
|
# enable db
|
|
|
|
[dbinfo]
|
|
|
|
# db type name
|
|
|
|
# suport mysql,sqlserver
|
|
|
|
name=mysql
|
|
|
|
`
|
|
|
|
)
|
|
|
|
cfg, err := NewConfigData("ini", []byte(inicontext))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
name := "newIniConfig.ini"
|
|
|
|
if err := cfg.SaveConfigFile(name); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(name)
|
|
|
|
|
|
|
|
if data, err := ioutil.ReadFile(name); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-01-24 03:37:43 +00:00
|
|
|
} else {
|
|
|
|
cfgData := string(data)
|
|
|
|
datas := strings.Split(saveResult, "\n")
|
|
|
|
for _, line := range datas {
|
|
|
|
if strings.Contains(cfgData, line+"\n") == false {
|
|
|
|
t.Fatalf("different after save ini config file. need contains %q", line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 06:53:52 +00:00
|
|
|
}
|
|
|
|
}
|