From f8f524013e78b6622993c344ca74d37b122dfd28 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 18 Jan 2019 09:13:37 +0100 Subject: [PATCH] go fmt --- app/app.go | 34 ++++++------ app/config.go | 42 +++++++-------- app/setup.go | 76 +++++++++++++-------------- coords/tileconvert.go | 4 +- coords/tilecoords.go | 10 ++-- db/sqlite.go | 10 ++-- db/sqlite_test.go | 1 - initialrenderer/renderer.go | 91 ++++++++++++++++----------------- layerconfig/layerconfig.go | 52 +++++++++---------- layerconfig/layerconfig_test.go | 30 +++++------ main.go | 32 ++++++------ mapblockrenderer/renderer.go | 28 +++++----- params/params.go | 8 +-- tiledb/accessor.go | 6 +-- tiledb/sqlite.go | 7 ++- tilerenderer/renderer.go | 37 +++++++------- tilerenderer/renderer_test.go | 34 ++++++------ 17 files changed, 247 insertions(+), 255 deletions(-) diff --git a/app/app.go b/app/app.go index 5c60f24..b38016a 100644 --- a/app/app.go +++ b/app/app.go @@ -1,14 +1,14 @@ package app import ( - "mapserver/params" - "mapserver/worldconfig" - "mapserver/db" - "mapserver/mapblockaccessor" - "mapserver/colormapping" - "mapserver/mapblockrenderer" - "mapserver/tiledb" - "mapserver/tilerenderer" + "mapserver/colormapping" + "mapserver/db" + "mapserver/mapblockaccessor" + "mapserver/mapblockrenderer" + "mapserver/params" + "mapserver/tiledb" + "mapserver/tilerenderer" + "mapserver/worldconfig" ) const ( @@ -16,15 +16,15 @@ const ( ) type App struct { - Params params.ParamsType - Config *Config - Worldconfig worldconfig.WorldConfig + Params params.ParamsType + Config *Config + Worldconfig worldconfig.WorldConfig - Blockdb db.DBAccessor - Tiledb tiledb.DBAccessor + Blockdb db.DBAccessor + Tiledb tiledb.DBAccessor - BlockAccessor *mapblockaccessor.MapBlockAccessor - Colormapping *colormapping.ColorMapping - Mapblockrenderer *mapblockrenderer.MapBlockRenderer - Tilerenderer *tilerenderer.TileRenderer + BlockAccessor *mapblockaccessor.MapBlockAccessor + Colormapping *colormapping.ColorMapping + Mapblockrenderer *mapblockrenderer.MapBlockRenderer + Tilerenderer *tilerenderer.TileRenderer } diff --git a/app/config.go b/app/config.go index 0dbd5fc..0ee408b 100644 --- a/app/config.go +++ b/app/config.go @@ -1,34 +1,34 @@ package app import ( - "encoding/json" - "io/ioutil" - "os" + "encoding/json" + "io/ioutil" + "os" ) type Config struct { - Port int `json:"port"` - EnableInitialRendering bool `json:"enableinitialrendering"` + Port int `json:"port"` + EnableInitialRendering bool `json:"enableinitialrendering"` } func ParseConfig(filename string) (*Config, error) { - cfg := Config{ - Port: 80, - EnableInitialRendering: true, - } + cfg := Config{ + Port: 80, + EnableInitialRendering: true, + } - info, err := os.Stat(filename) - if info != nil && err == nil { - data, err := ioutil.ReadFile(filename) - if err != nil { - return nil, err - } + info, err := os.Stat(filename) + if info != nil && err == nil { + data, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } - err = json.Unmarshal(data, &cfg) - if err != nil { - return nil, err - } - } + err = json.Unmarshal(data, &cfg) + if err != nil { + return nil, err + } + } - return &cfg, nil + return &cfg, nil } diff --git a/app/setup.go b/app/setup.go index 9dd1d3d..828a6d2 100644 --- a/app/setup.go +++ b/app/setup.go @@ -1,53 +1,53 @@ package app import ( - "mapserver/params" - "mapserver/worldconfig" - "mapserver/db" - "mapserver/mapblockaccessor" - "mapserver/colormapping" - "mapserver/mapblockrenderer" - "mapserver/tiledb" - "mapserver/tilerenderer" - "mapserver/layerconfig" + "mapserver/colormapping" + "mapserver/db" + "mapserver/layerconfig" + "mapserver/mapblockaccessor" + "mapserver/mapblockrenderer" + "mapserver/params" + "mapserver/tiledb" + "mapserver/tilerenderer" + "mapserver/worldconfig" - "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" - "errors" + "errors" ) func Setup(p params.ParamsType, cfg *Config) (*App, error) { - a := App{} - a.Params = p - a.Config = cfg + a := App{} + a.Params = p + a.Config = cfg - //Parse world config + //Parse world config - a.Worldconfig = worldconfig.Parse("world.mt") - logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver") + a.Worldconfig = worldconfig.Parse("world.mt") + logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver") - if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 { - return nil, errors.New("no supported backend found!") - } + if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 { + return nil, errors.New("no supported backend found!") + } - //create db accessor - var err error - a.Blockdb, err = db.NewSqliteAccessor("map.sqlite") + //create db accessor + var err error + a.Blockdb, err = db.NewSqliteAccessor("map.sqlite") if err != nil { return nil, err } - //migrate block db + //migrate block db err = a.Blockdb.Migrate() if err != nil { return nil, err } - //mapblock accessor + //mapblock accessor a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb) - //color mapping + //color mapping a.Colormapping = colormapping.NewColorMapping() err = a.Colormapping.LoadVFSColors(false, "/colors.txt") @@ -55,10 +55,10 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { return nil, err } - //mapblock renderer - a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping) + //mapblock renderer + a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping) - //tile database + //tile database a.Tiledb, err = tiledb.NewSqliteAccessor("tiles.sqlite") @@ -66,7 +66,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { return nil, err } - //migrate tile database + //migrate tile database err = a.Tiledb.Migrate() @@ -74,13 +74,13 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) { return nil, err } - //setup tile renderer - a.Tilerenderer = tilerenderer.NewTileRenderer( - a.Mapblockrenderer, - a.Tiledb, - a.Blockdb, - layerconfig.DefaultLayers, - ) + //setup tile renderer + a.Tilerenderer = tilerenderer.NewTileRenderer( + a.Mapblockrenderer, + a.Tiledb, + a.Blockdb, + layerconfig.DefaultLayers, + ) - return &a, nil + return &a, nil } diff --git a/coords/tileconvert.go b/coords/tileconvert.go index f5b25bc..cd1c32a 100644 --- a/coords/tileconvert.go +++ b/coords/tileconvert.go @@ -19,8 +19,8 @@ func GetMapBlockRangeFromTile(tc TileCoords, y int) MapBlockRange { mapBlockX1 := tc.X * scale mapBlockZ1 := (tc.Y * scale * -1) - 1 - mapBlockX2 := mapBlockX1 + scale-1 - mapBlockZ2 := (mapBlockZ1 + ((scale-1) * -1)) + mapBlockX2 := mapBlockX1 + scale - 1 + mapBlockZ2 := (mapBlockZ1 + ((scale - 1) * -1)) return MapBlockRange{ Pos1: NewMapBlockCoords(mapBlockX1, y, mapBlockZ1), diff --git a/coords/tilecoords.go b/coords/tilecoords.go index 5b50ec1..ac61839 100644 --- a/coords/tilecoords.go +++ b/coords/tilecoords.go @@ -5,8 +5,8 @@ import ( ) type TileCoords struct { - X, Y int - Zoom int + X, Y int + Zoom int LayerId int } @@ -20,9 +20,9 @@ func NewTileCoords(x, y, zoom int, layerId int) TileCoords { func (tc TileCoords) GetZoomedOutTile() TileCoords { return TileCoords{ - X: int(math.Floor(float64(tc.X) / 2.0)), - Y: int(math.Floor(float64(tc.Y) / 2.0)), - Zoom: tc.Zoom - 1, + X: int(math.Floor(float64(tc.X) / 2.0)), + Y: int(math.Floor(float64(tc.Y) / 2.0)), + Zoom: tc.Zoom - 1, LayerId: tc.LayerId} } diff --git a/db/sqlite.go b/db/sqlite.go index 972dee9..288c17e 100644 --- a/db/sqlite.go +++ b/db/sqlite.go @@ -2,12 +2,12 @@ package db import ( "database/sql" + "errors" _ "github.com/mattn/go-sqlite3" "github.com/sirupsen/logrus" "mapserver/coords" - "time" "strings" - "errors" + "time" ) const migrateScript = ` @@ -104,7 +104,7 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) { return nil, nil } -func sortAsc(a, b int) (int, int){ +func sortAsc(a, b int) (int, int) { if a > b { return b, a } else { @@ -112,7 +112,7 @@ func sortAsc(a, b int) (int, int){ } } -func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error){ +func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error) { poslist := make([]interface{}, 0) @@ -123,7 +123,7 @@ func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.M for x := minX; x <= maxX; x++ { for y := minY; y <= maxY; y++ { for z := minZ; z <= maxZ; z++ { - poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(x,y,z))) + poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(x, y, z))) } } } diff --git a/db/sqlite_test.go b/db/sqlite_test.go index 53b5cc1..7439c1a 100644 --- a/db/sqlite_test.go +++ b/db/sqlite_test.go @@ -75,7 +75,6 @@ func TestMigrateAndQuery(t *testing.T) { } - func TestMigrateAndQueryStride(t *testing.T) { tmpfile, err := ioutil.TempFile("", "TestMigrateAndQueryStride.*.sqlite") if err != nil { diff --git a/initialrenderer/renderer.go b/initialrenderer/renderer.go index 2c412db..e3480ee 100644 --- a/initialrenderer/renderer.go +++ b/initialrenderer/renderer.go @@ -1,71 +1,68 @@ package initialrenderer import ( - "mapserver/tilerenderer" - "mapserver/layerconfig" - "mapserver/coords" - "github.com/sirupsen/logrus" - "time" - + "github.com/sirupsen/logrus" + "mapserver/coords" + "mapserver/layerconfig" + "mapserver/tilerenderer" + "time" ) -func worker(tr *tilerenderer.TileRenderer, jobs <-chan coords.TileCoords){ - for coord := range(jobs){ - tr.Render(coord) - } +func worker(tr *tilerenderer.TileRenderer, jobs <-chan coords.TileCoords) { + for coord := range jobs { + tr.Render(coord) + } } func Render(tr *tilerenderer.TileRenderer, - layers []layerconfig.Layer){ + layers []layerconfig.Layer) { - start := time.Now() - complete_count := 256*256 - current_count := 0 - perf_count := 0 + start := time.Now() + complete_count := 256 * 256 + current_count := 0 + perf_count := 0 - jobs := make(chan coords.TileCoords, 100) + jobs := make(chan coords.TileCoords, 100) - go worker(tr, jobs) - go worker(tr, jobs) - go worker(tr, jobs) - go worker(tr, jobs) + go worker(tr, jobs) + go worker(tr, jobs) + go worker(tr, jobs) + go worker(tr, jobs) - for _, layer := range(layers) { + for _, layer := range layers { - //zoom 10 iterator - for x := -127; x<128; x++ { - for y := -127; y<128; y++ { - tc := coords.NewTileCoords(x,y,9,layer.Id) - jobs <- tc - current_count++ - perf_count++ + //zoom 10 iterator + for x := -127; x < 128; x++ { + for y := -127; y < 128; y++ { + tc := coords.NewTileCoords(x, y, 9, layer.Id) + jobs <- tc + current_count++ + perf_count++ - if time.Now().Sub(start).Seconds() > 2 { - start = time.Now() - progress := float64(current_count) / float64(complete_count) * 100 + if time.Now().Sub(start).Seconds() > 2 { + start = time.Now() + progress := float64(current_count) / float64(complete_count) * 100 - fields := logrus.Fields{ - "x": x, - "y": y, - "progress(%)": progress, - "layer": layer.Name, - "perf": perf_count, - } + fields := logrus.Fields{ + "x": x, + "y": y, + "progress(%)": progress, + "layer": layer.Name, + "perf": perf_count, + } - perf_count = 0 - logrus.WithFields(fields).Info("Initial render progress") - } - } - } + perf_count = 0 + logrus.WithFields(fields).Info("Initial render progress") + } + } + } + } - } - - close(jobs) + close(jobs) } - // zoom:1 == length=1 // zoom:2 == length=2 // zoom:3 == length=4 diff --git a/layerconfig/layerconfig.go b/layerconfig/layerconfig.go index 7cecf3b..08e828b 100644 --- a/layerconfig/layerconfig.go +++ b/layerconfig/layerconfig.go @@ -1,47 +1,47 @@ package layerconfig import ( - "encoding/json" - "io/ioutil" + "encoding/json" + "io/ioutil" ) type LayerContainer struct { - Layers []Layer `json:"layers"` + Layers []Layer `json:"layers"` } type Layer struct { - Id int `json:"id"` - Name string `json:"name"` - To int `json:"to"` - From int `json:"from"` + Id int `json:"id"` + Name string `json:"name"` + To int `json:"to"` + From int `json:"from"` } var DefaultLayers []Layer -func init(){ - DefaultLayers = []Layer{ - Layer{ - Id: 0, - Name: "Base", - From: -16, - To: 160, - }, - } +func init() { + DefaultLayers = []Layer{ + Layer{ + Id: 0, + Name: "Base", + From: -16, + To: 160, + }, + } } func ParseFile(filename string) ([]Layer, error) { - data, err := ioutil.ReadFile(filename) - if err != nil { - return nil, err - } + data, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } - lc := LayerContainer{} + lc := LayerContainer{} - err = json.Unmarshal(data, &lc) - if err != nil { - return nil, err - } + err = json.Unmarshal(data, &lc) + if err != nil { + return nil, err + } - return lc.Layers, nil + return lc.Layers, nil } diff --git a/layerconfig/layerconfig_test.go b/layerconfig/layerconfig_test.go index 4e8cb5d..57e9416 100644 --- a/layerconfig/layerconfig_test.go +++ b/layerconfig/layerconfig_test.go @@ -1,26 +1,26 @@ package layerconfig import ( - "testing" + "testing" ) -func TestReadJson(t *testing.T){ - layers, err := ParseFile("./testdata/layers.json") +func TestReadJson(t *testing.T) { + layers, err := ParseFile("./testdata/layers.json") - if err != nil { - t.Fatal(err) - } + if err != nil { + t.Fatal(err) + } - if layers == nil { - t.Fatal("no data") - } + if layers == nil { + t.Fatal("no data") + } - if len(layers) != 1 { - t.Fatal("length mismatch") - } + if len(layers) != 1 { + t.Fatal("length mismatch") + } - if layers[0].Name != "Base" { - t.Fatal("name mismatch") - } + if layers[0].Name != "Base" { + t.Fatal("name mismatch") + } } diff --git a/main.go b/main.go index 36b4335..ceec6fe 100644 --- a/main.go +++ b/main.go @@ -1,35 +1,33 @@ package main import ( - "mapserver/initialrenderer" - "github.com/sirupsen/logrus" - "mapserver/layerconfig" - "mapserver/app" - "mapserver/params" "encoding/json" + "github.com/sirupsen/logrus" + "mapserver/app" + "mapserver/initialrenderer" + "mapserver/layerconfig" + "mapserver/params" "fmt" ) - - func main() { logrus.SetLevel(logrus.InfoLevel) //Parse command line - p := params.Parse() + p := params.Parse() - if p.Help { - params.PrintHelp() - return - } + if p.Help { + params.PrintHelp() + return + } - if p.Version { - fmt.Print("Mapserver version: ") - fmt.Println(app.Version) - return - } + if p.Version { + fmt.Print("Mapserver version: ") + fmt.Println(app.Version) + return + } //parse Config cfg, err := app.ParseConfig("mapserver.json") diff --git a/mapblockrenderer/renderer.go b/mapblockrenderer/renderer.go index 80185b4..58ab75e 100644 --- a/mapblockrenderer/renderer.go +++ b/mapblockrenderer/renderer.go @@ -4,8 +4,8 @@ import ( "errors" "github.com/sirupsen/logrus" "image" - "image/draw" "image/color" + "image/draw" "mapserver/colormapping" "mapserver/coords" "mapserver/mapblockaccessor" @@ -13,8 +13,8 @@ import ( ) type MapBlockRenderer struct { - accessor *mapblockaccessor.MapBlockAccessor - colors *colormapping.ColorMapping + accessor *mapblockaccessor.MapBlockAccessor + colors *colormapping.ColorMapping enableShadow bool } @@ -33,23 +33,23 @@ func IsViewBlocking(nodeName string) bool { } func clamp(num int) uint8 { - if num < 0 { - return 0 - } + if num < 0 { + return 0 + } - if num > 255 { - return 255 - } + if num > 255 { + return 255 + } - return uint8(num) + return uint8(num) } func addColorComponent(c *color.RGBA, value int) *color.RGBA { return &color.RGBA{ - R: clamp( int(c.R) + value ), - G: clamp( int(c.G) + value ), - B: clamp( int(c.B) + value ), - A: clamp( int(c.A) + value ), + R: clamp(int(c.R) + value), + G: clamp(int(c.G) + value), + B: clamp(int(c.B) + value), + A: clamp(int(c.A) + value), } } diff --git a/params/params.go b/params/params.go index 74c4cdc..c1e1dc0 100644 --- a/params/params.go +++ b/params/params.go @@ -5,9 +5,9 @@ import ( ) type ParamsType struct { - Help bool - Version bool - Dumpconfig bool + Help bool + Version bool + Dumpconfig bool } func Parse() ParamsType { @@ -21,6 +21,6 @@ func Parse() ParamsType { return params } -func PrintHelp(){ +func PrintHelp() { flag.PrintDefaults() } diff --git a/tiledb/accessor.go b/tiledb/accessor.go index 3e20c53..032a767 100644 --- a/tiledb/accessor.go +++ b/tiledb/accessor.go @@ -5,9 +5,9 @@ import ( ) type Tile struct { - Pos coords.TileCoords - Data []byte - Mtime int64 + Pos coords.TileCoords + Data []byte + Mtime int64 } type DBAccessor interface { diff --git a/tiledb/sqlite.go b/tiledb/sqlite.go index 9d6be10..a022a7a 100644 --- a/tiledb/sqlite.go +++ b/tiledb/sqlite.go @@ -69,9 +69,9 @@ func (db *Sqlite3Accessor) GetTile(pos coords.TileCoords) (*Tile, error) { } mb := Tile{ - Pos: pos, - Data: data, - Mtime: mtime, + Pos: pos, + Data: data, + Mtime: mtime, } return &mb, nil @@ -100,7 +100,6 @@ func (db *Sqlite3Accessor) RemoveTile(pos coords.TileCoords) error { return err } - func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) { db, err := sql.Open("sqlite3", filename) if err != nil { diff --git a/tilerenderer/renderer.go b/tilerenderer/renderer.go index 4349434..a29470c 100644 --- a/tilerenderer/renderer.go +++ b/tilerenderer/renderer.go @@ -1,25 +1,25 @@ package tilerenderer import ( - "errors" - "image" - "time" "bytes" - "image/png" - "image/draw" - "mapserver/db" - "mapserver/coords" - "mapserver/mapblockrenderer" - "mapserver/layerconfig" - "mapserver/tiledb" + "errors" "github.com/sirupsen/logrus" + "image" + "image/draw" + "image/png" + "mapserver/coords" + "mapserver/db" + "mapserver/layerconfig" + "mapserver/mapblockrenderer" + "mapserver/tiledb" + "time" ) type TileRenderer struct { mapblockrenderer *mapblockrenderer.MapBlockRenderer - layers []layerconfig.Layer - tdb tiledb.DBAccessor - dba db.DBAccessor + layers []layerconfig.Layer + tdb tiledb.DBAccessor + dba db.DBAccessor } func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer, @@ -29,14 +29,14 @@ func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer, return &TileRenderer{ mapblockrenderer: mapblockrenderer, - layers: layers, - tdb: tdb, - dba: dba, + layers: layers, + tdb: tdb, + dba: dba, } } const ( - IMG_SIZE = 256 + IMG_SIZE = 256 ) func (tr *TileRenderer) Render(tc coords.TileCoords) ([]byte, error) { @@ -90,7 +90,7 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error) var layer *layerconfig.Layer - for _, l := range(tr.layers) { + for _, l := range tr.layers { if l.Id == tc.LayerId { layer = &l } @@ -203,7 +203,6 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error) png.Encode(buf, img) } - tile := tiledb.Tile{Pos: tc, Data: buf.Bytes(), Mtime: time.Now().Unix()} tr.tdb.SetTile(&tile) diff --git a/tilerenderer/renderer_test.go b/tilerenderer/renderer_test.go index 3c311f0..09c1acb 100644 --- a/tilerenderer/renderer_test.go +++ b/tilerenderer/renderer_test.go @@ -1,19 +1,19 @@ package tilerenderer import ( + "bytes" "github.com/sirupsen/logrus" "io/ioutil" "mapserver/colormapping" + "mapserver/coords" "mapserver/db" - "mapserver/mapblockaccessor" - "mapserver/testutils" - "mapserver/mapblockrenderer" - "mapserver/coords" "mapserver/layerconfig" + "mapserver/mapblockaccessor" + "mapserver/mapblockrenderer" + "mapserver/testutils" "mapserver/tiledb" "os" "testing" - "bytes" ) func TestTileRender(t *testing.T) { @@ -52,23 +52,23 @@ func TestTileRender(t *testing.T) { tdb, _ := tiledb.NewSqliteAccessor(tiletmpfile.Name()) tdb.Migrate() - tr := NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers) + tr := NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers) - if tr == nil { - panic("no renderer") - } + if tr == nil { + panic("no renderer") + } - coord := coords.NewTileCoords(0,0,12,0) + coord := coords.NewTileCoords(0, 0, 12, 0) - data, err := tr.Render(coord) + data, err := tr.Render(coord) - if err != nil { - panic(err) - } + if err != nil { + panic(err) + } - if data == nil { - panic("no data") - } + if data == nil { + panic("no data") + } f, _ := os.Create("../output/0_0_12.png") bytes.NewReader(data).WriteTo(f)