forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apolloconfig#115 from zouyx/feature/fixFormatParser
[Feature]: 增加读取 Apollo 配置反序列化插件
- Loading branch information
Showing
12 changed files
with
259 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package constant | ||
|
||
//ConfigFileFormat 配置文件类型 | ||
type ConfigFileFormat string | ||
|
||
const ( | ||
//Properties Properties | ||
Properties ConfigFileFormat = "properties" | ||
//XML XML | ||
XML ConfigFileFormat = "xml" | ||
//JSON JSON | ||
JSON ConfigFileFormat = "json" | ||
//YML YML | ||
YML ConfigFileFormat = "yml" | ||
//YAML YAML | ||
YAML ConfigFileFormat = "yaml" | ||
// DEFAULT DEFAULT | ||
DEFAULT ConfigFileFormat = "default" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package extension | ||
|
||
import ( | ||
"github.com/zouyx/agollo/v3/constant" | ||
"github.com/zouyx/agollo/v3/utils/parse" | ||
) | ||
|
||
var formatParser = make(map[constant.ConfigFileFormat]parse.ContentParser, 0) | ||
|
||
// AddFormatParser 设置 formatParser | ||
func AddFormatParser(key constant.ConfigFileFormat, contentParser parse.ContentParser) { | ||
formatParser[key] = contentParser | ||
} | ||
|
||
// GetFormatParser 获取 formatParser | ||
func GetFormatParser(key constant.ConfigFileFormat) parse.ContentParser { | ||
return formatParser[key] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package extension | ||
|
||
import ( | ||
"github.com/zouyx/agollo/v3/agcache" | ||
"github.com/zouyx/agollo/v3/constant" | ||
"testing" | ||
|
||
. "github.com/tevid/gohamcrest" | ||
) | ||
|
||
// TestParser 默认内容转换器 | ||
type TestParser struct { | ||
} | ||
|
||
// Parse 内存内容默认转换器 | ||
func (d *TestParser) Parse(cache agcache.CacheInterface) (string, error) { | ||
return "", nil | ||
} | ||
|
||
func TestAddFormatParser(t *testing.T) { | ||
AddFormatParser(constant.DEFAULT, &TestParser{}) | ||
AddFormatParser(constant.Properties, &TestParser{}) | ||
|
||
p := GetFormatParser(constant.DEFAULT) | ||
|
||
b := p.(*TestParser) | ||
Assert(t, b, NotNilVal()) | ||
|
||
Assert(t, len(formatParser), Equal(2)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package normal | ||
|
||
import ( | ||
"github.com/zouyx/agollo/v3/constant" | ||
"github.com/zouyx/agollo/v3/extension" | ||
"github.com/zouyx/agollo/v3/utils" | ||
|
||
"github.com/zouyx/agollo/v3/agcache" | ||
) | ||
|
||
func init() { | ||
extension.AddFormatParser(constant.DEFAULT, &Parser{}) | ||
} | ||
|
||
const ( | ||
defaultContentKey = "content" | ||
) | ||
|
||
// Parser 默认内容转换器 | ||
type Parser struct { | ||
} | ||
|
||
// Parse 内存内容默认转换器 | ||
func (d *Parser) Parse(cache agcache.CacheInterface) (string, error) { | ||
if cache == nil { | ||
return utils.Empty, nil | ||
} | ||
|
||
value, err := cache.Get(defaultContentKey) | ||
if err != nil { | ||
return utils.Empty, err | ||
} | ||
return string(value), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package normal | ||
|
||
import ( | ||
"errors" | ||
"github.com/zouyx/agollo/v3/utils" | ||
"github.com/zouyx/agollo/v3/utils/parse" | ||
"sync" | ||
"testing" | ||
|
||
. "github.com/tevid/gohamcrest" | ||
"github.com/zouyx/agollo/v3/agcache" | ||
) | ||
|
||
var ( | ||
testDefaultCache agcache.CacheInterface | ||
defaultParser parse.ContentParser | ||
) | ||
|
||
//DefaultCache 默认缓存 | ||
type DefaultCache struct { | ||
defaultCache sync.Map | ||
} | ||
|
||
//Set 获取缓存 | ||
func (d *DefaultCache) Set(key string, value []byte, expireSeconds int) (err error) { | ||
d.defaultCache.Store(key, value) | ||
return nil | ||
} | ||
|
||
//EntryCount 获取实体数量 | ||
func (d *DefaultCache) EntryCount() (entryCount int64) { | ||
count := int64(0) | ||
d.defaultCache.Range(func(key, value interface{}) bool { | ||
count++ | ||
return true | ||
}) | ||
return count | ||
} | ||
|
||
//Get 获取缓存 | ||
func (d *DefaultCache) Get(key string) (value []byte, err error) { | ||
v, ok := d.defaultCache.Load(key) | ||
if !ok { | ||
return nil, errors.New("load normal cache fail") | ||
} | ||
return v.([]byte), nil | ||
} | ||
|
||
//Range 遍历缓存 | ||
func (d *DefaultCache) Range(f func(key, value interface{}) bool) { | ||
d.defaultCache.Range(f) | ||
} | ||
|
||
//Del 删除缓存 | ||
func (d *DefaultCache) Del(key string) (affected bool) { | ||
d.defaultCache.Delete(key) | ||
return true | ||
} | ||
|
||
//Clear 清除所有缓存 | ||
func (d *DefaultCache) Clear() { | ||
d.defaultCache = sync.Map{} | ||
} | ||
|
||
//DefaultCacheFactory 构造默认缓存组件工厂类 | ||
type DefaultCacheFactory struct { | ||
} | ||
|
||
//Create 创建默认缓存组件 | ||
func (d *DefaultCacheFactory) Create() agcache.CacheInterface { | ||
return &DefaultCache{} | ||
} | ||
|
||
func init() { | ||
factory := &DefaultCacheFactory{} | ||
testDefaultCache = factory.Create() | ||
|
||
defaultParser = &Parser{} | ||
|
||
testDefaultCache.Set("a", []byte("b"), 100) | ||
testDefaultCache.Set("c", []byte("d"), 100) | ||
testDefaultCache.Set("content", []byte("content"), 100) | ||
} | ||
|
||
func TestDefaultParser(t *testing.T) { | ||
s, err := defaultParser.Parse(testDefaultCache) | ||
Assert(t, err, NilVal()) | ||
Assert(t, s, Equal("content")) | ||
|
||
s, err = defaultParser.Parse(nil) | ||
Assert(t, err, NilVal()) | ||
Assert(t, s, Equal(utils.Empty)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package parse | ||
|
||
import ( | ||
"github.com/zouyx/agollo/v3/agcache" | ||
) | ||
|
||
//ContentParser 内容转换 | ||
type ContentParser interface { | ||
Parse(cache agcache.CacheInterface) (string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package properties | ||
|
||
import ( | ||
"fmt" | ||
"github.com/zouyx/agollo/v3/agcache" | ||
"github.com/zouyx/agollo/v3/constant" | ||
"github.com/zouyx/agollo/v3/extension" | ||
"github.com/zouyx/agollo/v3/utils" | ||
) | ||
|
||
func init() { | ||
extension.AddFormatParser(constant.Properties, &Parser{}) | ||
} | ||
|
||
const ( | ||
propertiesFormat = "%s=%s\n" | ||
) | ||
|
||
// Parser properties转换器 | ||
type Parser struct { | ||
} | ||
|
||
// Parse 内存内容=>properties文件转换器 | ||
func (d *Parser) Parse(cache agcache.CacheInterface) (string, error) { | ||
properties := convertToProperties(cache) | ||
return properties, nil | ||
} | ||
|
||
func convertToProperties(cache agcache.CacheInterface) string { | ||
properties := utils.Empty | ||
if cache == nil { | ||
return properties | ||
} | ||
cache.Range(func(key, value interface{}) bool { | ||
properties += fmt.Sprintf(propertiesFormat, key, string(value.([]byte))) | ||
return true | ||
}) | ||
return properties | ||
} |
Oops, something went wrong.