refactor context: Extract loadJsonOrYaml to util package

The logic to deserialise a structure from *either* JSON or YAML is reused several
times and can be easily extracted, which this commit does.
This commit is contained in:
Vincent Ambo 2017-06-22 15:52:43 +02:00
parent e2f7cf6258
commit 3728d0ae2e
2 changed files with 30 additions and 29 deletions

View file

@ -1,13 +1,8 @@
package context
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/ghodss/yaml"
"github.com/polydawn/meep"
"github.com/tazjin/kontemplate/util"
)
@ -35,27 +30,8 @@ type ContextLoadingError struct {
// Attempt to load and deserialise a Context from the specified file.
func LoadContextFromFile(filename string) (*Context, error) {
file, err := ioutil.ReadFile(filename)
if err != nil {
return nil, meep.New(
&ContextLoadingError{Filename: filename},
meep.Cause(err),
)
}
var c Context
if strings.HasSuffix(filename, "json") {
err = json.Unmarshal(file, &c)
} else if strings.HasSuffix(filename, "yaml") || strings.HasSuffix(filename, "yml") {
err = yaml.Unmarshal(file, &c)
} else {
return nil, meep.New(
&ContextLoadingError{Filename: filename},
meep.Cause(fmt.Errorf("File format not supported. Must be JSON or YAML.")),
)
}
err := util.LoadJsonOrYaml(filename, &c)
if err != nil {
return nil, meep.New(
@ -112,16 +88,14 @@ func loadDefaultValues(rs *ResourceSet, c *Context) *map[string]interface{} {
var defaultVars map[string]interface{}
// Attempt to load YAML values
y, err := ioutil.ReadFile(path.Join(c.BaseDir, rs.Name, "default.yaml"))
err := util.LoadJsonOrYaml(path.Join(c.BaseDir, rs.Name, "default.yaml"), &defaultVars)
if err == nil {
yaml.Unmarshal(y, &defaultVars)
return util.Merge(&defaultVars, &rs.Values)
}
// Attempt to load JSON values
j, err := ioutil.ReadFile(path.Join(c.BaseDir, rs.Name, "default.json"))
err = util.LoadJsonOrYaml(path.Join(c.BaseDir, rs.Name, "default.json"), &defaultVars)
if err == nil {
json.Unmarshal(j, &defaultVars)
return util.Merge(&defaultVars, &rs.Values)
}

View file

@ -1,5 +1,14 @@
package util
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/ghodss/yaml"
)
// Merges two maps together. Values from the second map override values in the first map.
// The returned map is new if anything was changed.
func Merge(in1 *map[string]interface{}, in2 *map[string]interface{}) *map[string]interface{} {
@ -22,3 +31,21 @@ func Merge(in1 *map[string]interface{}, in2 *map[string]interface{}) *map[string
return &new
}
// Loads either a YAML or JSON file from the specified path and deserialises it into the provided interface.
func LoadJsonOrYaml(filename string, addr interface{}) error {
file, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if strings.HasSuffix(filename, "json") {
err = json.Unmarshal(file, addr)
} else if strings.HasSuffix(filename, "yaml") || strings.HasSuffix(filename, "yml") {
err = yaml.Unmarshal(file, addr)
} else {
return fmt.Errorf("File format not supported. Must be JSON or YAML.")
}
return nil
}