working cfg
This commit is contained in:
parent
e2a197fe1f
commit
0507d13d63
2
main.go
2
main.go
@ -13,5 +13,5 @@ func main() {
|
||||
}
|
||||
|
||||
worldcfg := worldconfig.Parse(p.Worlddir + "world.mt")
|
||||
fmt.Println("Backend: ", worldcfg.Backend)
|
||||
fmt.Println("Config ", worldcfg)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user