From abbf16b4bdc6438c80c85f6f092cb61528ef2ced Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Thu, 17 Jan 2019 10:23:48 +0100 Subject: [PATCH] layer parsing --- layerconfig/layerconfig.go | 47 ++++++++++++++++++++++++++++++++ layerconfig/layerconfig_test.go | 26 ++++++++++++++++++ layerconfig/testdata/layers.json | 8 ++++++ 3 files changed, 81 insertions(+) create mode 100644 layerconfig/layerconfig.go create mode 100644 layerconfig/layerconfig_test.go create mode 100644 layerconfig/testdata/layers.json 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