From 0507d13d6395e009b3cbac2d08619623e1080044 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sat, 5 Jan 2019 17:21:24 +0100 Subject: [PATCH] working cfg --- Makefile | 6 +++++ main.go | 2 +- worldconfig/parse.go | 49 +++++++++++++++++++-------------------- worldconfig/parse_test.go | 10 ++------ 4 files changed, 33 insertions(+), 34 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..83baeac --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ + +test: + go test ./... + +build: + go build . diff --git a/main.go b/main.go index 2c7bfb8..a0baa21 100644 --- a/main.go +++ b/main.go @@ -13,5 +13,5 @@ func main() { } worldcfg := worldconfig.Parse(p.Worlddir + "world.mt") - fmt.Println("Backend: ", worldcfg.Backend) + fmt.Println("Config ", worldcfg) } diff --git a/worldconfig/parse.go b/worldconfig/parse.go index 590b438..af1ba6e 100644 --- a/worldconfig/parse.go +++ b/worldconfig/parse.go @@ -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 diff --git a/worldconfig/parse_test.go b/worldconfig/parse_test.go index 42f8316..f275566 100644 --- a/worldconfig/parse_test.go +++ b/worldconfig/parse_test.go @@ -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") }