1
0
forked from MTSR/mapserver

layer parsing

This commit is contained in:
NatureFreshMilk 2019-01-17 10:23:48 +01:00
parent 914e2cb667
commit abbf16b4bd
3 changed files with 81 additions and 0 deletions

View File

@ -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
}

View File

@ -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")
}
}

8
layerconfig/testdata/layers.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"layers": [{
"id": 0,
"name": "Base",
"from": -16,
"to": 160
}]
}