refactor ennoblement/repository csv line parser, add dev.sh
This commit is contained in:
parent
00c63df35a
commit
323e9b4ca5
4
dev.sh
Executable file
4
dev.sh
Executable file
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
export MODE=development
|
||||
export GIN_MODE=debug
|
||||
go run main.go
|
|
@ -2,13 +2,7 @@ package repository
|
|||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tribalwarshelp/shared/models"
|
||||
)
|
||||
|
||||
func getCSVData(url string) ([][]string, error) {
|
||||
|
@ -19,33 +13,3 @@ func getCSVData(url string) ([][]string, error) {
|
|||
defer resp.Body.Close()
|
||||
return csv.NewReader(resp.Body).ReadAll()
|
||||
}
|
||||
|
||||
func parseLine(line []string, timezone string) (*models.Ennoblement, error) {
|
||||
loc, err := time.LoadLocation(timezone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Invalid timezone: %s", timezone)
|
||||
}
|
||||
if len(line) != 4 {
|
||||
return nil, fmt.Errorf("Invalid line format (should be village_id,timestamp,new_owner,old_owner)")
|
||||
}
|
||||
e := &models.Ennoblement{}
|
||||
e.VillageID, err = strconv.Atoi(line[0])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "*models.Ennoblement.VillageID")
|
||||
}
|
||||
timestamp, err := strconv.Atoi(line[1])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "timestamp")
|
||||
}
|
||||
e.EnnobledAt = time.Unix(int64(timestamp), 0).In(loc)
|
||||
e.NewOwnerID, err = strconv.Atoi(line[2])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "*models.Ennoblement.NewOwnerID")
|
||||
}
|
||||
e.OldOwnerID, err = strconv.Atoi(line[3])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "*models.Ennoblement.OldOwnerID")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
|
51
ennoblement/repository/line_parser.go
Normal file
51
ennoblement/repository/line_parser.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tribalwarshelp/shared/models"
|
||||
)
|
||||
|
||||
type lineParser struct {
|
||||
location *time.Location
|
||||
}
|
||||
|
||||
func newLineParser(timezone string) (*lineParser, error) {
|
||||
loc, err := time.LoadLocation(timezone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Invalid timezone: %s", timezone)
|
||||
}
|
||||
return &lineParser{
|
||||
location: loc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (parser *lineParser) parse(line []string) (*models.Ennoblement, error) {
|
||||
if len(line) != 4 {
|
||||
return nil, fmt.Errorf("Invalid line format (should be village_id,timestamp,new_owner,old_owner)")
|
||||
}
|
||||
var err error
|
||||
e := &models.Ennoblement{}
|
||||
e.VillageID, err = strconv.Atoi(line[0])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "*models.Ennoblement.VillageID")
|
||||
}
|
||||
timestamp, err := strconv.Atoi(line[1])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "timestamp")
|
||||
}
|
||||
e.EnnobledAt = time.Unix(int64(timestamp), 0).In(parser.location)
|
||||
e.NewOwnerID, err = strconv.Atoi(line[2])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "*models.Ennoblement.NewOwnerID")
|
||||
}
|
||||
e.OldOwnerID, err = strconv.Atoi(line[3])
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "*models.Ennoblement.OldOwnerID")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
|
@ -42,8 +42,12 @@ func (repo *pgRepository) Fetch(ctx context.Context, server string) ([]*models.E
|
|||
}
|
||||
|
||||
e := []*models.Ennoblement{}
|
||||
lineParser, err := newLineParser(s.LangVersion.Timezone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, line := range lines {
|
||||
ennoblement, err := parseLine(line, s.LangVersion.Timezone)
|
||||
ennoblement, err := lineParser.parse(line)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -7,7 +7,6 @@ require (
|
|||
github.com/gin-gonic/gin v1.6.3
|
||||
github.com/go-pg/pg/v10 v10.0.0-beta.1
|
||||
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
||||
github.com/go-redis/redis/v8 v8.0.0-beta.2 // indirect
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
|
|
7
go.sum
7
go.sum
|
@ -59,8 +59,6 @@ github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1
|
|||
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||
github.com/go-playground/validator/v10 v10.3.0 h1:nZU+7q+yJoFmwvNgv/LnPUkwPal62+b2xXj0AU1Es7o=
|
||||
github.com/go-playground/validator/v10 v10.3.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
|
||||
github.com/go-redis/redis/v8 v8.0.0-beta.2 h1:9S28J9QMBotgI3tGgXbX1Wk9i8QYC3Orw4bTLoPrQeI=
|
||||
github.com/go-redis/redis/v8 v8.0.0-beta.2/go.mod h1:o1M7JtsgfDYyv3o+gBn/jJ1LkqpnCrmil7PSppZGBak=
|
||||
github.com/gogo/protobuf v1.0.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
|
@ -160,10 +158,6 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
|
|||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200603143626-e91530ae0889 h1:6wXJ4Hdh7NlLlTiAtQknHIZYoTZfYtU8nBXQvhUMRik=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200603143626-e91530ae0889/go.mod h1:Ej+yG8aDKHPLRUVYxn7bT9zA0sdZZoGGgVJAAPGCK/o=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200603152200-61e5804c01ae h1:1fMU6i/EIDZafe1Ae93rt+gbVkNf/xX/REU015CtUOI=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200603152200-61e5804c01ae/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200604135345-b6b45da13005 h1:8n2DkkZKk+V0lGF9fgdFr8wux6Gr1C+HkZUSy8dMIE4=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200604135345-b6b45da13005/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
||||
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
|
@ -187,7 +181,6 @@ github.com/vmihailenco/msgpack/v5 v5.0.0-alpha.2/go.mod h1:LDfrk4wJpSFwkzNOJxrCW
|
|||
github.com/vmihailenco/tagparser v0.1.0/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
||||
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
|
||||
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
||||
go.opentelemetry.io/otel v0.5.0/go.mod h1:jzBIgIzK43Iu1BpDAXwqOd6UPsSAk+ewVZ5ofSXw4Ek=
|
||||
go.opentelemetry.io/otel v0.6.0 h1:+vkHm/XwJ7ekpISV2Ixew93gCrxTbuwTF5rSewnLLgw=
|
||||
go.opentelemetry.io/otel v0.6.0/go.mod h1:jzBIgIzK43Iu1BpDAXwqOd6UPsSAk+ewVZ5ofSXw4Ek=
|
||||
golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
|
|
|
@ -40,12 +40,12 @@ func New(server string, cfg Config) *DataLoaders {
|
|||
playerByID[player.ID] = player
|
||||
}
|
||||
|
||||
sorted := make([]*models.Player, len(ids))
|
||||
inOrder := make([]*models.Player, len(ids))
|
||||
for i, id := range ids {
|
||||
sorted[i] = playerByID[id]
|
||||
inOrder[i] = playerByID[id]
|
||||
}
|
||||
|
||||
return sorted, nil
|
||||
return inOrder, nil
|
||||
},
|
||||
},
|
||||
TribeByID: TribeLoader{
|
||||
|
@ -64,12 +64,12 @@ func New(server string, cfg Config) *DataLoaders {
|
|||
tribeByID[tribe.ID] = tribe
|
||||
}
|
||||
|
||||
sorted := make([]*models.Tribe, len(ids))
|
||||
inOrder := make([]*models.Tribe, len(ids))
|
||||
for i, id := range ids {
|
||||
sorted[i] = tribeByID[id]
|
||||
inOrder[i] = tribeByID[id]
|
||||
}
|
||||
|
||||
return sorted, nil
|
||||
return inOrder, nil
|
||||
},
|
||||
},
|
||||
VillageByID: VillageLoader{
|
||||
|
@ -88,12 +88,12 @@ func New(server string, cfg Config) *DataLoaders {
|
|||
villageByID[village.ID] = village
|
||||
}
|
||||
|
||||
sorted := make([]*models.Village, len(ids))
|
||||
inOrder := make([]*models.Village, len(ids))
|
||||
for i, id := range ids {
|
||||
sorted[i] = villageByID[id]
|
||||
inOrder[i] = villageByID[id]
|
||||
}
|
||||
|
||||
return sorted, nil
|
||||
return inOrder, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -5,13 +5,12 @@ import "github.com/99designs/gqlgen/graphql"
|
|||
func getServer(rctx *graphql.FieldContext) (string, bool) {
|
||||
server := ""
|
||||
ok := false
|
||||
parent := rctx.Parent
|
||||
for parent != nil {
|
||||
server, ok = parent.Args["server"].(string)
|
||||
for rctx != nil {
|
||||
server, ok = rctx.Args["server"].(string)
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
parent = parent.Parent
|
||||
rctx = rctx.Parent
|
||||
}
|
||||
return server, ok
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ func SanitizeSort(sort string) string {
|
|||
return ""
|
||||
}
|
||||
keyword := "ASC"
|
||||
if length == 2 && strings.ToUpper(splitted[1]) == "DESC" {
|
||||
if strings.ToUpper(splitted[1]) == "DESC" {
|
||||
keyword = "DESC"
|
||||
}
|
||||
return strings.ToLower(Underscore(splitted[0])) + " " + keyword
|
||||
|
|
Reference in New Issue
Block a user