1
0
forked from MTSR/mapserver
mapserver/travelnetparser/parser.go
BuckarooBanzay 201d983ac0 os.ReadFile
2023-01-20 18:04:27 +01:00

76 lines
1.5 KiB
Go

package travelnetparser
import (
"errors"
"fmt"
"mapserver/luaparser"
"os"
)
type GenericPos struct {
X int `json:"x"`
Y int `json:"y"`
Z int `json:"z"`
}
type Travelnet struct {
Timestamp int64 `json:"timestamp"`
Pos *GenericPos `json:"pos"`
}
func ParseFile(filename string) (map[string]map[string]map[string]*Travelnet, error) {
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return Parse(content)
}
// owner -> network -> name ->Travelnet-Data
func Parse(data []byte) (map[string]map[string]map[string]*Travelnet, error) {
p := luaparser.New()
travelnets := make(map[string]map[string]map[string]*Travelnet)
ownermap, err := p.ParseMap(string(data[:]))
if err != nil {
return nil, err
}
for owner, rawnetworkmap := range ownermap {
fmt.Println(owner)
networkmap, ok := rawnetworkmap.(map[string]interface{})
if !ok {
return nil, errors.New("networkmap parsing failed")
}
for network, rawstationmap := range networkmap {
fmt.Println("+", network)
stationmap, ok := rawstationmap.(map[string]interface{})
if !ok {
return nil, errors.New("stationmap parsing failed")
}
for station, rawentries := range stationmap {
fmt.Println("++", station)
entries, ok := rawentries.(map[string]interface{})
if !ok {
return nil, errors.New("entries parsing failed")
}
for entry := range entries {
fmt.Println("+++", entry)
//TODO:
}
}
}
}
return travelnets, nil
}