Skip to content

Commit

Permalink
Pull request 115: add-kv
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit a80373e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Aug 7 17:50:50 2024 +0300

    container: imp docs

commit 2f67a14
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Aug 7 17:35:26 2024 +0300

    container: add KeyValue
  • Loading branch information
ainar-g committed Aug 7, 2024
1 parent 22225e0 commit 4c9a47e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
12 changes: 12 additions & 0 deletions container/keyvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package container

// KeyValue is a simple structure for pairs of values where one identifies the
// other. It is useful when multiple values need to be processed in a defined
// order and each value is associated with its own name or ID.
type KeyValue[K comparable, V any] struct {
Key K
Value V
}

// KeyValues is a slice of [KeyValue]s.
type KeyValues[K comparable, V any] []KeyValue[K, V]
44 changes: 44 additions & 0 deletions container/keyvalue_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package container_test

import (
"fmt"

"github.com/AdguardTeam/golibs/container"
)

func ExampleKeyValues() {
type service struct{}

var (
serviceA = service{}
serviceB = service{}
serviceC = service{}
)

type svcName string

validate := func(_ service) (err error) {
return nil
}

// These should be validated in this order.
services := container.KeyValues[svcName, service]{{
Key: "svc_a",
Value: serviceA,
}, {
Key: "svc_b",
Value: serviceB,
}, {
Key: "svc_c",
Value: serviceC,
}}

for _, kv := range services {
fmt.Printf("validating service %q: %v\n", kv.Key, validate(kv.Value))
}

// Output:
// validating service "svc_a": <nil>
// validating service "svc_b": <nil>
// validating service "svc_c": <nil>
}
8 changes: 6 additions & 2 deletions netutil/httputil/logmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/timeutil"
)

// LogMiddleware adds a logger using [slogutil.ContextWithLogger] and logs the
Expand Down Expand Up @@ -45,8 +46,11 @@ func (mw *LogMiddleware) Wrap(h http.Handler) (wrapped http.Handler) {

l.Log(ctx, mw.lvl, "started")
defer func() {
elapsed := time.Since(startTime)
l.Log(ctx, mw.lvl, "finished", "code", rw.code, "elapsed", elapsed.String())
// TODO(a.garipov): Augment our JSON handler to use
// [time.Duration.String] automatically?
l.Log(ctx, mw.lvl, "finished", "code", rw.code, "elapsed", timeutil.Duration{
Duration: time.Since(startTime),
})
}()

h.ServeHTTP(rw, r)
Expand Down
4 changes: 4 additions & 0 deletions netutil/httputil/logmw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func TestLogMiddleware(t *testing.T) {

if i == 1 {
assert.Equal(t, float64(http.StatusOK), obj["code"])

// Make sure that the "elapsed" attribute is printed consistently.
elapsedStr := testutil.RequireTypeAssert[string](t, obj["elapsed"])
assert.Regexp(t, `[0-9.]+[a-zµ]+`, elapsedStr)
}
}
}

0 comments on commit 4c9a47e

Please sign in to comment.