1
0
forked from MTSR/mapserver

settings separation

This commit is contained in:
NatureFreshMilk 2019-02-12 13:58:48 +01:00
parent 35e34ad74e
commit dc056e7e9c
3 changed files with 110 additions and 98 deletions

View File

@ -25,7 +25,7 @@ type App struct {
Blockdb db.DBAccessor
Objectdb mapobjectdb.DBAccessor
TileDB *tiledb.TileDB
Settings *settings.Settings
Settings settings.Settings
BlockAccessor *mapblockaccessor.MapBlockAccessor
Colormapping *colormapping.ColorMapping

View File

@ -0,0 +1,100 @@
package settings
import (
"mapserver/mapobjectdb"
"strconv"
)
type DBSettings struct {
db mapobjectdb.DBAccessor
}
func New(db mapobjectdb.DBAccessor) Settings {
return &DBSettings{
db: db,
}
}
func (this *DBSettings) GetString(key string, defaultValue string) string {
str, err := this.db.GetSetting(key, defaultValue)
if err != nil {
panic(err)
}
return str
}
func (this *DBSettings) SetString(key string, value string) {
err := this.db.SetSetting(key, value)
if err != nil {
panic(err)
}
}
func (this *DBSettings) GetInt(key string, defaultValue int) int {
str, err := this.db.GetSetting(key, strconv.Itoa(defaultValue))
if err != nil {
panic(err)
}
value, err := strconv.Atoi(str)
if err != nil {
panic(err)
}
return value
}
func (this *DBSettings) SetInt(key string, value int) {
err := this.db.SetSetting(key, strconv.Itoa(value))
if err != nil {
panic(err)
}
}
func (this *DBSettings) GetInt64(key string, defaultValue int64) int64 {
str, err := this.db.GetSetting(key, strconv.FormatInt(defaultValue, 10))
if err != nil {
panic(err)
}
value, err := strconv.ParseInt(str, 10, 64)
if err != nil {
panic(err)
}
return value
}
func (this *DBSettings) SetInt64(key string, value int64) {
err := this.db.SetSetting(key, strconv.FormatInt(value, 10))
if err != nil {
panic(err)
}
}
func (this *DBSettings) GetBool(key string, defaultValue bool) bool {
defStr := "false"
if defaultValue {
defStr = "true"
}
str, err := this.db.GetSetting(key, defStr)
if err != nil {
panic(err)
}
return str == "true"
}
func (this *DBSettings) SetBool(key string, value bool) {
defStr := "false"
if value {
defStr = "true"
}
err := this.db.SetSetting(key, defStr)
if err != nil {
panic(err)
}
}

View File

@ -1,10 +1,5 @@
package settings
import (
"mapserver/mapobjectdb"
"strconv"
)
const (
SETTING_LAST_MTIME = "last_mtime"
SETTING_LASTX = "last_x"
@ -14,96 +9,13 @@ const (
SETTING_LEGACY_PROCESSED = "legacy_processed"
)
type Settings struct {
db mapobjectdb.DBAccessor
}
func New(db mapobjectdb.DBAccessor) *Settings {
return &Settings{
db: db,
}
}
func (this *Settings) GetString(key string, defaultValue string) string {
str, err := this.db.GetSetting(key, defaultValue)
if err != nil {
panic(err)
}
return str
}
func (this *Settings) SetString(key string, value string) {
err := this.db.SetSetting(key, value)
if err != nil {
panic(err)
}
}
func (this *Settings) GetInt(key string, defaultValue int) int {
str, err := this.db.GetSetting(key, strconv.Itoa(defaultValue))
if err != nil {
panic(err)
}
value, err := strconv.Atoi(str)
if err != nil {
panic(err)
}
return value
}
func (this *Settings) SetInt(key string, value int) {
err := this.db.SetSetting(key, strconv.Itoa(value))
if err != nil {
panic(err)
}
}
func (this *Settings) GetInt64(key string, defaultValue int64) int64 {
str, err := this.db.GetSetting(key, strconv.FormatInt(defaultValue, 10))
if err != nil {
panic(err)
}
value, err := strconv.ParseInt(str, 10, 64)
if err != nil {
panic(err)
}
return value
}
func (this *Settings) SetInt64(key string, value int64) {
err := this.db.SetSetting(key, strconv.FormatInt(value, 10))
if err != nil {
panic(err)
}
}
func (this *Settings) GetBool(key string, defaultValue bool) bool {
defStr := "false"
if defaultValue {
defStr = "true"
}
str, err := this.db.GetSetting(key, defStr)
if err != nil {
panic(err)
}
return str == "true"
}
func (this *Settings) SetBool(key string, value bool) {
defStr := "false"
if value {
defStr = "true"
}
err := this.db.SetSetting(key, defStr)
if err != nil {
panic(err)
}
type Settings interface {
GetString(key string, defaultValue string) string
SetString(key string, value string)
GetInt(key string, defaultValue int) int
SetInt(key string, value int)
GetInt64(key string, defaultValue int64) int64
SetInt64(key string, value int64)
GetBool(key string, defaultValue bool) bool
SetBool(key string, value bool)
}