@@ -162,21 +162,21 @@ type endpointMapKey string
162162// unordered set of address strings within an endpoint. This map is not thread
163163// safe, thus it is unsafe to access concurrently. Must be created via
164164// NewEndpointMap; do not construct directly.
165- type EndpointMap struct {
166- endpoints map [endpointMapKey ]endpointData
165+ type EndpointMap [ T any ] struct {
166+ endpoints map [endpointMapKey ]endpointData [ T ]
167167}
168168
169- type endpointData struct {
169+ type endpointData [ T any ] struct {
170170 // decodedKey stores the original key to avoid decoding when iterating on
171171 // EndpointMap keys.
172172 decodedKey Endpoint
173- value any
173+ value T
174174}
175175
176176// NewEndpointMap creates a new EndpointMap.
177- func NewEndpointMap () * EndpointMap {
178- return & EndpointMap {
179- endpoints : make (map [endpointMapKey ]endpointData ),
177+ func NewEndpointMap [ T any ] () * EndpointMap [ T ] {
178+ return & EndpointMap [ T ] {
179+ endpoints : make (map [endpointMapKey ]endpointData [ T ] ),
180180 }
181181}
182182
@@ -196,25 +196,25 @@ func encodeEndpoint(e Endpoint) endpointMapKey {
196196}
197197
198198// Get returns the value for the address in the map, if present.
199- func (em * EndpointMap ) Get (e Endpoint ) (value any , ok bool ) {
199+ func (em * EndpointMap [ T ] ) Get (e Endpoint ) (value T , ok bool ) {
200200 val , found := em .endpoints [encodeEndpoint (e )]
201201 if found {
202202 return val .value , true
203203 }
204- return nil , false
204+ return value , false
205205}
206206
207207// Set updates or adds the value to the address in the map.
208- func (em * EndpointMap ) Set (e Endpoint , value any ) {
208+ func (em * EndpointMap [ T ] ) Set (e Endpoint , value T ) {
209209 en := encodeEndpoint (e )
210- em .endpoints [en ] = endpointData {
210+ em .endpoints [en ] = endpointData [ T ] {
211211 decodedKey : Endpoint {Addresses : e .Addresses },
212212 value : value ,
213213 }
214214}
215215
216216// Len returns the number of entries in the map.
217- func (em * EndpointMap ) Len () int {
217+ func (em * EndpointMap [ T ] ) Len () int {
218218 return len (em .endpoints )
219219}
220220
@@ -223,7 +223,7 @@ func (em *EndpointMap) Len() int {
223223// the unordered set of addresses. Thus, endpoint information returned is not
224224// the full endpoint data (drops duplicated addresses and attributes) but can be
225225// used for EndpointMap accesses.
226- func (em * EndpointMap ) Keys () []Endpoint {
226+ func (em * EndpointMap [ T ] ) Keys () []Endpoint {
227227 ret := make ([]Endpoint , 0 , len (em .endpoints ))
228228 for _ , en := range em .endpoints {
229229 ret = append (ret , en .decodedKey )
@@ -232,16 +232,16 @@ func (em *EndpointMap) Keys() []Endpoint {
232232}
233233
234234// Values returns a slice of all current map values.
235- func (em * EndpointMap ) Values () []any {
236- ret := make ([]any , 0 , len (em .endpoints ))
235+ func (em * EndpointMap [ T ] ) Values () []T {
236+ ret := make ([]T , 0 , len (em .endpoints ))
237237 for _ , val := range em .endpoints {
238238 ret = append (ret , val .value )
239239 }
240240 return ret
241241}
242242
243243// Delete removes the specified endpoint from the map.
244- func (em * EndpointMap ) Delete (e Endpoint ) {
244+ func (em * EndpointMap [ T ] ) Delete (e Endpoint ) {
245245 en := encodeEndpoint (e )
246246 delete (em .endpoints , en )
247247}
0 commit comments