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
673993fa
Commit
673993fa
authored
Dec 24, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #412 from fuxiaohei/master
add comment in config package.
parents
d0e2c5c6
6f3803ce
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
74 additions
and
27 deletions
+74
-27
file.go
cache/file.go
+6
-4
memory.go
cache/memory.go
+2
-1
config.go
config/config.go
+6
-4
ini.go
config/ini.go
+18
-13
json.go
config/json.go
+18
-2
xml.go
config/xml.go
+12
-2
yaml.go
config/yaml.go
+12
-1
No files found.
cache/file.go
View file @
673993fa
...
...
@@ -47,10 +47,11 @@ type FileCache struct {
EmbedExpiry
int
}
// Create new file cache with
default directory and suffix
.
// Create new file cache with
no config
.
// the level and expiry need set in method StartAndGC as config string.
func
NewFileCache
()
*
FileCache
{
return
&
FileCache
{
CachePath
:
FileCachePath
,
FileSuffix
:
FileCacheFileSuffix
}
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
return
&
FileCache
{}
}
// Start and begin gc for file cache.
...
...
@@ -142,13 +143,14 @@ func (this *FileCache) Get(key string) interface{} {
}
// Put value into file cache.
// timeout means how long to keep this file, unit of second.
// timeout means how long to keep this file, unit of ms.
// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever.
func
(
this
*
FileCache
)
Put
(
key
string
,
val
interface
{},
timeout
int64
)
error
{
filename
:=
this
.
getCacheFileName
(
key
)
var
item
FileCacheItem
item
.
Data
=
val
if
timeout
==
FileCacheEmbedExpiry
{
item
.
Expired
=
time
.
Now
()
.
Unix
()
+
(
86400
*
365
*
10
)
//
10年
item
.
Expired
=
time
.
Now
()
.
Unix
()
+
(
86400
*
365
*
10
)
//
ten years
}
else
{
item
.
Expired
=
time
.
Now
()
.
Unix
()
+
timeout
}
...
...
cache/memory.go
View file @
673993fa
...
...
@@ -26,7 +26,7 @@ type MemoryCache struct {
lock
sync
.
RWMutex
dur
time
.
Duration
items
map
[
string
]
*
MemoryItem
Every
int
// run an expiration check Every cloc
;
time
Every
int
// run an expiration check Every cloc
k
time
}
// NewMemoryCache returns a new MemoryCache.
...
...
@@ -52,6 +52,7 @@ func (bc *MemoryCache) Get(name string) interface{} {
}
// Put cache to memory.
// if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute).
func
(
bc
*
MemoryCache
)
Put
(
name
string
,
value
interface
{},
expired
int64
)
error
{
bc
.
lock
.
Lock
()
defer
bc
.
lock
.
Unlock
()
...
...
config/config.go
View file @
673993fa
...
...
@@ -4,9 +4,10 @@ import (
"fmt"
)
// ConfigContainer defines how to get and set value from configuration raw data.
type
ConfigContainer
interface
{
Set
(
key
,
val
string
)
error
String
(
key
string
)
string
Set
(
key
,
val
string
)
error
// support section::key type in given key when using ini type.
String
(
key
string
)
string
// support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
Int
(
key
string
)
(
int
,
error
)
Int64
(
key
string
)
(
int64
,
error
)
Bool
(
key
string
)
(
bool
,
error
)
...
...
@@ -14,6 +15,7 @@ type ConfigContainer interface {
DIY
(
key
string
)
(
interface
{},
error
)
}
// Config is the adapter interface for parsing config file to get raw data to ConfigContainer.
type
Config
interface
{
Parse
(
key
string
)
(
ConfigContainer
,
error
)
}
...
...
@@ -33,8 +35,8 @@ func Register(name string, adapter Config) {
adapters
[
name
]
=
adapter
}
// adapterName
r is ini/json/xml/yaml
// filename is the config file path
// adapterName
is ini/json/xml/yaml.
// filename is the config file path
.
func
NewConfig
(
adapterName
,
fileaname
string
)
(
ConfigContainer
,
error
)
{
adapter
,
ok
:=
adapters
[
adapterName
]
if
!
ok
{
...
...
config/ini.go
View file @
673993fa
...
...
@@ -13,21 +13,21 @@ import (
)
var
(
DEFAULT_SECTION
=
"default"
bNumComment
=
[]
byte
{
'#'
}
// number sign
bSemComment
=
[]
byte
{
';'
}
// semicolon
DEFAULT_SECTION
=
"default"
// default section means if some ini items not in a section, make them in default section,
bNumComment
=
[]
byte
{
'#'
}
// number sign
al
bSemComment
=
[]
byte
{
';'
}
// semicolon
signal
bEmpty
=
[]
byte
{}
bEqual
=
[]
byte
{
'='
}
bDQuote
=
[]
byte
{
'"'
}
sectionStart
=
[]
byte
{
'['
}
sectionEnd
=
[]
byte
{
']'
}
bEqual
=
[]
byte
{
'='
}
// equal signal
bDQuote
=
[]
byte
{
'"'
}
// quote signal
sectionStart
=
[]
byte
{
'['
}
// section start signal
sectionEnd
=
[]
byte
{
']'
}
// section end signal
)
// IniConfig implements Config to parse ini file.
type
IniConfig
struct
{
}
// ParseFile creates a new Config and parses the file configuration from the
// named file.
// ParseFile creates a new Config and parses the file configuration from the named file.
func
(
ini
*
IniConfig
)
Parse
(
name
string
)
(
ConfigContainer
,
error
)
{
file
,
err
:=
os
.
Open
(
name
)
if
err
!=
nil
{
...
...
@@ -106,11 +106,12 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
return
cfg
,
nil
}
// A Config represents the configuration.
// A Config represents the ini configuration.
// When set and get value, support key as section:name type.
type
IniConfigContainer
struct
{
filename
string
data
map
[
string
]
map
[
string
]
string
//section=> key:val
sectionComment
map
[
string
]
string
//
s
ction : comment
data
map
[
string
]
map
[
string
]
string
//
section=> key:val
sectionComment
map
[
string
]
string
//
se
ction : comment
keycomment
map
[
string
]
string
// id: []{comment, key...}; id 1 is for main comment.
sync
.
RWMutex
}
...
...
@@ -127,6 +128,7 @@ func (c *IniConfigContainer) Int(key string) (int, error) {
return
strconv
.
Atoi
(
c
.
getdata
(
key
))
}
// Int64 returns the int64 value for a given key.
func
(
c
*
IniConfigContainer
)
Int64
(
key
string
)
(
int64
,
error
)
{
key
=
strings
.
ToLower
(
key
)
return
strconv
.
ParseInt
(
c
.
getdata
(
key
),
10
,
64
)
...
...
@@ -145,6 +147,8 @@ func (c *IniConfigContainer) String(key string) string {
}
// WriteValue writes a new value for key.
// if write to one section, the key need be "section::key".
// if the section is not existed, it panics.
func
(
c
*
IniConfigContainer
)
Set
(
key
,
value
string
)
error
{
c
.
Lock
()
defer
c
.
Unlock
()
...
...
@@ -169,6 +173,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
return
nil
}
// DIY returns the raw value by a given key.
func
(
c
*
IniConfigContainer
)
DIY
(
key
string
)
(
v
interface
{},
err
error
)
{
key
=
strings
.
ToLower
(
key
)
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
...
...
@@ -177,7 +182,7 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
return
v
,
errors
.
New
(
"key not find"
)
}
//section.key or key
//
section.key or key
func
(
c
*
IniConfigContainer
)
getdata
(
key
string
)
string
{
c
.
RLock
()
defer
c
.
RUnlock
()
...
...
config/json.go
View file @
673993fa
...
...
@@ -9,9 +9,11 @@ import (
"sync"
)
// JsonConfig is a json config parser and implements Config interface.
type
JsonConfig
struct
{
}
// Parse returns a ConfigContainer with parsed json config map.
func
(
js
*
JsonConfig
)
Parse
(
filename
string
)
(
ConfigContainer
,
error
)
{
file
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
...
...
@@ -32,11 +34,14 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
return
x
,
nil
}
// A Config represents the json configuration.
// Only when get value, support key as section:name type.
type
JsonConfigContainer
struct
{
data
map
[
string
]
interface
{}
sync
.
RWMutex
}
// Bool returns the boolean value for a given key.
func
(
c
*
JsonConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
...
...
@@ -48,9 +53,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
}
else
{
return
false
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
false
,
nil
}
// Int returns the integer value for a given key.
func
(
c
*
JsonConfigContainer
)
Int
(
key
string
)
(
int
,
error
)
{
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
...
...
@@ -62,8 +68,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
}
else
{
return
0
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
0
,
nil
}
// Int64 returns the int64 value for a given key.
func
(
c
*
JsonConfigContainer
)
Int64
(
key
string
)
(
int64
,
error
)
{
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
...
...
@@ -75,8 +83,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
}
else
{
return
0
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
0
,
nil
}
// Float returns the float value for a given key.
func
(
c
*
JsonConfigContainer
)
Float
(
key
string
)
(
float64
,
error
)
{
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
...
...
@@ -88,8 +98,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
}
else
{
return
0.0
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
0.0
,
nil
}
// String returns the string value for a given key.
func
(
c
*
JsonConfigContainer
)
String
(
key
string
)
string
{
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
...
...
@@ -101,8 +113,10 @@ func (c *JsonConfigContainer) String(key string) string {
}
else
{
return
""
}
return
""
}
// WriteValue writes a new value for key.
func
(
c
*
JsonConfigContainer
)
Set
(
key
,
val
string
)
error
{
c
.
Lock
()
defer
c
.
Unlock
()
...
...
@@ -110,6 +124,7 @@ func (c *JsonConfigContainer) Set(key, val string) error {
return
nil
}
// DIY returns the raw value by a given key.
func
(
c
*
JsonConfigContainer
)
DIY
(
key
string
)
(
v
interface
{},
err
error
)
{
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
...
...
@@ -117,9 +132,10 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
}
else
{
return
nil
,
errors
.
New
(
"not exist key"
)
}
return
nil
,
nil
}
//section.key or key
//
section.key or key
func
(
c
*
JsonConfigContainer
)
getdata
(
key
string
)
interface
{}
{
c
.
RLock
()
defer
c
.
RUnlock
()
...
...
config/xml.go
View file @
673993fa
//xml parse should incluce in <config></config> tags
package
config
import
(
...
...
@@ -12,9 +10,13 @@ import (
"github.com/beego/x2j"
)
// XmlConfig is a xml config parser and implements Config interface.
// xml configurations should be included in <config></config> tag.
// only support key/value pair as <key>value</key> as each item.
type
XMLConfig
struct
{
}
// Parse returns a ConfigContainer with parsed xml config map.
func
(
xmls
*
XMLConfig
)
Parse
(
filename
string
)
(
ConfigContainer
,
error
)
{
file
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
...
...
@@ -36,27 +38,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
return
x
,
nil
}
// A Config represents the xml configuration.
type
XMLConfigContainer
struct
{
data
map
[
string
]
interface
{}
sync
.
Mutex
}
// Bool returns the boolean value for a given key.
func
(
c
*
XMLConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
return
strconv
.
ParseBool
(
c
.
data
[
key
]
.
(
string
))
}
// Int returns the integer value for a given key.
func
(
c
*
XMLConfigContainer
)
Int
(
key
string
)
(
int
,
error
)
{
return
strconv
.
Atoi
(
c
.
data
[
key
]
.
(
string
))
}
// Int64 returns the int64 value for a given key.
func
(
c
*
XMLConfigContainer
)
Int64
(
key
string
)
(
int64
,
error
)
{
return
strconv
.
ParseInt
(
c
.
data
[
key
]
.
(
string
),
10
,
64
)
}
// Float returns the float value for a given key.
func
(
c
*
XMLConfigContainer
)
Float
(
key
string
)
(
float64
,
error
)
{
return
strconv
.
ParseFloat
(
c
.
data
[
key
]
.
(
string
),
64
)
}
// String returns the string value for a given key.
func
(
c
*
XMLConfigContainer
)
String
(
key
string
)
string
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
string
);
ok
{
return
v
...
...
@@ -64,6 +72,7 @@ func (c *XMLConfigContainer) String(key string) string {
return
""
}
// WriteValue writes a new value for key.
func
(
c
*
XMLConfigContainer
)
Set
(
key
,
val
string
)
error
{
c
.
Lock
()
defer
c
.
Unlock
()
...
...
@@ -71,6 +80,7 @@ func (c *XMLConfigContainer) Set(key, val string) error {
return
nil
}
// DIY returns the raw value by a given key.
func
(
c
*
XMLConfigContainer
)
DIY
(
key
string
)
(
v
interface
{},
err
error
)
{
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
v
,
nil
...
...
config/yaml.go
View file @
673993fa
...
...
@@ -12,9 +12,11 @@ import (
"github.com/beego/goyaml2"
)
// YAMLConfig is a yaml config parser and implements Config interface.
type
YAMLConfig
struct
{
}
// Parse returns a ConfigContainer with parsed yaml config map.
func
(
yaml
*
YAMLConfig
)
Parse
(
filename
string
)
(
ConfigContainer
,
error
)
{
y
:=
&
YAMLConfigContainer
{
data
:
make
(
map
[
string
]
interface
{}),
...
...
@@ -27,7 +29,8 @@ func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) {
return
y
,
nil
}
// 从Reader读取YAML
// Read yaml file to map.
// if json like, use json package, unless goyaml2 package.
func
ReadYmlReader
(
path
string
)
(
cnf
map
[
string
]
interface
{},
err
error
)
{
err
=
nil
f
,
err
:=
os
.
Open
(
path
)
...
...
@@ -68,11 +71,13 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
return
}
// A Config represents the yaml configuration.
type
YAMLConfigContainer
struct
{
data
map
[
string
]
interface
{}
sync
.
Mutex
}
// Bool returns the boolean value for a given key.
func
(
c
*
YAMLConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
bool
);
ok
{
return
v
,
nil
...
...
@@ -80,6 +85,7 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
return
false
,
errors
.
New
(
"not bool value"
)
}
// Int returns the integer value for a given key.
func
(
c
*
YAMLConfigContainer
)
Int
(
key
string
)
(
int
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
int64
);
ok
{
return
int
(
v
),
nil
...
...
@@ -87,6 +93,7 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) {
return
0
,
errors
.
New
(
"not int value"
)
}
// Int64 returns the int64 value for a given key.
func
(
c
*
YAMLConfigContainer
)
Int64
(
key
string
)
(
int64
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
int64
);
ok
{
return
v
,
nil
...
...
@@ -94,6 +101,7 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
return
0
,
errors
.
New
(
"not bool value"
)
}
// Float returns the float value for a given key.
func
(
c
*
YAMLConfigContainer
)
Float
(
key
string
)
(
float64
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
float64
);
ok
{
return
v
,
nil
...
...
@@ -101,6 +109,7 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) {
return
0.0
,
errors
.
New
(
"not float64 value"
)
}
// String returns the string value for a given key.
func
(
c
*
YAMLConfigContainer
)
String
(
key
string
)
string
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
string
);
ok
{
return
v
...
...
@@ -108,6 +117,7 @@ func (c *YAMLConfigContainer) String(key string) string {
return
""
}
// WriteValue writes a new value for key.
func
(
c
*
YAMLConfigContainer
)
Set
(
key
,
val
string
)
error
{
c
.
Lock
()
defer
c
.
Unlock
()
...
...
@@ -115,6 +125,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error {
return
nil
}
// DIY returns the raw value by a given key.
func
(
c
*
YAMLConfigContainer
)
DIY
(
key
string
)
(
v
interface
{},
err
error
)
{
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
v
,
nil
...
...
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