github.com/cilium/statedb@v0.3.2/index/string.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package index 5 6 import ( 7 "fmt" 8 "iter" 9 ) 10 11 func String(s string) Key { 12 return []byte(s) 13 } 14 15 func FromString(s string) (Key, error) { 16 return String(s), nil 17 } 18 19 func Stringer[T fmt.Stringer](s T) Key { 20 return String(s.String()) 21 } 22 23 func StringSlice(ss []string) KeySet { 24 keys := make([]Key, 0, len(ss)) 25 for _, s := range ss { 26 keys = append(keys, String(s)) 27 } 28 return NewKeySet(keys...) 29 } 30 31 func StringerSlice[T fmt.Stringer](ss []T) KeySet { 32 keys := make([]Key, 0, len(ss)) 33 for _, s := range ss { 34 keys = append(keys, Stringer(s)) 35 } 36 return NewKeySet(keys...) 37 } 38 39 func StringerSeq[T fmt.Stringer](seq iter.Seq[T]) KeySet { 40 return Seq[T](Stringer, seq) 41 } 42 43 func StringerSeq2[A fmt.Stringer, B any](seq iter.Seq2[A, B]) KeySet { 44 return Seq2[A, B](Stringer, seq) 45 }