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

View File

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