1
0
forked from MTSR/mapserver

working parse

This commit is contained in:
Thomas Rudin 2019-01-04 13:02:17 +01:00
parent 013985f76f
commit 7863099925
5 changed files with 84 additions and 12 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
mapserver
mapserver
world.mt

View File

@ -2,12 +2,16 @@ package main
import (
"mapserver/params"
"mapserver/worldconfig"
"fmt"
)
func main() {
params.Parse()
p := params.Params()
p := params.Parse()
if p.Help {
return
}
worldcfg := worldconfig.Parse(p.Worlddir + "world.mt")
fmt.Println("Backend: ", worldcfg.Backend)
}

View File

@ -10,13 +10,7 @@ type ParamsType struct {
Help bool
}
var params ParamsType
func Params() ParamsType {
return params
}
func Parse() {
func Parse() ParamsType {
params := ParamsType{}
flag.StringVar(&(params.Worlddir), "worlddir", "./", "world directory")
@ -27,4 +21,6 @@ func Parse() {
if params.Help {
flag.PrintDefaults()
}
return params
}

View File

@ -1,8 +1,53 @@
package workdconfig
package worldconfig
import (
"bufio"
"os"
"strings"
)
const (
BACKEND_SQLITE3 string = "sqlite3"
BACKEND_FILES string = "files"
BACKEND_POSTGRES string = "postgresql"
)
const (
CONFIG_BACKEND string = "backend"
CONFIG_PLAYER_BACKEND string = "player_backend"
)
type WorldConfig struct {
Backend string
PlayerBackend string
}
func Parse(data []byte) WorldConfig {
func Parse(filename string) WorldConfig {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
cfg := WorldConfig{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
sc := bufio.NewScanner(strings.NewReader(scanner.Text()))
sc.Split(bufio.ScanWords)
lastPart := ""
for sc.Scan() {
switch (lastPart) {
case CONFIG_BACKEND:
cfg.Backend = sc.Text()
case CONFIG_PLAYER_BACKEND:
cfg.PlayerBackend = sc.Text()
}
if sc.Text() != "=" {
lastPart = sc.Text()
}
}
}
return cfg
}

26
worldconfig/parse_test.go Normal file
View File

@ -0,0 +1,26 @@
package worldconfig_test
import (
"testing"
worldconfig "mapserver/worldconfig"
)
func TestParseSqlite(t *testing.T) {
cfg := worldconfig.Parse("./testdata/world.mt.sqlite")
if cfg.Backend != worldconfig.BACKEND_SQLITE3 {
t.Fatal("not sqlite3")
}
if cfg.PlayerBackend != worldconfig.BACKEND_FILES {
t.Fatal("not files")
}
}
func TestParsePostgres(t *testing.T) {
cfg := worldconfig.Parse("./testdata/world.mt.postgres")
if cfg.Backend != worldconfig.BACKEND_POSTGRES {
t.Fatal("not postgres")
}
if cfg.PlayerBackend != worldconfig.BACKEND_POSTGRES {
t.Fatal("not postgres")
}
}