working cfg

This commit is contained in:
Thomas Rudin 2019-01-05 17:21:24 +01:00
parent e2a197fe1f
commit 0507d13d63
4 changed files with 33 additions and 34 deletions

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
test:
go test ./...
build:
go build .

View File

@ -13,5 +13,5 @@ func main() {
}
worldcfg := worldconfig.Parse(p.Worlddir + "world.mt")
fmt.Println("Backend: ", worldcfg.Backend)
fmt.Println("Config ", worldcfg)
}

View File

@ -33,14 +33,14 @@ type WorldConfig struct {
Backend string
PlayerBackend string
PsqlConnection *PsqlConfig
PsqlPlayerConnection *PsqlConfig
PsqlConnection PsqlConfig
PsqlPlayerConnection PsqlConfig
}
func parseConnectionString(str string) *PsqlConfig {
func parseConnectionString(str string) PsqlConfig {
cfg := PsqlConfig{}
pairs := strings.Split(str, "[ ]")
pairs := strings.Split(str, " ")
for _, pair := range pairs {
fmt.Println(pair)
kv := strings.Split(pair, "=")
@ -58,7 +58,7 @@ func parseConnectionString(str string) *PsqlConfig {
}
}
return &cfg
return cfg
}
func Parse(filename string) WorldConfig {
@ -71,28 +71,27 @@ func Parse(filename string) WorldConfig {
cfg := WorldConfig{}
scanner := bufio.NewScanner(file)
lastPart := ""
for scanner.Scan() {
sc := bufio.NewScanner(strings.NewReader(scanner.Text()))
sc.Split(bufio.ScanWords)
for sc.Scan() {
switch (lastPart) {
case CONFIG_BACKEND:
cfg.Backend = sc.Text()
case CONFIG_PLAYER_BACKEND:
cfg.PlayerBackend = sc.Text()
case CONFIG_PSQL_CONNECTION:
cfg.PsqlConnection = parseConnectionString(sc.Text())
continue
case CONFIG_PSQL_PLAYER_CONNECTION:
cfg.PsqlPlayerConnection = parseConnectionString(sc.Text())
continue
}
if sc.Text() != "=" {
lastPart = sc.Text()
}
line := scanner.Text()
sepIndex := strings.Index(line, "=")
if sepIndex < 0 {
continue
}
valueStr := strings.Trim(line[sepIndex+1:], " ")
keyStr := strings.Trim(line[:sepIndex], " ")
switch keyStr {
case CONFIG_BACKEND:
cfg.Backend = valueStr
case CONFIG_PLAYER_BACKEND:
cfg.PlayerBackend = valueStr
case CONFIG_PSQL_CONNECTION:
cfg.PsqlConnection = parseConnectionString(valueStr)
case CONFIG_PSQL_PLAYER_CONNECTION:
cfg.PsqlPlayerConnection = parseConnectionString(valueStr)
}
}
return cfg

View File

@ -2,6 +2,7 @@ package worldconfig_test
import (
"testing"
"fmt"
worldconfig "mapserver/worldconfig"
)
@ -17,6 +18,7 @@ func TestParseSqlite(t *testing.T) {
func TestParsePostgres(t *testing.T) {
cfg := worldconfig.Parse("./testdata/world.mt.postgres")
fmt.Println(cfg)
if cfg.Backend != worldconfig.BACKEND_POSTGRES {
t.Fatal("not postgres")
}
@ -25,14 +27,6 @@ func TestParsePostgres(t *testing.T) {
t.Fatal("not postgres")
}
if cfg.PsqlConnection == nil {
t.Fatal("no connection")
}
if cfg.PsqlPlayerConnection == nil {
t.Fatal("no connection")
}
if cfg.PsqlConnection.Host != "postgres" {
t.Fatal("param err")
}