config: add some structures for configuration

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-10-13 16:48:57 -04:00
parent 1d0bd01fe7
commit 49139446ab
2 changed files with 84 additions and 0 deletions

24
config.go Normal file
View File

@ -0,0 +1,24 @@
package main
import "encoding/xml"
type Config struct {
XMLName xml.Name `xml:"config"`
SyncRefs []SyncRef `xml:"refs>ref"`
Groups []Group `xml:"groups>group"`
}
type Group struct {
XMLName xml.Name `xml:"group"`
ID string `xml:"id,attr"`
Active bool `xml:"active,attr"`
Contains []string `xml:"contains>refID"`
}
type SyncRef struct {
XMLName xml.Name `xml:"ref"`
ID string `xml:"id,attr"`
URI string
ReadOnly bool
Deletes bool
}

60
config_test.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"encoding/xml"
"net/url"
"testing"
uuid "github.com/satori/go.uuid"
)
func TestConfig(t *testing.T) {
u, err := url.Parse("file:///tmp/tmp.xJiSJUcyus")
if err != nil {
t.Fatal(err)
}
so1 := SyncRef{
ID: uuid.NewV4().String(),
URI: u.String(),
}
so2 := SyncRef{
ID: uuid.NewV4().String(),
URI: "file:///tmp/tmp.bhSM7oRner",
}
config := Config{
SyncRefs: []SyncRef{
so1,
so2,
},
Groups: []Group{
Group{
ID: uuid.NewV4().String(),
Contains: []string{so1.ID, so2.ID},
},
},
}
buf, err := xml.MarshalIndent(config, "", " ")
if err != nil {
t.Fatal(err)
}
newConfig := Config{}
if err := xml.Unmarshal(buf, &newConfig); err != nil {
t.Fatal(err)
}
for _, group := range newConfig.Groups {
for _, contain := range group.Contains {
found := false
for _, ref := range newConfig.SyncRefs {
if ref.ID == contain {
found = true
}
}
if !found {
t.Errorf("expected all group ref IDs to exist, but %q did not", contain)
}
}
}
}