Skip to content

Commit

Permalink
Merge pull request #26 from HugeSkull/issue/remote-marshal-panic
Browse files Browse the repository at this point in the history
Issue/remote marshal panic
  • Loading branch information
shima-park authored Nov 28, 2019
2 parents d4a0a71 + db74ee4 commit ea55edf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 188 deletions.
84 changes: 12 additions & 72 deletions viper-remote/marshal.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,24 @@
package remote

import (
"encoding/json"
"bytes"
"fmt"
"io"

"github.com/hashicorp/hcl/hcl/printer"

"github.com/hashicorp/hcl"
"github.com/magiconair/properties"
toml "github.com/pelletier/go-toml"
yaml "gopkg.in/yaml.v2"
)

// ConfigMarshalError happens when failing to marshal the configuration.
type ConfigMarshalError struct {
err error
}

// Error returns the formatted configuration error.
func (e ConfigMarshalError) Error() string {
return fmt.Sprintf("While marshaling config: %s", e.err.Error())
}

func MarshalWriter(w io.Writer, c map[string]interface{}, configType string) error {
switch configType {
case "json":
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return ConfigMarshalError{err}
}
_, err = w.Write(b)
if err != nil {
return ConfigMarshalError{err}
}

case "hcl":
b, err := json.Marshal(c)
if err != nil {
return ConfigMarshalError{err}
}
ast, err := hcl.Parse(string(b))
if err != nil {
return ConfigMarshalError{err}
}
err = printer.Fprint(w, ast.Node)
func marshalProperties(c map[string]interface{}) ([]byte, error) {
p := properties.NewProperties()
for key, val := range c {
_, _, err := p.Set(key, fmt.Sprint(val))
if err != nil {
return ConfigMarshalError{err}
}

case "prop", "props", "properties":
p := properties.NewProperties()
for key, val := range c {
_, _, err := p.Set(key, fmt.Sprint(val))
if err != nil {
return ConfigMarshalError{err}
}
}
_, err := p.WriteComment(w, "#", properties.UTF8)
if err != nil {
return ConfigMarshalError{err}
}

case "toml":
t, err := toml.TreeFromMap(c)
if err != nil {
return ConfigMarshalError{err}
}
if _, err := t.WriteTo(w); err != nil {
return ConfigMarshalError{err}
}

case "yaml", "yml":
b, err := yaml.Marshal(c)
if err != nil {
return ConfigMarshalError{err}
}
if _, err = w.Write(b); err != nil {
return ConfigMarshalError{err}
return nil, err
}
}
return nil
buff := bytes.NewBuffer(nil)
_, err := p.WriteComment(buff, "#", properties.UTF8)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
23 changes: 10 additions & 13 deletions viper-remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync"

"github.com/shima-park/agollo"
Expand Down Expand Up @@ -97,24 +96,22 @@ func newAgollo(appid, endpoint string, opts []agollo.Option) (agollo.Agollo, err

func (cm apolloConfigManager) Get(namespace string) ([]byte, error) {
configs := cm.agollo.GetNameSpace(namespace)
return marshalSettings(getConfigType(namespace), configs)
return marshalConfigs(getConfigType(namespace), configs)
}

func marshalSettings(configType string, configs map[string]interface{}) ([]byte, error) {
buff := bytes.NewBuffer(nil)
settings := map[string]interface{}{}
func marshalConfigs(configType string, configs map[string]interface{}) ([]byte, error) {
var bts []byte
var err error
switch configType {
case "json", "yml", "yaml", "xml":
content := configs["content"].(string)
err := UnmarshalReader(strings.NewReader(content), settings, configType)
if err != nil {
return nil, err
content := configs["content"]
if content != nil {
bts = []byte(content.(string))
}
case "properties":
settings = configs
bts, err = marshalProperties(configs)
}
err := MarshalWriter(buff, settings, configType)
return buff.Bytes(), err
return bts, err
}

func (cm apolloConfigManager) Watch(namespace string, stop chan bool) <-chan *viper.RemoteResponse {
Expand All @@ -135,7 +132,7 @@ func (cm apolloConfigManager) Watch(namespace string, stop chan bool) <-chan *vi
}

configType := getConfigType(namespace)
value, err := marshalSettings(configType, r.NewValue)
value, err := marshalConfigs(configType, r.NewValue)

resp <- &viper.RemoteResponse{Value: value, Error: err}
}
Expand Down
103 changes: 0 additions & 103 deletions viper-remote/unmarshal.go

This file was deleted.

0 comments on commit ea55edf

Please sign in to comment.