| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package model |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | |
| 8 | "github.com/gohugoio/hashstructure" |
| 9 | ) |
| 10 | |
| 11 | // CalcHash calculates a hash for any object using hashstructure. |
| 12 | // Used by discoverers to generate unique hashes for targets. |
| 13 | func CalcHash(obj any) (uint64, error) { |
| 14 | return hashstructure.Hash(obj, nil) |
| 15 | } |
| 16 | |
| 17 | // MapAny converts a map[string]string to map[string]any. |
| 18 | // Used by discoverers to convert labels/annotations for template evaluation. |
| 19 | func MapAny(src map[string]string) map[string]any { |
| 20 | if src == nil { |
| 21 | return nil |
| 22 | } |
| 23 | m := make(map[string]any, len(src)) |
| 24 | for k, v := range src { |
| 25 | m[k] = v |
| 26 | } |
| 27 | return m |
| 28 | } |
| 29 | |
| 30 | // SendTargetGroup sends a target group to the channel with context cancellation support. |
| 31 | // If tgg is nil, nothing is sent. |
| 32 | func SendTargetGroup(ctx context.Context, in chan<- []TargetGroup, tgg TargetGroup) { |
| 33 | if tgg == nil { |
| 34 | return |
| 35 | } |
| 36 | select { |
| 37 | case <-ctx.Done(): |
| 38 | case in <- []TargetGroup{tgg}: |
| 39 | } |
| 40 | } |