Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
7e65338c
Commit
7e65338c
authored
Mar 29, 2016
by
ysqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change key format
key format : ${ENV_PART||defaultValue} or ${ENV_PART}
parent
5bd7d8c4
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
103 additions
and
91 deletions
+103
-91
config.go
config/config.go
+33
-28
config_test.go
config/config_test.go
+55
-0
ini.go
config/ini.go
+1
-1
ini_test.go
config/ini_test.go
+3
-15
json.go
config/json.go
+1
-1
json_test.go
config/json_test.go
+4
-16
xml.go
config/xml/xml.go
+1
-1
xml_test.go
config/xml/xml_test.go
+2
-14
yaml.go
config/yaml/yaml.go
+1
-1
yaml_test.go
config/yaml/yaml_test.go
+2
-14
No files found.
config/config.go
View file @
7e65338c
...
@@ -43,7 +43,6 @@ package config
...
@@ -43,7 +43,6 @@ package config
import
(
import
(
"fmt"
"fmt"
"os"
"os"
"strings"
)
)
// Configer defines how to get and set value from configuration raw data.
// Configer defines how to get and set value from configuration raw data.
...
@@ -107,17 +106,17 @@ func NewConfigData(adapterName string, data []byte) (Configer, error) {
...
@@ -107,17 +106,17 @@ func NewConfigData(adapterName string, data []byte) (Configer, error) {
return
adapter
.
ParseData
(
data
)
return
adapter
.
ParseData
(
data
)
}
}
//
ChooseRealValue
ForMap convert all string value with environment variable.
//
ExpandValueEnv
ForMap convert all string value with environment variable.
func
ChooseRealValue
ForMap
(
m
map
[
string
]
interface
{})
map
[
string
]
interface
{}
{
func
ExpandValueEnv
ForMap
(
m
map
[
string
]
interface
{})
map
[
string
]
interface
{}
{
for
k
,
v
:=
range
m
{
for
k
,
v
:=
range
m
{
switch
value
:=
v
.
(
type
)
{
switch
value
:=
v
.
(
type
)
{
case
string
:
case
string
:
m
[
k
]
=
ChooseRealValue
(
value
)
m
[
k
]
=
ExpandValueEnv
(
value
)
case
map
[
string
]
interface
{}
:
case
map
[
string
]
interface
{}
:
m
[
k
]
=
ChooseRealValue
ForMap
(
value
)
m
[
k
]
=
ExpandValueEnv
ForMap
(
value
)
case
map
[
string
]
string
:
case
map
[
string
]
string
:
for
k2
,
v2
:=
range
value
{
for
k2
,
v2
:=
range
value
{
value
[
k2
]
=
ChooseRealValue
(
v2
)
value
[
k2
]
=
ExpandValueEnv
(
v2
)
}
}
m
[
k
]
=
value
m
[
k
]
=
value
}
}
...
@@ -125,42 +124,48 @@ func ChooseRealValueForMap(m map[string]interface{}) map[string]interface{} {
...
@@ -125,42 +124,48 @@ func ChooseRealValueForMap(m map[string]interface{}) map[string]interface{} {
return
m
return
m
}
}
//
ChooseRealValue
returns value of convert with environment variable.
//
ExpandValueEnv
returns value of convert with environment variable.
//
//
// Return environment variable if value start with "$
$
".
// Return environment variable if value start with "$
{" and end with "}
".
// Return default value if environment variable is empty or not exist.
// Return default value if environment variable is empty or not exist.
//
//
// It accept value formats "$
$env" , "$$env||" , "$$env||defaultValue
" , "defaultvalue".
// It accept value formats "$
{env}" , "${env||}}" , "${env||defaultValue}
" , "defaultvalue".
// Examples:
// Examples:
// v1 := config.
ChooseRealValue("$$GOPATH
") // return the GOPATH environment variable.
// v1 := config.
ExpandValueEnv("${GOPATH}
") // return the GOPATH environment variable.
// v2 := config.
ChooseRealValue("$$GOAsta||/usr/local/go/
") // return the default value "/usr/local/go/".
// v2 := config.
ExpandValueEnv("${GOAsta||/usr/local/go}
") // return the default value "/usr/local/go/".
// v3 := config.
ChooseRealValue
("Astaxie") // return the value "Astaxie".
// v3 := config.
ExpandValueEnv
("Astaxie") // return the value "Astaxie".
func
ChooseRealValue
(
value
string
)
(
realValue
string
)
{
func
ExpandValueEnv
(
value
string
)
(
realValue
string
)
{
realValue
=
value
realValue
=
value
if
value
==
""
{
vLen
:=
len
(
value
)
// 3 = ${}
if
vLen
<
3
{
return
return
}
}
// Need start with "${" and end with "}", then return.
sign
:=
"$$"
// Environment variable identifier.
if
value
[
0
]
!=
'$'
||
value
[
1
]
!=
'{'
||
value
[
vLen
-
1
]
!=
'}'
{
sep
:=
"||"
// Environment variable and default value separator.
// Not use environment variable.
if
strings
.
HasPrefix
(
value
,
sign
)
==
false
{
return
return
}
}
sepIndex
:=
strings
.
Index
(
value
,
sep
)
key
:=
""
if
sepIndex
==
-
1
{
defalutV
:=
""
realValue
=
os
.
Getenv
(
string
(
value
[
len
(
sign
)
:
]))
// value start with "${"
}
else
{
for
i
:=
2
;
i
<
vLen
;
i
++
{
realValue
=
os
.
Getenv
(
string
(
value
[
len
(
sign
)
:
sepIndex
]))
if
value
[
i
]
==
'|'
&&
(
i
+
1
<
vLen
&&
value
[
i
+
1
]
==
'|'
)
{
// Find defalut value.
key
=
value
[
2
:
i
]
if
realValue
==
""
{
defalutV
=
value
[
i
+
2
:
vLen
-
1
]
// other string is default value.
realValue
=
string
(
value
[
sepIndex
+
len
(
sep
)
:
])
break
}
else
if
value
[
i
]
==
'}'
{
key
=
value
[
2
:
i
]
break
}
}
}
}
realValue
=
os
.
Getenv
(
key
)
if
realValue
==
""
{
realValue
=
defalutV
}
return
return
}
}
...
...
config/config_test.go
0 → 100644
View file @
7e65338c
// Copyright 2016 beego Author. All Rights Reserved.
//
// 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
config
import
(
"os"
"testing"
)
func
TestExpandValueEnv
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
item
string
want
string
}{
{
""
,
""
},
{
"$"
,
"$"
},
{
"{"
,
"{"
},
{
"{}"
,
"{}"
},
{
"${}"
,
""
},
{
"${|}"
,
""
},
{
"${}"
,
""
},
{
"${{}}"
,
""
},
{
"${{||}}"
,
"}"
},
{
"${pwd||}"
,
""
},
{
"${pwd||}"
,
""
},
{
"${pwd||}"
,
""
},
{
"${pwd||}}"
,
"}"
},
{
"${pwd||{{||}}}"
,
"{{||}}"
},
{
"${GOPATH}"
,
os
.
Getenv
(
"GOPATH"
)},
{
"${GOPATH||}"
,
os
.
Getenv
(
"GOPATH"
)},
{
"${GOPATH||root}"
,
os
.
Getenv
(
"GOPATH"
)},
{
"${GOPATH_NOT||root}"
,
"root"
},
{
"${GOPATH_NOT||||root}"
,
"||root"
},
}
for
_
,
c
:=
range
testCases
{
if
got
:=
ExpandValueEnv
(
c
.
item
);
got
!=
c
.
want
{
t
.
Errorf
(
"expand value error, item %q want %q, got %q"
,
c
.
item
,
c
.
want
,
got
)
}
}
}
config/ini.go
View file @
7e65338c
...
@@ -166,7 +166,7 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
...
@@ -166,7 +166,7 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
val
=
bytes
.
Trim
(
val
,
`"`
)
val
=
bytes
.
Trim
(
val
,
`"`
)
}
}
cfg
.
data
[
section
][
key
]
=
ChooseRealValue
(
string
(
val
))
cfg
.
data
[
section
][
key
]
=
ExpandValueEnv
(
string
(
val
))
if
comment
.
Len
()
>
0
{
if
comment
.
Len
()
>
0
{
cfg
.
keyComment
[
section
+
"."
+
key
]
=
comment
.
String
()
cfg
.
keyComment
[
section
+
"."
+
key
]
=
comment
.
String
()
comment
.
Reset
()
comment
.
Reset
()
...
...
config/ini_test.go
View file @
7e65338c
...
@@ -42,20 +42,14 @@ needlogin = ON
...
@@ -42,20 +42,14 @@ needlogin = ON
enableSession = Y
enableSession = Y
enableCookie = N
enableCookie = N
flag = 1
flag = 1
path1 = $$GOPATH
path1 = ${GOPATH}
path2 = $$GOPATH||/home/go
path2 = ${GOPATH||/home/go}
path3 = $$GOPATH$$GOPATH2||/home/go
token1 = $$TOKEN
token2 = $$TOKEN||
token3 = $$TOKEN||astaxie
token4 = token$$TOKEN
token5 = $$TOKEN$$TOKEN||TOKEN
[demo]
[demo]
key1="asta"
key1="asta"
key2 = "xie"
key2 = "xie"
CaseInsensitive = true
CaseInsensitive = true
peers = one;two;three
peers = one;two;three
password = $
$GOPATH
password = $
{GOPATH}
`
`
keyValue
=
map
[
string
]
interface
{}{
keyValue
=
map
[
string
]
interface
{}{
...
@@ -75,12 +69,6 @@ password = $$GOPATH
...
@@ -75,12 +69,6 @@ password = $$GOPATH
"flag"
:
true
,
"flag"
:
true
,
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path3"
:
"/home/go"
,
"token1"
:
""
,
"token2"
:
""
,
"token3"
:
"astaxie"
,
"token4"
:
"token$$TOKEN"
,
"token5"
:
"TOKEN"
,
"demo::key1"
:
"asta"
,
"demo::key1"
:
"asta"
,
"demo::key2"
:
"xie"
,
"demo::key2"
:
"xie"
,
"demo::CaseInsensitive"
:
true
,
"demo::CaseInsensitive"
:
true
,
...
...
config/json.go
View file @
7e65338c
...
@@ -58,7 +58,7 @@ func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
...
@@ -58,7 +58,7 @@ func (js *JSONConfig) ParseData(data []byte) (Configer, error) {
x
.
data
[
"rootArray"
]
=
wrappingArray
x
.
data
[
"rootArray"
]
=
wrappingArray
}
}
x
.
data
=
ChooseRealValue
ForMap
(
x
.
data
)
x
.
data
=
ExpandValueEnv
ForMap
(
x
.
data
)
return
x
,
nil
return
x
,
nil
}
}
...
...
config/json_test.go
View file @
7e65338c
...
@@ -86,25 +86,19 @@ func TestJson(t *testing.T) {
...
@@ -86,25 +86,19 @@ func TestJson(t *testing.T) {
"enableSession": "Y",
"enableSession": "Y",
"enableCookie": "N",
"enableCookie": "N",
"flag": 1,
"flag": 1,
"path1": "$$GOPATH",
"path1": "${GOPATH}",
"path2": "$$GOPATH||/home/go",
"path2": "${GOPATH||/home/go}",
"path3": "$$GOPATH$$GOPATH2||/home/go",
"token1": "$$TOKEN",
"token2": "$$TOKEN||",
"token3": "$$TOKEN||astaxie",
"token4": "token$$TOKEN",
"token5": "$$TOKEN$$TOKEN||TOKEN",
"database": {
"database": {
"host": "host",
"host": "host",
"port": "port",
"port": "port",
"database": "database",
"database": "database",
"username": "username",
"username": "username",
"password": "$
$GOPATH
",
"password": "$
{GOPATH}
",
"conns":{
"conns":{
"maxconnection":12,
"maxconnection":12,
"autoconnect":true,
"autoconnect":true,
"connectioninfo":"info",
"connectioninfo":"info",
"root": "$
$GOPATH
"
"root": "$
{GOPATH}
"
}
}
}
}
}`
}`
...
@@ -126,12 +120,6 @@ func TestJson(t *testing.T) {
...
@@ -126,12 +120,6 @@ func TestJson(t *testing.T) {
"flag"
:
true
,
"flag"
:
true
,
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path3"
:
"/home/go"
,
"token1"
:
""
,
"token2"
:
""
,
"token3"
:
"astaxie"
,
"token4"
:
"token$$TOKEN"
,
"token5"
:
"TOKEN"
,
"database::host"
:
"host"
,
"database::host"
:
"host"
,
"database::port"
:
"port"
,
"database::port"
:
"port"
,
"database::database"
:
"database"
,
"database::database"
:
"database"
,
...
...
config/xml/xml.go
View file @
7e65338c
...
@@ -69,7 +69,7 @@ func (xc *Config) Parse(filename string) (config.Configer, error) {
...
@@ -69,7 +69,7 @@ func (xc *Config) Parse(filename string) (config.Configer, error) {
return
nil
,
err
return
nil
,
err
}
}
x
.
data
=
config
.
ChooseRealValue
ForMap
(
d
[
"config"
]
.
(
map
[
string
]
interface
{}))
x
.
data
=
config
.
ExpandValueEnv
ForMap
(
d
[
"config"
]
.
(
map
[
string
]
interface
{}))
return
x
,
nil
return
x
,
nil
}
}
...
...
config/xml/xml_test.go
View file @
7e65338c
...
@@ -35,14 +35,8 @@ func TestXML(t *testing.T) {
...
@@ -35,14 +35,8 @@ func TestXML(t *testing.T) {
<runmode>dev</runmode>
<runmode>dev</runmode>
<autorender>false</autorender>
<autorender>false</autorender>
<copyrequestbody>true</copyrequestbody>
<copyrequestbody>true</copyrequestbody>
<path1>$$GOPATH</path1>
<path1>${GOPATH}</path1>
<path2>$$GOPATH||/home/go</path2>
<path2>${GOPATH||/home/go}</path2>
<path3>$$GOPATH$$GOPATH2||/home/go</path3>
<token1>$$TOKEN</token1>
<token2>$$TOKEN||</token2>
<token3>$$TOKEN||astaxie</token3>
<token4>token$$TOKEN</token4>
<token5>$$TOKEN$$TOKEN||TOKEN</token5>
</config>
</config>
`
`
keyValue
=
map
[
string
]
interface
{}{
keyValue
=
map
[
string
]
interface
{}{
...
@@ -55,12 +49,6 @@ func TestXML(t *testing.T) {
...
@@ -55,12 +49,6 @@ func TestXML(t *testing.T) {
"copyrequestbody"
:
true
,
"copyrequestbody"
:
true
,
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path3"
:
"/home/go"
,
"token1"
:
""
,
"token2"
:
""
,
"token3"
:
"astaxie"
,
"token4"
:
"token$$TOKEN"
,
"token5"
:
"TOKEN"
,
"error"
:
""
,
"error"
:
""
,
"emptystrings"
:
[]
string
{},
"emptystrings"
:
[]
string
{},
}
}
...
...
config/yaml/yaml.go
View file @
7e65338c
...
@@ -110,7 +110,7 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
...
@@ -110,7 +110,7 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
log
.
Println
(
"Not a Map? >> "
,
string
(
buf
),
data
)
log
.
Println
(
"Not a Map? >> "
,
string
(
buf
),
data
)
cnf
=
nil
cnf
=
nil
}
}
cnf
=
config
.
ChooseRealValue
ForMap
(
cnf
)
cnf
=
config
.
ExpandValueEnv
ForMap
(
cnf
)
return
return
}
}
...
...
config/yaml/yaml_test.go
View file @
7e65338c
...
@@ -34,14 +34,8 @@ func TestYaml(t *testing.T) {
...
@@ -34,14 +34,8 @@ func TestYaml(t *testing.T) {
"autorender": false
"autorender": false
"copyrequestbody": true
"copyrequestbody": true
"PATH": GOPATH
"PATH": GOPATH
"path1": $$GOPATH
"path1": ${GOPATH}
"path2": $$GOPATH||/home/go
"path2": ${GOPATH||/home/go}
"path3": $$GOPATH$$GOPATH2||/home/go
"token1": $$TOKEN
"token2": $$TOKEN||
"token3": $$TOKEN||astaxie
"token4": token$$TOKEN
"token5": $$TOKEN$$TOKEN||TOKEN
"empty": ""
"empty": ""
`
`
...
@@ -56,12 +50,6 @@ func TestYaml(t *testing.T) {
...
@@ -56,12 +50,6 @@ func TestYaml(t *testing.T) {
"PATH"
:
"GOPATH"
,
"PATH"
:
"GOPATH"
,
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path1"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path2"
:
os
.
Getenv
(
"GOPATH"
),
"path3"
:
"/home/go"
,
"token1"
:
""
,
"token2"
:
""
,
"token3"
:
"astaxie"
,
"token4"
:
"token$$TOKEN"
,
"token5"
:
"TOKEN"
,
"error"
:
""
,
"error"
:
""
,
"emptystrings"
:
[]
string
{},
"emptystrings"
:
[]
string
{},
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment