Skip to content

Commit

Permalink
Simplify names of base service types involving generics for logging (#…
Browse files Browse the repository at this point in the history
…518)

A small improvement to base service so that when it's using reflect to
get a name of the service's type for logging purposes, it strips a long
path out of a generic type for a more succinct result.

So this long, unsightly blemish:

    QueryCacher[[]*github.com/riverqueue/riverui/internal/dbsqlc.JobCountByStateRow]

Would become this, which is quite a bit more tolerable:

    QueryCacher[[]*dbsqlc.JobCountByStateRow]
  • Loading branch information
brandur authored Aug 8, 2024
1 parent 21d1c23 commit 52cfb03
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
18 changes: 17 additions & 1 deletion rivershared/baseservice/base_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"math/rand"
"reflect"
"regexp"
"time"

"github.com/riverqueue/river/rivershared/util/randutil"
Expand Down Expand Up @@ -166,7 +167,7 @@ func Init[TService withBaseService](archetype *Archetype, service TService) TSer
baseService := service.GetBaseService()

baseService.Logger = archetype.Logger
baseService.Name = reflect.TypeOf(service).Elem().Name()
baseService.Name = simplifyLogName(reflect.TypeOf(service).Elem().Name())
baseService.Rand = archetype.Rand
baseService.Time = archetype.Time

Expand Down Expand Up @@ -202,3 +203,18 @@ func (g *UnStubbableTimeGenerator) NowUTCOrNil() *time.Time { return nil }
func (g *UnStubbableTimeGenerator) StubNowUTC(nowUTC time.Time) time.Time {
panic("time not stubbable outside tests")
}

var stripGenericTypePathRE = regexp.MustCompile(`\[([\[\]\*]*).*/([^/]+)\]`)

// Simplies the name of a Go type that uses generics for cleaner logging output.
//
// So this:
//
// QueryCacher[[]*github.com/riverqueue/riverui/internal/dbsqlc.JobCountByStateRow]
//
// Becomes this:
//
// QueryCacher[[]*dbsqlc.JobCountByStateRow]
func simplifyLogName(name string) string {
return stripGenericTypePathRE.ReplaceAllString(name, `[$1$2]`)
}
19 changes: 19 additions & 0 deletions rivershared/baseservice/base_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,22 @@ func archetype() *Archetype {
Time: &UnStubbableTimeGenerator{},
}
}

func TestSimplifyLogName(t *testing.T) {
t.Parallel()

require.Equal(t, "NotGeneric", simplifyLogName("NotGeneric"))

// Simplified for use during debugging. Real generics will tend to have
// fully qualified paths and not look like this.
require.Equal(t, "Simple[int]", simplifyLogName("Simple[int]"))
require.Equal(t, "Simple[*int]", simplifyLogName("Simple[*int]"))
require.Equal(t, "Simple[[]int]", simplifyLogName("Simple[[]int]"))
require.Equal(t, "Simple[[]*int]", simplifyLogName("Simple[[]*int]"))

// More realistic examples.
require.Equal(t, "QueryCacher[dbsqlc.JobCountByStateRow]", simplifyLogName("QueryCacher[github.com/riverqueue/riverui/internal/dbsqlc.JobCountByStateRow]"))
require.Equal(t, "QueryCacher[*dbsqlc.JobCountByStateRow]", simplifyLogName("QueryCacher[*github.com/riverqueue/riverui/internal/dbsqlc.JobCountByStateRow]"))
require.Equal(t, "QueryCacher[[]dbsqlc.JobCountByStateRow]", simplifyLogName("QueryCacher[[]github.com/riverqueue/riverui/internal/dbsqlc.JobCountByStateRow]"))
require.Equal(t, "QueryCacher[[]*dbsqlc.JobCountByStateRow]", simplifyLogName("QueryCacher[[]*github.com/riverqueue/riverui/internal/dbsqlc.JobCountByStateRow]"))
}

0 comments on commit 52cfb03

Please sign in to comment.