2022-04-05 07:09:27 +00:00
|
|
|
//
|
|
|
|
// Copyright 2020 Wireline, Inc.
|
|
|
|
//
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
set "github.com/deckarep/golang-set"
|
|
|
|
)
|
|
|
|
|
2022-10-17 06:25:19 +00:00
|
|
|
func Int64ToBytes(num int64) ([]byte, error) {
|
2022-04-05 07:09:27 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2022-10-17 06:25:19 +00:00
|
|
|
if err := binary.Write(buf, binary.BigEndian, num); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetToSlice(set set.Set) []string {
|
|
|
|
names := []string{}
|
|
|
|
|
|
|
|
for name := range set.Iter() {
|
|
|
|
if name, ok := name.(string); ok && name != "" {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.SliceStable(names, func(i, j int) bool { return names[i] < names[j] })
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func SliceToSet(names []string) set.Set {
|
|
|
|
set := set.NewThreadUnsafeSet()
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
if name != "" {
|
|
|
|
set.Add(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendUnique(list []string, element string) []string {
|
|
|
|
set := SliceToSet(list)
|
|
|
|
set.Add(element)
|
|
|
|
|
|
|
|
return SetToSlice(set)
|
|
|
|
}
|