From 81b9a4341f96d81999e3f0c8c54dc377753290fd Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 4 Mar 2024 01:09:08 +0300 Subject: [PATCH] logger: extend interface, fix tests Signed-off-by: Vasiliy Tolstov --- logger/logger.go | 4 +- logger/noop.go | 10 ++-- logger/slog/slog.go | 4 ++ service.go | 100 +++---------------------------------- service_test.go | 8 +++ util/time/duration_test.go | 8 +-- 6 files changed, 33 insertions(+), 101 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 425bedfe..6448ccaf 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -57,7 +57,9 @@ type Logger interface { Log(ctx context.Context, level Level, args ...interface{}) // Logf logs message with needed level Logf(ctx context.Context, level Level, msg string, args ...interface{}) - // String returns the name of logger + // Name returns broker instance name + Name() string + // String returns the type of logger String() string } diff --git a/logger/noop.go b/logger/noop.go index 04f0515b..17f3608b 100644 --- a/logger/noop.go +++ b/logger/noop.go @@ -13,11 +13,15 @@ func NewLogger(opts ...Option) Logger { return &noopLogger{opts: options} } -func (l *noopLogger) V(lvl Level) bool { +func (l *noopLogger) V(_ Level) bool { return false } -func (l *noopLogger) Level(lvl Level) { +func (l *noopLogger) Level(_ Level) { +} + +func (l *noopLogger) Name() string { + return l.opts.Name } func (l *noopLogger) Init(opts ...Option) error { @@ -35,7 +39,7 @@ func (l *noopLogger) Clone(opts ...Option) Logger { return nl } -func (l *noopLogger) Fields(attrs ...interface{}) Logger { +func (l *noopLogger) Fields(_ ...interface{}) Logger { return l } diff --git a/logger/slog/slog.go b/logger/slog/slog.go index 6e12b65e..b2b756c3 100644 --- a/logger/slog/slog.go +++ b/logger/slog/slog.go @@ -368,6 +368,10 @@ func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{} _ = s.slog.Handler().Handle(ctx, r) } +func (s *slogLogger) Name() string { + return s.opts.Name +} + func (s *slogLogger) String() string { return "slog" } diff --git a/service.go b/service.go index 9ae899a3..22bcb74e 100644 --- a/service.go +++ b/service.go @@ -7,15 +7,11 @@ import ( "go.unistack.org/micro/v3/broker" "go.unistack.org/micro/v3/client" - "go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/config" - "go.unistack.org/micro/v3/flow" "go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/register" - "go.unistack.org/micro/v3/resolver" "go.unistack.org/micro/v3/router" - "go.unistack.org/micro/v3/selector" "go.unistack.org/micro/v3/server" "go.unistack.org/micro/v3/store" "go.unistack.org/micro/v3/tracer" @@ -380,97 +376,15 @@ func (s *service) Run() error { return s.Stop() } -func getNameIndex(n string, ifaces interface{}) int { - type namer interface { - Name() string - } +type Namer interface { + Name() string +} - switch vt := ifaces.(type) { - case []broker.Broker: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []client.Client: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []codec.Codec: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []config.Config: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []flow.Flow: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []logger.Logger: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []meter.Meter: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []register.Register: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []resolver.Resolver: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []router.Router: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []selector.Selector: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []server.Server: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []store.Store: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } - } - case []tracer.Tracer: - for idx, iface := range vt { - if nm, ok := iface.(namer); ok && nm.Name() == n { - return idx - } +func getNameIndex[T Namer](n string, ifaces []T) int { + for idx, iface := range ifaces { + if iface.Name() == n { + return idx } } - return 0 } diff --git a/service_test.go b/service_test.go index 97fd612b..b6bca118 100644 --- a/service_test.go +++ b/service_test.go @@ -41,6 +41,14 @@ func (ti *testItem) Name() string { return ti.name } +func Test_getNameIndex(t *testing.T) { + items := []*testItem{{name: "test1"}, {name: "test2"}} + idx := getNameIndex("test2", items) + if items[idx].Name() != "test2" { + t.Fatal("getNameIndex wrong") + } +} + func TestRegisterHandler(t *testing.T) { type args struct { s server.Server diff --git a/util/time/duration_test.go b/util/time/duration_test.go index 80b7289c..12447510 100644 --- a/util/time/duration_test.go +++ b/util/time/duration_test.go @@ -35,8 +35,8 @@ func TestUnmarshalJSON(t *testing.T) { err = json.Unmarshal([]byte(`{"ttl":"1y"}`), v) if err != nil { t.Fatal(err) - } else if v.TTL != 31536000000000000 { - t.Fatalf("invalid duration %v != 31536000000000000", v.TTL) + } else if v.TTL != 31622400000000000 { + t.Fatalf("invalid duration %v != 31622400000000000", v.TTL) } } @@ -55,7 +55,7 @@ func TestParseDuration(t *testing.T) { if err != nil { t.Fatalf("ParseDuration error: %v", err) } - if td.String() != "8760h0m0s" { - t.Fatalf("ParseDuration 1y != 8760h0m0s : %s", td.String()) + if td.String() != "8784h0m0s" { + t.Fatalf("ParseDuration 1y != 8784h0m0s : %s", td.String()) } }