diff --git a/layerconfig/layerconfig.go b/layerconfig/layerconfig.go new file mode 100644 index 0000000..7cecf3b --- /dev/null +++ b/layerconfig/layerconfig.go @@ -0,0 +1,47 @@ +package layerconfig + +import ( + "encoding/json" + "io/ioutil" +) + +type LayerContainer struct { + Layers []Layer `json:"layers"` +} + +type Layer struct { + Id int `json:"id"` + Name string `json:"name"` + To int `json:"to"` + From int `json:"from"` +} + +var DefaultLayers []Layer + +func init(){ + DefaultLayers = []Layer{ + Layer{ + Id: 0, + Name: "Base", + From: -16, + To: 160, + }, + } +} + +func ParseFile(filename string) ([]Layer, error) { + + data, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + + lc := LayerContainer{} + + err = json.Unmarshal(data, &lc) + if err != nil { + return nil, err + } + + return lc.Layers, nil +} diff --git a/layerconfig/layerconfig_test.go b/layerconfig/layerconfig_test.go new file mode 100644 index 0000000..4e8cb5d --- /dev/null +++ b/layerconfig/layerconfig_test.go @@ -0,0 +1,26 @@ +package layerconfig + +import ( + "testing" +) + +func TestReadJson(t *testing.T){ + layers, err := ParseFile("./testdata/layers.json") + + if err != nil { + t.Fatal(err) + } + + if layers == nil { + t.Fatal("no data") + } + + if len(layers) != 1 { + t.Fatal("length mismatch") + } + + if layers[0].Name != "Base" { + t.Fatal("name mismatch") + } + +} diff --git a/layerconfig/testdata/layers.json b/layerconfig/testdata/layers.json new file mode 100644 index 0000000..8e405ff --- /dev/null +++ b/layerconfig/testdata/layers.json @@ -0,0 +1,8 @@ +{ + "layers": [{ + "id": 0, + "name": "Base", + "from": -16, + "to": 160 + }] +} \ No newline at end of file