refactor: rename fn -> f, loadJSONFile -> readJSONFile
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dawid Wysokiński 2023-02-24 07:24:16 +01:00
parent c86ce6ad8b
commit 8080b2d6fd
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
7 changed files with 28 additions and 25 deletions

View File

@ -128,7 +128,7 @@ func newCleanUpCommand() *cli.Command {
type runJobFunc func(ctx context.Context, job *service.Job) error
func runJob(c *cli.Context, timeout time.Duration, fn runJobFunc) error {
func runJob(c *cli.Context, timeout time.Duration, f runJobFunc) error {
db, err := internal.NewBunDB()
if err != nil {
return fmt.Errorf("internal.NewBunDB: %w", err)
@ -160,7 +160,7 @@ func runJob(c *cli.Context, timeout time.Duration, fn runJobFunc) error {
playerSnapshotSvc := service.NewPlayerSnapshot(bundb.NewPlayerSnapshot(db), playerSvc)
ennoblementSvc := service.NewEnnoblement(bundb.NewEnnoblement(db), client)
return fn(ctx, service.NewJob(
return f(ctx, service.NewJob(
versionSvc,
serverSvc,
msg.NewServerPublisher(publisher, marshaler),

View File

@ -174,7 +174,7 @@ type updateServerParamsApplier struct {
}
func (u updateServerParamsApplier) apply(q *bun.UpdateQuery) *bun.UpdateQuery {
for _, fn := range [...]func(*bun.UpdateQuery) *bun.UpdateQuery{
for _, f := range [...]func(*bun.UpdateQuery) *bun.UpdateQuery{
u.applyConfig,
u.applyUnitInfo,
u.applyBuildingInfo,
@ -191,7 +191,7 @@ func (u updateServerParamsApplier) apply(q *bun.UpdateQuery) *bun.UpdateQuery {
u.applyPlayerSnapshotsCreatedAt,
u.applyTribeSnapshotsCreatedAt,
} {
q = fn(q)
q = f(q)
}
return q
}

View File

@ -154,7 +154,8 @@ func TestUpdateData(t *testing.T) {
require.NoError(t, jobSvc.UpdateData(ctx))
expectedServers := loadJSONFile[[]domain.Server](t, filesys, path.Join("expected", "servers.json"))
var expectedServers []domain.Server
readJSONFile(t, filesys, path.Join("expected", "servers.json"), &expectedServers)
assert.Eventuallyf(t, func() bool {
servers, err := serverRepo.List(ctx, domain.ListServersParams{
Special: domain.NullBool{
@ -179,7 +180,8 @@ func TestUpdateData(t *testing.T) {
require.NoError(t, ctx.Err())
expectedPlayers := loadJSONFile[[]domain.Player](t, filesys, path.Join("expected", "players.json"))
var expectedPlayers []domain.Player
readJSONFile(t, filesys, path.Join("expected", "players.json"), &expectedPlayers)
assert.Eventuallyf(t, func() bool {
players, err := playerRepo.List(ctx, domain.ListPlayersParams{
Sort: []domain.PlayerSort{
@ -206,7 +208,8 @@ func TestUpdateData(t *testing.T) {
require.NoError(t, ctx.Err())
expectedTribes := loadJSONFile[[]domain.Tribe](t, filesys, path.Join("expected", "tribes.json"))
var expectedTribes []domain.Tribe
readJSONFile(t, filesys, path.Join("expected", "tribes.json"), &expectedTribes)
assert.Eventuallyf(t, func() bool {
tribes, err := tribeRepo.List(ctx, domain.ListTribesParams{
Sort: []domain.TribeSort{
@ -233,7 +236,8 @@ func TestUpdateData(t *testing.T) {
require.NoError(t, ctx.Err())
expectedVillages := loadJSONFile[[]domain.Village](t, filesys, path.Join("expected", "villages.json"))
var expectedVillages []domain.Village
readJSONFile(t, filesys, path.Join("expected", "villages.json"), &expectedVillages)
assert.Eventuallyf(t, func() bool {
villages, err := villageRepo.List(ctx, domain.ListVillagesParams{})
if err != nil {
@ -251,7 +255,8 @@ func TestUpdateData(t *testing.T) {
require.NoError(t, ctx.Err())
expectedTribeChanges := loadJSONFile[[]domain.TribeChange](t, filesys, path.Join("expected", "tribe-changes.json"))
var expectedTribeChanges []domain.TribeChange
readJSONFile(t, filesys, path.Join("expected", "tribe-changes.json"), &expectedTribeChanges)
assert.Eventuallyf(t, func() bool {
tcs, err := tribeChangeRepo.List(ctx, domain.ListTribeChangesParams{
Sort: []domain.TribeChangeSort{
@ -379,7 +384,8 @@ func TestUpdateEnnoblements(t *testing.T) {
require.NoError(t, jobSvc.UpdateEnnoblements(ctx))
expectedEnnoblements := loadJSONFile[[]domain.Ennoblement](t, filesys, path.Join("expected", "ennoblements.json"))
var expectedEnnoblements []domain.Ennoblement
readJSONFile(t, filesys, path.Join("expected", "ennoblements.json"), &expectedEnnoblements)
assert.Eventuallyf(t, func() bool {
ennoblements, err := ennoblementRepo.List(ctx, domain.ListEnnoblementsParams{
Sort: []domain.EnnoblementSort{
@ -653,7 +659,7 @@ func (t *urlChangerTransport) RoundTrip(r *http.Request) (*http.Response, error)
return t.transport.RoundTrip(r2)
}
func loadJSONFile[T any](tb testing.TB, filesys fs.FS, path string) T {
func readJSONFile(tb testing.TB, filesys fs.FS, path string, v any) {
tb.Helper()
f, err := filesys.Open(path)
@ -662,10 +668,7 @@ func loadJSONFile[T any](tb testing.TB, filesys fs.FS, path string) T {
_ = f.Close()
}()
var res T
require.NoError(tb, json.NewDecoder(f).Decode(&res))
return res
require.NoError(tb, json.NewDecoder(f).Decode(v))
}
func isWithinDuration(expected, actual time.Time, delta time.Duration) bool {

View File

@ -35,15 +35,15 @@ func TestSnapshotPublisher_CmdCreate(t *testing.T) {
tests := []struct {
topic string
fn func(ctx context.Context, payloads ...domain.CreateSnapshotsCmdPayload) error
f func(ctx context.Context, payloads ...domain.CreateSnapshotsCmdPayload) error
}{
{
topic: "players.cmd.create_snapshots",
fn: publisher.CmdCreatePlayers,
f: publisher.CmdCreatePlayers,
},
{
topic: "tribes.cmd.create_snapshots",
fn: publisher.CmdCreateTribes,
f: publisher.CmdCreateTribes,
},
}
@ -56,7 +56,7 @@ func TestSnapshotPublisher_CmdCreate(t *testing.T) {
msgs, err := pubSub.Subscribe(context.Background(), tt.topic)
require.NoError(t, err)
require.NoError(t, tt.fn(context.Background(), payloads...))
require.NoError(t, tt.f(context.Background(), payloads...))
receivedMsgs, _ := subscriber.BulkRead(msgs, len(payloads), time.Second)
require.Len(t, receivedMsgs, len(payloads))

View File

@ -249,14 +249,14 @@ type createPlayerParamsBuilder struct {
func (c createPlayerParamsBuilder) build() domain.CreatePlayerParams {
var p domain.CreatePlayerParams
for _, fn := range [...]func(p domain.CreatePlayerParams) domain.CreatePlayerParams{
for _, f := range [...]func(p domain.CreatePlayerParams) domain.CreatePlayerParams{
c.setBase,
c.setBestRank,
c.setMostVillages,
c.setMostPoints,
c.setLastActivityAt,
} {
p = fn(p)
p = f(p)
}
return p
}

View File

@ -222,13 +222,13 @@ type createTribeParamsBuilder struct {
func (c createTribeParamsBuilder) build() domain.CreateTribeParams {
var p domain.CreateTribeParams
for _, fn := range [...]func(p domain.CreateTribeParams) domain.CreateTribeParams{
for _, f := range [...]func(p domain.CreateTribeParams) domain.CreateTribeParams{
c.setBase,
c.setBestRank,
c.setMostVillages,
c.setMostPoints,
} {
p = fn(p)
p = f(p)
}
return p
}

View File

@ -51,8 +51,8 @@ func WithHTTPClient(hc *http.Client) ClientOption {
// WithEnnoblementsUseInterfaceFunc runs every time Client.GetEnnoblements is called, and allows conditionally
// configuring the client to use /interface.php?func=get_conquer_extended instead of /map/conquer_extended.txt.
// If none is specified, a default function is used that checks if since is after now - 23h.
func WithEnnoblementsUseInterfaceFunc(fn func(since time.Time) bool) ClientOption {
func WithEnnoblementsUseInterfaceFunc(f func(since time.Time) bool) ClientOption {
return func(cfg *clientConfig) {
cfg.ennoblementsUseInterfaceFunc = fn
cfg.ennoblementsUseInterfaceFunc = f
}
}