File size: 4,049 Bytes
d6f631f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | package notification
import (
"fmt"
"github.com/samber/lo"
)
// ChannelTypes returns a set of ChannelType from Channel slice
func ChannelTypes(channels []Channel) []ChannelType {
seen := make(map[ChannelType]struct{}, len(channels))
types := make([]ChannelType, 0, len(channels))
for _, channel := range channels {
if _, ok := seen[channel.Type]; ok {
continue
}
seen[channel.Type] = struct{}{}
types = append(types, channel.Type)
}
return types
}
// ChannelIDsByType returns a list of Channel identifiers from Channel slice with the provided ChannelType
func ChannelIDsByType(channels []Channel, channelType ChannelType) []string {
ids := make([]string, 0, len(channels))
for _, channel := range channels {
if channel.Type != channelType {
continue
}
ids = append(ids, channel.ID)
}
return ids
}
// DeliveryStatusStates returns a list of EventDeliveryStatusState from EventDeliveryStatus slice
func DeliveryStatusStates(statuses []EventDeliveryStatus) []EventDeliveryStatusState {
seen := make(map[EventDeliveryStatusState]struct{}, len(statuses))
types := make([]EventDeliveryStatusState, 0, len(statuses))
for _, status := range statuses {
if _, ok := seen[status.State]; ok {
continue
}
seen[status.State] = struct{}{}
types = append(types, status.State)
}
return types
}
func interfaceMapToStringMap(m map[string]interface{}, strict bool) (map[string]string, error) {
var s map[string]string
if len(m) > 0 {
s = make(map[string]string, len(m))
for k, v := range m {
switch t := v.(type) {
case string:
s[k] = t
case fmt.Stringer:
s[k] = t.String()
case int, int32, int64:
s[k] = fmt.Sprintf("%d", t)
case float32, float64:
s[k] = fmt.Sprintf("%f", t)
case bool:
s[k] = fmt.Sprintf("%t", t)
default:
if strict {
return nil, fmt.Errorf("failed to cast value with %T to string", t)
} else {
continue
}
}
}
}
return s, nil
}
func StrictInterfaceMapToStringMap(m map[string]interface{}) (map[string]string, error) {
return interfaceMapToStringMap(m, true)
}
func InterfaceMapToStringMap(m map[string]interface{}) map[string]string {
s, _ := interfaceMapToStringMap(m, false)
return s
}
type difference[T comparable] struct {
leftMap map[T]struct{}
left []T
rightMap map[T]struct{}
right []T
}
func (d difference[T]) Has(item T) bool {
return d.HasLeft(item) || d.HasRight(item)
}
func (d difference[T]) HasLeft(item T) bool {
if _, ok := d.leftMap[item]; ok {
return true
}
return false
}
func (d difference[T]) HasRight(item T) bool {
if _, ok := d.rightMap[item]; ok {
return true
}
return false
}
func (d difference[T]) Left() []T {
return d.left
}
func (d difference[T]) Right() []T {
return d.right
}
func (d difference[T]) HasChanged() bool {
return len(d.left) > 0 || len(d.right) > 0
}
func (d difference[T]) All() []T {
return append(d.left, d.right...)
}
type ChannelIDsDifference struct {
diff difference[string]
}
func (d ChannelIDsDifference) Has(id string) bool {
return d.diff.Has(id)
}
func (d ChannelIDsDifference) HasChanged() bool {
return d.diff.HasChanged()
}
func (d ChannelIDsDifference) InAdditions(id string) bool {
return d.diff.HasLeft(id)
}
func (d ChannelIDsDifference) InRemovals(id string) bool {
return d.diff.HasRight(id)
}
func (d ChannelIDsDifference) Additions() []string {
return d.diff.Left()
}
func (d ChannelIDsDifference) Removals() []string {
return d.diff.Right()
}
func (d ChannelIDsDifference) All() []string {
return d.diff.All()
}
func NewChannelIDsDifference(new, old []string) *ChannelIDsDifference {
left, right := lo.Difference(new, old)
leftMap := lo.SliceToMap(left, func(item string) (string, struct{}) {
return item, struct{}{}
})
rightMap := lo.SliceToMap(right, func(item string) (string, struct{}) {
return item, struct{}{}
})
return &ChannelIDsDifference{
diff: difference[string]{
leftMap: leftMap,
left: left,
rightMap: rightMap,
right: right,
},
}
}
|