Skip to content

Commit

Permalink
Finish v3.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Jun 7, 2020
2 parents d188cea + 8b4c7b5 commit e3c2021
Show file tree
Hide file tree
Showing 26 changed files with 697 additions and 257 deletions.
35 changes: 0 additions & 35 deletions component/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,6 @@ var (
jsonConfigFile = &json.ConfigFile{}
)

func TestCreateApolloConfigWithJson(t *testing.T) {
jsonStr := `{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))

Assert(t, err, NilVal())
Assert(t, config, NotNilVal())

Assert(t, "100004458", Equal(config.AppID))
Assert(t, "default", Equal(config.Cluster))
Assert(t, "application", Equal(config.NamespaceName))
Assert(t, "20170430092936-dee2d58e74515ff3", Equal(config.ReleaseKey))
Assert(t, "value1", Equal(config.Configurations["key1"]))
Assert(t, "value2", Equal(config.Configurations["key2"]))

}

func TestCreateApolloConfigWithJsonError(t *testing.T) {
jsonStr := `jklasdjflasjdfa`

config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))

Assert(t, err, NotNilVal())
Assert(t, config, NilVal())
}

func TestSelectOnlyOneHost(t *testing.T) {
trySyncServerIPList()
appConfig := env.GetPlainAppConfig()
Expand Down
34 changes: 33 additions & 1 deletion component/notify/componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package notify
import (
"encoding/json"
"fmt"
"github.com/zouyx/agollo/v3/constant"
"net/url"
"path"
"sync"
"time"

Expand All @@ -27,6 +29,8 @@ const (
syncNofityConnectTimeout = 3 * time.Second //3s

defaultNotificationId = int64(-1)

defaultContentKey = "content"
)

var (
Expand Down Expand Up @@ -250,7 +254,7 @@ func updateAllNotifications(remoteConfigs []*apolloNotify) {

//AutoSyncConfigServicesSuccessCallBack 同步配置回调
func AutoSyncConfigServicesSuccessCallBack(responseBody []byte) (o interface{}, err error) {
apolloConfig, err := env.CreateApolloConfigWithJSON(responseBody)
apolloConfig, err := createApolloConfigWithJSON(responseBody)

if err != nil {
log.Error("Unmarshal Msg Fail,Error:", err)
Expand All @@ -263,6 +267,34 @@ func AutoSyncConfigServicesSuccessCallBack(responseBody []byte) (o interface{},
return nil, nil
}

// createApolloConfigWithJSON 使用json配置转换成apolloconfig
func createApolloConfigWithJSON(b []byte) (*env.ApolloConfig, error) {
apolloConfig := &env.ApolloConfig{}
err := json.Unmarshal(b, apolloConfig)
if utils.IsNotNil(err) {
return nil, err
}

parser := extension.GetFormatParser(constant.ConfigFileFormat(path.Ext(apolloConfig.NamespaceName)))
if parser == nil {
parser = extension.GetFormatParser(constant.DEFAULT)
}

if parser == nil {
return apolloConfig, nil
}

m, err := parser.Parse(apolloConfig.Configurations[defaultContentKey])
if err != nil {
log.Debug("GetContent fail ! error:", err)
}

if len(m) > 0 {
apolloConfig.Configurations = m
}
return apolloConfig, nil
}

//AutoSyncConfigServices 自动同步配置
func AutoSyncConfigServices(newAppConfig *config.AppConfig) error {
return autoSyncNamespaceConfigServices(newAppConfig, allNotifications)
Expand Down
35 changes: 35 additions & 0 deletions component/notify/componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,38 @@ func getTestAppConfig() *config.AppConfig {

return c.(*config.AppConfig)
}

func TestCreateApolloConfigWithJson(t *testing.T) {
jsonStr := `{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := createApolloConfigWithJSON([]byte(jsonStr))

Assert(t, err, NilVal())
Assert(t, config, NotNilVal())

Assert(t, "100004458", Equal(config.AppID))
Assert(t, "default", Equal(config.Cluster))
Assert(t, "application", Equal(config.NamespaceName))
Assert(t, "20170430092936-dee2d58e74515ff3", Equal(config.ReleaseKey))
Assert(t, "value1", Equal(config.Configurations["key1"]))
Assert(t, "value2", Equal(config.Configurations["key2"]))

}

func TestCreateApolloConfigWithJsonError(t *testing.T) {
jsonStr := `jklasdjflasjdfa`

config, err := createApolloConfigWithJSON([]byte(jsonStr))

Assert(t, err, NotNilVal())
Assert(t, config, NilVal())
}
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"
)
11 changes: 0 additions & 11 deletions env/apollo_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package env

import (
"encoding/json"
"sync"

"github.com/zouyx/agollo/v3/utils"
Expand Down Expand Up @@ -67,13 +66,3 @@ func (a *ApolloConfig) Init(appID string, cluster string, namespace string) {
a.Cluster = cluster
a.NamespaceName = namespace
}

//CreateApolloConfigWithJSON 使用json配置转换成apolloconfig
func CreateApolloConfigWithJSON(b []byte) (*ApolloConfig, error) {
apolloConfig := &ApolloConfig{}
err := json.Unmarshal(b, apolloConfig)
if utils.IsNotNil(err) {
return nil, err
}
return apolloConfig, nil
}
15 changes: 13 additions & 2 deletions env/file/json/json_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package json

import (
"encoding/json"
"github.com/zouyx/agollo/v3/utils"
"os"
"testing"

Expand All @@ -23,7 +25,7 @@ func TestJSONFileHandler_WriteConfigFile(t *testing.T) {
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))
config, err := createApolloConfigWithJSON([]byte(jsonStr))
os.Remove(extension.GetFileHandler().GetConfigFile(configPath, config.NamespaceName))

Assert(t, err, NilVal())
Expand All @@ -44,7 +46,7 @@ func TestJSONFileHandler_LoadConfigFile(t *testing.T) {
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))
config, err := createApolloConfigWithJSON([]byte(jsonStr))

Assert(t, err, NilVal())
newConfig, e := extension.GetFileHandler().LoadConfigFile("", config.NamespaceName)
Expand All @@ -56,3 +58,12 @@ func TestJSONFileHandler_LoadConfigFile(t *testing.T) {
Assert(t, config.Cluster, Equal(newConfig.Cluster))
Assert(t, config.NamespaceName, Equal(newConfig.NamespaceName))
}

func createApolloConfigWithJSON(b []byte) (*env.ApolloConfig, error) {
apolloConfig := &env.ApolloConfig{}
err := json.Unmarshal(b, apolloConfig)
if utils.IsNotNil(err) {
return nil, err
}
return apolloConfig, nil
}
3 changes: 1 addition & 2 deletions env/file/json/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/extension"
)

Expand All @@ -23,7 +22,7 @@ func TestRawHandler_WriteConfigFile(t *testing.T) {
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))
config, err := createApolloConfigWithJSON([]byte(jsonStr))
os.Remove(extension.GetFileHandler().GetConfigFile(configPath, config.NamespaceName))

Assert(t, err, NilVal())
Expand Down
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]
}
29 changes: 29 additions & 0 deletions extension/format_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package extension

import (
"github.com/zouyx/agollo/v3/constant"
"testing"

. "github.com/tevid/gohamcrest"
)

// TestParser 默认内容转换器
type TestParser struct {
}

// Parse 内存内容默认转换器
func (d *TestParser) Parse(s string) (map[string]string, error) {
return nil, 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))
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/zouyx/agollo/v3

require github.com/tevid/gohamcrest v1.1.1
require (
github.com/spf13/viper v1.7.0
github.com/tevid/gohamcrest v1.1.1
)

go 1.13

0 comments on commit e3c2021

Please sign in to comment.