Skip to content

Commit

Permalink
Merge pull request #5 from lalamove/add-more-format-support
Browse files Browse the repository at this point in the history
Add json and yaml format
  • Loading branch information
kyue1005 authored Jan 11, 2021
2 parents 2d5ca77 + c315179 commit 627f6b3
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 9 deletions.
15 changes: 14 additions & 1 deletion configs/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,22 @@ myAppID:
dynamic:
p6spy: false
primary: master
json: >
[
{
"hello": "hall"
},
{
"hello": "hall"
}
]
myNamespace3:
releaseKey: "20200309212653-7fec91b6d277b5ad"
yml: >
plain text
xml: >
plain text
plain text
yaml: >
plain text
json: >
plain text
4 changes: 4 additions & 0 deletions internal/routes/apollo/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ func (a *Apollo) getNamespace(appID string, cluster string, namespace string) (w
func (a *Apollo) getNamespaceConfig(extension string, namespace watcher.Namespace) (interface{}, error) {
switch extension {
case ".yml":
return map[string]string{"content": namespace.Yml}, nil
case ".yaml":
return map[string]string{"content": namespace.Yaml}, nil
case ".json":
return map[string]string{"content": namespace.JSON}, nil
case ".xml":
return map[string]string{"content": namespace.XML}, nil
case ".properties":
Expand Down
65 changes: 59 additions & 6 deletions internal/routes/apollo/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,36 @@ var stubConfigs = []watcher.ConfigMap{
dynamic:
p6spy: false
primary: master
`, Yml: `spring:
datasource:
dynamic:
p6spy: false
primary: master
`,
XML: "",
JSON: `[
{
"abc":"lbs-test",
},
{
"def":"456",
}
]
`,
},
"ns2": {
ReleaseKey: "abc",
Properties: map[string]string{},
Yaml: `[raw]
Yml: `[raw]
key = value
`,
Yaml: `[raw]
key = value
`,
XML: "plain text",
JSON: `[raw]
key = value
`,
},
},
},
Expand Down Expand Up @@ -186,8 +206,8 @@ func TestGetNamespaceConfig(t *testing.T) {
)
})

t.Run("get yml", func(t *testing.T) {
cfg, err := a.getNamespaceConfig(".yml", stubConfigs[0]["app"]["cluster"]["ns"])
t.Run("get yaml", func(t *testing.T) {
cfg, err := a.getNamespaceConfig(".yaml", stubConfigs[0]["app"]["cluster"]["ns"])
require.Nil(t, err)

c, ok := cfg.(map[string]string)
Expand All @@ -200,7 +220,6 @@ func TestGetNamespaceConfig(t *testing.T) {
err = yaml.Unmarshal([]byte(content), y)
require.Nil(t, err)
b, err := yaml.Marshal(y)
t.Log(string(b))
require.Nil(t, err)

require.Equal(
Expand All @@ -227,8 +246,42 @@ func TestGetNamespaceConfig(t *testing.T) {
)
})

t.Run("get invalid yml", func(t *testing.T) {
cfg, err := a.getNamespaceConfig(".yml", stubConfigs[0]["app"]["cluster"]["ns2"])
t.Run("get json", func(t *testing.T) {
cfg, err := a.getNamespaceConfig(".json", stubConfigs[0]["app"]["cluster"]["ns"])
require.Nil(t, err)

c, ok := cfg.(map[string]string)
require.True(t, ok)

content, found := c["content"]
require.True(t, found)

require.Equal(
t,
stubConfigs[0]["app"]["cluster"]["ns"].JSON,
content,
)
})

t.Run("get invalid json", func(t *testing.T) {
cfg, err := a.getNamespaceConfig(".json", stubConfigs[0]["app"]["cluster"]["ns2"])
require.Nil(t, err)

c, ok := cfg.(map[string]string)
require.True(t, ok)

content, found := c["content"]
require.True(t, found)

require.Equal(
t,
stubConfigs[0]["app"]["cluster"]["ns2"].JSON,
content,
)
})

t.Run("get invalid yaml", func(t *testing.T) {
cfg, err := a.getNamespaceConfig(".yaml", stubConfigs[0]["app"]["cluster"]["ns2"])
require.Nil(t, err)

c, ok := cfg.(map[string]string)
Expand Down
29 changes: 27 additions & 2 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package watcher

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -19,7 +20,9 @@ import (
type Namespace struct {
ReleaseKey string `yaml:"releaseKey" json:"releaseKey"`
Properties map[string]string `yaml:"properties" json:"properties"`
Yaml string `yaml:"yml" json:"yml"`
Yml string `yaml:"yml" json:"yml"`
Yaml string `yaml:"yaml" json:"yaml"`
JSON string `yaml:"json" json:"json"`
XML string `yaml:"xml" json:"xml"`
}

Expand Down Expand Up @@ -156,14 +159,25 @@ func (w *Watcher) readConfigMap(log nlogger.Provider) error {
if nsKey == "" {
return fmt.Errorf("invalid namespace name '%s' in %s/%s", nsKey, appKey, clusterKey)
}
if ns.Properties == nil && ns.Yaml == "" && ns.XML == "" {
if ns.Properties == nil && ns.Yml == "" && ns.Yaml == "" && ns.XML == "" && ns.JSON == "" {
return fmt.Errorf("invalid namespace '%s' in %s/%s", nsKey, appKey, clusterKey)
}
for configKey := range ns.Properties {
if configKey == "" {
return fmt.Errorf("invalid config key '%s' in %s/%s/%s", configKey, appKey, clusterKey, nsKey)
}
}
// validate Yml
if ns.Yml != "" {
cfg := make(map[interface{}]interface{})
if err := yaml.Unmarshal([]byte(ns.Yml), &cfg); err != nil {
log.Get().Warn(fmt.Sprintf(
"failed to parse yml config for namespace '%s' in %s/%s: %s",
nsKey, appKey, clusterKey, err.Error(),
))
}
}

// validate Yaml
if ns.Yaml != "" {
cfg := make(map[interface{}]interface{})
Expand All @@ -174,6 +188,17 @@ func (w *Watcher) readConfigMap(log nlogger.Provider) error {
))
}
}

// validate JSON
if ns.JSON != "" {
var cfg interface{}
if err := json.Unmarshal([]byte(ns.JSON), &cfg); err != nil {
log.Get().Warn(fmt.Sprintf(
"failed to parse json config for namespace '%s' in %s/%s: %s",
nsKey, appKey, clusterKey, err.Error(),
))
}
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ var stubConfigs = []ConfigMap{
"mysql.uri": "mysql://root@localhost/mysql",
"snowflake.uri": "http://192.168.0.1/snowflake",
},
Yml: "",
Yaml: "",
XML: "",
JSON: "",
},
},
},
Expand All @@ -38,8 +40,10 @@ var stubConfigs = []ConfigMap{
"mysql.uri": "mysql://root@localhost/mysql2",
"snowflake.uri": "http://192.168.0.1/snowflake",
},
Yml: "invalid yml; key = value",
Yaml: "invalid yml; key = value",
XML: "",
JSON: "invalid json; key = value",
},
},
},
Expand Down

0 comments on commit 627f6b3

Please sign in to comment.