Skip to content

Commit

Permalink
fixes #55 Allow user to specify name of key for includes array
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-miracl committed Apr 12, 2018
1 parent e2aecaf commit 9ecbc66
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
3 changes: 3 additions & 0 deletions conflate.go
Expand Up @@ -4,6 +4,9 @@ import (
"net/url"
)

// Includes is used to specify the top level key that holds the includes array
var Includes = "includes"

// Conflate contains a 'working' merged data set and optionally a JSON v4 schema
type Conflate struct {
data interface{}
Expand Down
7 changes: 7 additions & 0 deletions conflate/main.go
Expand Up @@ -24,10 +24,17 @@ func main() {
defaults := flag.Bool("defaults", false, "Apply defaults from schema to data")
validate := flag.Bool("validate", false, "Validate the data against the schema")
format := flag.String("format", "", "Output format of the data JSON/YAML/TOML")
includes := flag.String("includes", "includes", "Name of includes array. Blank string suppresses expansion of includes arrays")
noincludes := flag.Bool("noincludes", false, "Switches off conflation of includes. Overrides any --includes setting.")
expand := flag.Bool("expand", false, "Expand environment variables in files")

flag.Parse()

conflate.Includes = *includes
if *noincludes {
conflate.Includes = ""
}

c := conflate.New()
c.Expand(*expand)

Expand Down
2 changes: 1 addition & 1 deletion conflate_test.go
Expand Up @@ -43,7 +43,7 @@ func TestFromFiles_IncludesRemoved(t *testing.T) {
var testData map[string]interface{}
err = c.Unmarshal(&testData)
assert.Nil(t, err)
assert.Nil(t, testData["includes"])
assert.Nil(t, testData[Includes])
}

func TestAddData_Expand(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions filedata.go
Expand Up @@ -83,11 +83,14 @@ func (fd *filedata) unmarshal() error {
}

func (fd *filedata) extractIncludes() error {
err := jsonMarshalUnmarshal(fd.obj["includes"], &fd.includes)
if Includes == "" {
return nil
}
err := jsonMarshalUnmarshal(fd.obj[Includes], &fd.includes)
if err != nil {
return wrapError(err, "Could not extract includes")
}
delete(fd.obj, "includes")
delete(fd.obj, Includes)
return nil
}

Expand Down
39 changes: 35 additions & 4 deletions filedata_test.go
Expand Up @@ -138,29 +138,29 @@ func TestFiledata_TOMLAsJSON(t *testing.T) {
func TestFiledata_NoIncludes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"x": 1}`))
assert.Nil(t, err)
assert.Nil(t, fd.obj["includes"])
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}

func TestFiledata_BlankIncludes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"includes":[], "x": 1}`))
assert.Nil(t, err)
assert.Nil(t, fd.obj["includes"])
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}

func TestFiledata_NullIncludes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"includes":null, "x": 1}`))
assert.Nil(t, err)
assert.Nil(t, fd.obj["includes"])
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}

func TestFiledata_Includes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"includes":["test1", "test2"], "x": 1}`))
assert.Nil(t, err)
assert.Equal(t, fd.includes, []string{"test1", "test2"})
assert.Nil(t, fd.obj["includes"])
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}

Expand All @@ -178,3 +178,34 @@ func TestFiledatas_Unmarshal(t *testing.T) {
}
assert.Equal(t, fds.objs(), []interface{}{testMarshalData, testMarshalData, testMarshalData})
}

func TestFiledatas_DifferentIncludes(t *testing.T) {
old := Includes
Includes = "using"
defer func() { Includes = old }()
fd, err := testLoader.wrapFiledata([]byte(`{"using":["test1", "test2"], "x": 1}`))
assert.Nil(t, err)
assert.Equal(t, fd.includes, []string{"test1", "test2"})
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}

func TestFiledatas_NoIncludes(t *testing.T) {
old := Includes
Includes = "using"
defer func() { Includes = old }()
fd, err := testLoader.wrapFiledata([]byte(`{"includes":["test1", "test2"]}`))
assert.Nil(t, err)
assert.Empty(t, fd.includes)
assert.Equal(t, fd.obj, map[string]interface{}{"includes": []interface{}{"test1", "test2"}})
}

func TestFiledatas_IgnoreIncludes(t *testing.T) {
old := Includes
Includes = ""
defer func() { Includes = old }()
fd, err := testLoader.wrapFiledata([]byte(`{"":["test1", "test2"]}`))
assert.Nil(t, err)
assert.Empty(t, fd.includes)
assert.Equal(t, fd.obj, map[string]interface{}{"": []interface{}{"test1", "test2"}})
}

0 comments on commit 9ecbc66

Please sign in to comment.