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"
|
2013-08-21 16:07:33 +00:00
|
|
|
"os"
|
|
|
|
"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 TestJsonStartsWithArray(t *testing.T) {
|
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
|
|
|
const jsoncontextwitharray = `[
|
2014-06-11 08:33:32 +00:00
|
|
|
{
|
|
|
|
"url": "user",
|
|
|
|
"serviceAPI": "http://www.test.com/user"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"url": "employee",
|
|
|
|
"serviceAPI": "http://www.test.com/employee"
|
|
|
|
}
|
|
|
|
]`
|
|
|
|
f, err := os.Create("testjsonWithArray.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(jsoncontextwitharray)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
defer os.Remove("testjsonWithArray.conf")
|
|
|
|
jsonconf, err := NewConfig("json", "testjsonWithArray.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rootArray, err := jsonconf.DIY("rootArray")
|
2014-06-25 02:39:37 +00:00
|
|
|
if err != nil {
|
2014-06-11 08:33:32 +00:00
|
|
|
t.Error("array does not exist as element")
|
|
|
|
}
|
|
|
|
rootArrayCasted := rootArray.([]interface{})
|
2014-06-25 02:39:37 +00:00
|
|
|
if rootArrayCasted == nil {
|
2014-06-11 08:33:32 +00:00
|
|
|
t.Error("array from root is nil")
|
2014-06-25 02:39:37 +00:00
|
|
|
} else {
|
2014-06-11 08:33:32 +00:00
|
|
|
elem := rootArrayCasted[0].(map[string]interface{})
|
|
|
|
if elem["url"] != "user" || elem["serviceAPI"] != "http://www.test.com/user" {
|
|
|
|
t.Error("array[0] values are not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
elem2 := rootArrayCasted[1].(map[string]interface{})
|
|
|
|
if elem2["url"] != "employee" || elem2["serviceAPI"] != "http://www.test.com/employee" {
|
|
|
|
t.Error("array[1] values are not valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
func TestJson(t *testing.T) {
|
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
|
|
|
|
|
|
|
var (
|
|
|
|
jsoncontext = `{
|
|
|
|
"appname": "beeapi",
|
|
|
|
"testnames": "foo;bar",
|
|
|
|
"httpport": 8080,
|
|
|
|
"mysqlport": 3600,
|
|
|
|
"PI": 3.1415976,
|
|
|
|
"runmode": "dev",
|
|
|
|
"autorender": false,
|
|
|
|
"copyrequestbody": true,
|
|
|
|
"session": "on",
|
|
|
|
"cookieon": "off",
|
|
|
|
"newreg": "OFF",
|
|
|
|
"needlogin": "ON",
|
|
|
|
"enableSession": "Y",
|
|
|
|
"enableCookie": "N",
|
|
|
|
"flag": 1,
|
|
|
|
"database": {
|
|
|
|
"host": "host",
|
|
|
|
"port": "port",
|
|
|
|
"database": "database",
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"conns":{
|
|
|
|
"maxconnection":12,
|
|
|
|
"autoconnect":true,
|
|
|
|
"connectioninfo":"info"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
keyValue = map[string]interface{}{
|
|
|
|
"appname": "beeapi",
|
|
|
|
"testnames": []string{"foo", "bar"},
|
|
|
|
"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,
|
|
|
|
"database::host": "host",
|
|
|
|
"database::port": "port",
|
|
|
|
"database::database": "database",
|
|
|
|
"database::password": "password",
|
|
|
|
"database::conns::maxconnection": 12,
|
|
|
|
"database::conns::autoconnect": true,
|
|
|
|
"database::conns::connectioninfo": "info",
|
|
|
|
"unknown": "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-08-21 16:07:33 +00:00
|
|
|
f, err := os.Create("testjson.conf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(jsoncontext)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
defer os.Remove("testjson.conf")
|
|
|
|
jsonconf, err := NewConfig("json", "testjson.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 = jsonconf.Int(k)
|
|
|
|
case int64:
|
|
|
|
value, err = jsonconf.Int64(k)
|
|
|
|
case float64:
|
|
|
|
value, err = jsonconf.Float(k)
|
|
|
|
case bool:
|
|
|
|
value, err = jsonconf.Bool(k)
|
|
|
|
case []string:
|
|
|
|
value = jsonconf.Strings(k)
|
|
|
|
case string:
|
|
|
|
value = jsonconf.String(k)
|
|
|
|
default:
|
|
|
|
value, err = jsonconf.DIY(k)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-01-25 13:33:57 +00:00
|
|
|
t.Fatalf("get key %q value fatal,%v err %s", k, v, 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
|
|
|
} 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 = jsonconf.Set("name", "astaxie"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if jsonconf.String("name") != "astaxie" {
|
|
|
|
t.Fatal("get name error")
|
|
|
|
}
|
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-11-11 13:25:03 +00:00
|
|
|
if db, err := jsonconf.DIY("database"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if m, ok := db.(map[string]interface{}); !ok {
|
2013-12-10 10:09:58 +00:00
|
|
|
t.Log(db)
|
2013-11-11 13:25:03 +00:00
|
|
|
t.Fatal("db not map[string]interface{}")
|
|
|
|
} else {
|
|
|
|
if m["host"].(string) != "host" {
|
|
|
|
t.Fatal("get host err")
|
|
|
|
}
|
|
|
|
}
|
2014-05-31 04:48:23 +00:00
|
|
|
|
|
|
|
if _, err := jsonconf.Int("unknown"); err == nil {
|
|
|
|
t.Error("unknown keys should return an error when expecting an Int")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := jsonconf.Int64("unknown"); err == nil {
|
|
|
|
t.Error("unknown keys should return an error when expecting an Int64")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := jsonconf.Float("unknown"); err == nil {
|
|
|
|
t.Error("unknown keys should return an error when expecting a Float")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := jsonconf.DIY("unknown"); err == nil {
|
|
|
|
t.Error("unknown keys should return an error when expecting an interface{}")
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := jsonconf.String("unknown"); val != "" {
|
|
|
|
t.Error("unknown keys should return an empty string when expecting a String")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := jsonconf.Bool("unknown"); err == nil {
|
|
|
|
t.Error("unknown keys should return an error when expecting a Bool")
|
|
|
|
}
|
2014-12-17 09:02:46 +00:00
|
|
|
|
|
|
|
if !jsonconf.DefaultBool("unknow", true) {
|
|
|
|
t.Error("unknown keys with default value wrong")
|
|
|
|
}
|
2013-08-21 16:07:33 +00:00
|
|
|
}
|