-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters