-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support marshal and unmarshal (#10)
* add Makefile * expose all private fields * support marshaling and unmarshaling * upgrade go to 1.20
- Loading branch information
Showing
11 changed files
with
331 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
SOURCES ?= $(shell find . -name '*.go') | ||
SOURCES_NONTEST ?= $(shell find . -name '*.go' -not -name '*_test.go') | ||
|
||
.PHONY: test | ||
test: | ||
go test -timeout $${TEST_TIMEOUT:-10s} -v ./... | ||
|
||
# test-all ignores testcache (go clean testcache) | ||
.PHONY: test-all | ||
test-all: | ||
go test -timeout $${TEST_TIMEOUT:-10s} -v -count=1 ./... | ||
|
||
.PHONY: upgrade | ||
upgrade: | ||
rm -f go.sum | ||
go get -u -d ./...; go mod tidy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
module github.com/relex/aini | ||
|
||
go 1.13 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 | ||
github.com/stretchr/testify v1.7.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
github.com/samber/lo v1.38.1 | ||
github.com/stretchr/testify v1.7.0 | ||
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package aini | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/samber/lo" | ||
"golang.org/x/exp/maps" | ||
) | ||
|
||
type alwaysNil interface{} // to hold place for Group and Host references; must be nil in serialized form | ||
|
||
func (group *Group) MarshalJSON() ([]byte, error) { | ||
type groupWithoutCustomMarshal Group | ||
|
||
return json.Marshal(&struct { | ||
groupWithoutCustomMarshal | ||
Hosts map[string]alwaysNil | ||
Children map[string]alwaysNil | ||
Parents map[string]alwaysNil | ||
DirectParents map[string]alwaysNil | ||
}{ | ||
groupWithoutCustomMarshal: groupWithoutCustomMarshal(*group), | ||
Hosts: makeNilValueMap(group.Hosts), | ||
Children: makeNilValueMap(group.Children), | ||
Parents: makeNilValueMap(group.Parents), | ||
DirectParents: makeNilValueMap(group.DirectParents), | ||
}) | ||
} | ||
|
||
func (host *Host) MarshalJSON() ([]byte, error) { | ||
type hostWithoutCustomMarshal Host | ||
|
||
return json.Marshal(&struct { | ||
hostWithoutCustomMarshal | ||
Groups map[string]alwaysNil | ||
DirectGroups map[string]alwaysNil | ||
}{ | ||
hostWithoutCustomMarshal: hostWithoutCustomMarshal(*host), | ||
Groups: makeNilValueMap(host.Groups), | ||
DirectGroups: makeNilValueMap(host.DirectGroups), | ||
}) | ||
} | ||
|
||
func makeNilValueMap[K comparable, V any](m map[K]*V) map[K]alwaysNil { | ||
return lo.MapValues(m, func(_ *V, _ K) alwaysNil { return nil }) | ||
} | ||
|
||
func (inventory *InventoryData) UnmarshalJSON(data []byte) error { | ||
type inventoryWithoutCustomUnmarshal InventoryData | ||
var rawInventory inventoryWithoutCustomUnmarshal | ||
if err := json.Unmarshal(data, &rawInventory); err != nil { | ||
return err | ||
} | ||
// rawInventory's Groups and Hosts should now contain all properties, | ||
// except child group maps and host maps are filled with original keys and null values | ||
|
||
// reassign child groups and hosts to reference rawInventory.Hosts and .Groups | ||
|
||
for _, group := range rawInventory.Groups { | ||
group.Hosts = lo.PickByKeys(rawInventory.Hosts, maps.Keys(group.Hosts)) | ||
group.Children = lo.PickByKeys(rawInventory.Groups, maps.Keys(group.Children)) | ||
group.Parents = lo.PickByKeys(rawInventory.Groups, maps.Keys(group.Parents)) | ||
group.DirectParents = lo.PickByKeys(rawInventory.Groups, maps.Keys(group.DirectParents)) | ||
} | ||
|
||
for _, host := range rawInventory.Hosts { | ||
host.Groups = lo.PickByKeys(rawInventory.Groups, maps.Keys(host.Groups)) | ||
host.DirectGroups = lo.PickByKeys(rawInventory.Groups, maps.Keys(host.DirectGroups)) | ||
} | ||
|
||
inventory.Groups = rawInventory.Groups | ||
inventory.Hosts = rawInventory.Hosts | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package aini | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const minMarshalInventory = `[Animals] | ||
ET | ||
[Animals:children] | ||
Cats | ||
[Cats] | ||
Lion | ||
` | ||
|
||
//go:embed marshal_test_inventory.json | ||
var minMarshalJSON string | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
v, err := ParseString(minMarshalInventory) | ||
assert.Nil(t, err) | ||
|
||
j, err := json.MarshalIndent(v, "", " ") | ||
assert.Nil(t, err) | ||
assert.Equal(t, minMarshalJSON, string(j)) | ||
|
||
t.Run("unmarshal", func(t *testing.T) { | ||
var v2 InventoryData | ||
assert.Nil(t, json.Unmarshal(j, &v2)) | ||
assert.Equal(t, v.Hosts["Lion"], v2.Hosts["Lion"]) | ||
assert.Equal(t, v.Groups["Cats"], v2.Groups["Cats"]) | ||
}) | ||
} | ||
|
||
func TestMarshalWithVars(t *testing.T) { | ||
v, err := ParseFile("test_data/inventory") | ||
assert.Nil(t, err) | ||
|
||
v.HostsToLower() | ||
v.GroupsToLower() | ||
v.AddVarsLowerCased("test_data") | ||
|
||
j, err := json.MarshalIndent(v, "", " ") | ||
assert.Nil(t, err) | ||
|
||
t.Run("unmarshal", func(t *testing.T) { | ||
var v2 InventoryData | ||
assert.Nil(t, json.Unmarshal(j, &v2)) | ||
assert.Equal(t, v.Hosts["host1"], v2.Hosts["host1"]) | ||
assert.Equal(t, v.Groups["tomcat"], v2.Groups["tomcat"]) | ||
}) | ||
} |
Oops, something went wrong.