Skip to content

Commit

Permalink
Merge pull request apolloconfig#115 from zouyx/feature/fixFormatParser
Browse files Browse the repository at this point in the history
[Feature]: 增加读取 Apollo 配置反序列化插件
  • Loading branch information
zouyx committed Jun 5, 2020
2 parents 23b4503 + dfda727 commit 0485522
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 105 deletions.
19 changes: 19 additions & 0 deletions constant/config_file_format.go
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"
)
18 changes: 18 additions & 0 deletions extension/format_parser.go
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]
}
30 changes: 30 additions & 0 deletions extension/format_parser_test.go
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))
}
2 changes: 2 additions & 0 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/zouyx/agollo/v3/protocol/auth"
_ "github.com/zouyx/agollo/v3/protocol/auth/sign"
"github.com/zouyx/agollo/v3/storage"
_ "github.com/zouyx/agollo/v3/utils/parse/normal"
_ "github.com/zouyx/agollo/v3/utils/parse/properties"
)

var (
Expand Down
30 changes: 4 additions & 26 deletions storage/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"github.com/zouyx/agollo/v3/constant"
"strconv"
"sync"
"sync/atomic"
Expand All @@ -12,22 +13,6 @@ import (
"github.com/zouyx/agollo/v3/utils"
)

//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"
)

const (
//1 minute
configCacheExpireTime = 120
Expand All @@ -38,15 +23,8 @@ const (
var (
//config from apollo
apolloConfigCache sync.Map

formatParser = make(map[ConfigFileFormat]utils.ContentParser, 0)
defaultFormatParser = &utils.DefaultParser{}
)

func init() {
formatParser[Properties] = &utils.PropertiesParser{}
}

//InitConfigCache 获取程序配置初始化agollo内润配置
func InitConfigCache() {
if env.GetPlainAppConfig() == nil {
Expand Down Expand Up @@ -275,10 +253,10 @@ func UpdateApolloConfigCache(configurations map[string]string, expireTime int, n
}

//GetContent 获取配置文件内容
func (c *Config) GetContent(format ConfigFileFormat) string {
parser := formatParser[format]
func (c *Config) GetContent(format constant.ConfigFileFormat) string {
parser := extension.GetFormatParser(format)
if parser == nil {
parser = defaultFormatParser
parser = extension.GetFormatParser(constant.DEFAULT)
}
s, err := parser.Parse(c.cache)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion storage/repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"github.com/zouyx/agollo/v3/constant"
"strings"
"testing"
"time"
Expand All @@ -9,6 +10,9 @@ import (
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/env"
_ "github.com/zouyx/agollo/v3/env/file/json"

_ "github.com/zouyx/agollo/v3/utils/parse/normal"
_ "github.com/zouyx/agollo/v3/utils/parse/properties"
)

//init param
Expand Down Expand Up @@ -84,7 +88,7 @@ func TestGetConfig(t *testing.T) {
Assert(t, b, Equal(false))

//content
content := config.GetContent(Properties)
content := config.GetContent(constant.Properties)
hasFloat := strings.Contains(content, "float=1")
Assert(t, hasFloat, Equal(true))

Expand Down
34 changes: 34 additions & 0 deletions utils/parse/normal/parser.go
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
}
93 changes: 93 additions & 0 deletions utils/parse/normal/parser_test.go
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))
}
10 changes: 10 additions & 0 deletions utils/parse/parser.go
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)
}
39 changes: 39 additions & 0 deletions utils/parse/properties/parser.go
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
}

0 comments on commit 0485522

Please sign in to comment.