Replacing IsSliceMember ,CloneStringSlice and SliceStringEqual with slices package functions

This commit is contained in:
gezimbll
2023-10-31 12:00:04 -04:00
committed by Dan Christian Bogos
parent 65310e7437
commit 32ed816de7
74 changed files with 585 additions and 583 deletions

View File

@@ -20,6 +20,7 @@ package utils
import (
"fmt"
"slices"
"sort"
"strconv"
"strings"
@@ -244,7 +245,7 @@ func (cI *CostIncrement) Clone() (cIcln *CostIncrement) {
func (uF *UnitFactor) Clone() *UnitFactor {
cln := new(UnitFactor)
if uF.FilterIDs != nil {
cln.FilterIDs = CloneStringSlice(uF.FilterIDs)
cln.FilterIDs = slices.Clone(uF.FilterIDs)
}
if uF.Factor != nil {
cln.Factor = uF.Factor.Clone()

View File

@@ -35,6 +35,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"sync"
@@ -644,7 +645,7 @@ func (h HierarchyPath) Clone() (cln HierarchyPath) {
if h == nil {
return
}
return CloneStringSlice(h)
return slices.Clone(h)
}
// Mask a number of characters in the suffix of the destination

View File

@@ -18,6 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import "slices"
// NewEventChargers instantiates the EventChargers in a central place
func NewEventCharges() (ec *EventCharges) {
ec = &EventCharges{
@@ -330,10 +332,10 @@ func (ac *AccountCharge) Clone() *AccountCharge {
cln.BalanceLimit = ac.BalanceLimit.Clone()
}
if ac.AttributeIDs != nil {
cln.AttributeIDs = CloneStringSlice(ac.AttributeIDs)
cln.AttributeIDs = slices.Clone(ac.AttributeIDs)
}
if ac.JoinedChargeIDs != nil {
cln.JoinedChargeIDs = CloneStringSlice(ac.JoinedChargeIDs)
cln.JoinedChargeIDs = slices.Clone(ac.JoinedChargeIDs)
}
return cln
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"slices"
"time"
"github.com/ericlagergren/decimal"
@@ -88,7 +89,7 @@ func CloneDynamicStringSliceOpt(in []*DynamicStringSliceOpt) (cl []*DynamicStrin
for i, val := range in {
cl[i] = &DynamicStringSliceOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -100,7 +101,7 @@ func CloneDynamicStringOpt(in []*DynamicStringOpt) (cl []*DynamicStringOpt) {
for i, val := range in {
cl[i] = &DynamicStringOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -112,7 +113,7 @@ func CloneDynamicInterfaceOpt(in []*DynamicInterfaceOpt) (cl []*DynamicInterface
for i, val := range in {
cl[i] = &DynamicInterfaceOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -124,7 +125,7 @@ func CloneDynamicBoolOpt(in []*DynamicBoolOpt) (cl []*DynamicBoolOpt) {
for i, val := range in {
cl[i] = &DynamicBoolOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -136,7 +137,7 @@ func CloneDynamicIntOpt(in []*DynamicIntOpt) (cl []*DynamicIntOpt) {
for i, val := range in {
cl[i] = &DynamicIntOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -148,7 +149,7 @@ func CloneDynamicFloat64Opt(in []*DynamicFloat64Opt) (cl []*DynamicFloat64Opt) {
for i, val := range in {
cl[i] = &DynamicFloat64Opt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -160,7 +161,7 @@ func CloneDynamicDurationOpt(in []*DynamicDurationOpt) (cl []*DynamicDurationOpt
for i, val := range in {
cl[i] = &DynamicDurationOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: val.Value,
}
}
@@ -172,7 +173,7 @@ func CloneDynamicDecimalBigOpt(in []*DynamicDecimalBigOpt) (cl []*DynamicDecimal
for i, val := range in {
cl[i] = &DynamicDecimalBigOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: CloneDecimalBig(val.Value),
}
}
@@ -184,7 +185,7 @@ func CloneDynamicIntPointerOpt(in []*DynamicIntPointerOpt) (cl []*DynamicIntPoin
for i, val := range in {
cl[i] = &DynamicIntPointerOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: IntPointer(*val.Value),
}
}
@@ -196,7 +197,7 @@ func CloneDynamicDurationPointerOpt(in []*DynamicDurationPointerOpt) (cl []*Dyna
for i, val := range in {
cl[i] = &DynamicDurationPointerOpt{
Tenant: val.Tenant,
FilterIDs: CloneStringSlice(val.FilterIDs),
FilterIDs: slices.Clone(val.FilterIDs),
Value: DurationPointer(*val.Value),
}
}
@@ -211,10 +212,10 @@ func DynamicStringSliceOptEqual(v1, v2 []*DynamicStringSliceOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if !SliceStringEqual(v1[i].Value, v2[i].Value) {
if !slices.Equal(v1[i].Value, v2[i].Value) {
return false
}
}
@@ -229,7 +230,7 @@ func DynamicStringOptEqual(v1, v2 []*DynamicStringOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if v1[i].Value != v2[i].Value {
@@ -247,7 +248,7 @@ func DynamicBoolOptEqual(v1, v2 []*DynamicBoolOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if v1[i].Value != v2[i].Value {
@@ -265,7 +266,7 @@ func DynamicIntOptEqual(v1, v2 []*DynamicIntOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if v1[i].Value != v2[i].Value {
@@ -283,7 +284,7 @@ func DynamicFloat64OptEqual(v1, v2 []*DynamicFloat64Opt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if v1[i].Value != v2[i].Value {
@@ -301,7 +302,7 @@ func DynamicDurationOptEqual(v1, v2 []*DynamicDurationOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if v1[i].Value != v2[i].Value {
@@ -319,7 +320,7 @@ func DynamicDecimalBigOptEqual(v1, v2 []*DynamicDecimalBigOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) ||
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) ||
v1[i].Value.Cmp(v2[i].Value) != 0 {
return false
}
@@ -335,7 +336,7 @@ func DynamicInterfaceOptEqual(v1, v2 []*DynamicInterfaceOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if v1[i].Value != v2[i].Value {
@@ -353,7 +354,7 @@ func DynamicIntPointerOptEqual(v1, v2 []*DynamicIntPointerOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if *v1[i].Value != *v2[i].Value {
@@ -371,7 +372,7 @@ func DynamicDurationPointerOptEqual(v1, v2 []*DynamicDurationPointerOpt) bool {
if v1[i].Tenant != v2[i].Tenant {
return false
}
if !SliceStringEqual(v1[i].FilterIDs, v2[i].FilterIDs) {
if !slices.Equal(v1[i].FilterIDs, v2[i].FilterIDs) {
return false
}
if *v1[i].Value != *v2[i].Value {

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"slices"
"strconv"
"strings"
)
@@ -105,7 +106,7 @@ func (fWp FlagParams) Clone() (cln FlagParams) {
for flg, params := range fWp {
var cprm []string
if params != nil {
cprm = CloneStringSlice(params)
cprm = slices.Clone(params)
}
cln[flg] = cprm
}

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"slices"
"strconv"
"strings"
)
@@ -85,7 +86,7 @@ func (onm *OrderedNavigableMap) Remove(fullPath *FullPath) (err error) {
if fullPath.Path == EmptyString {
return ErrWrongPath
}
// fullPath.PathSlice = CloneStringSlice(fullPath.PathSlice) // clone the items to not modify the templates
// fullPath.PathSlice = slices.Clone(fullPath.PathSlice) // clone the items to not modify the templates
if err = onm.nm.Remove(fullPath.PathSlice); err != nil { // remove them from DataNode
return
}
@@ -130,7 +131,7 @@ func (onm *OrderedNavigableMap) SetAsSlice(fullPath *FullPath, vals []*DataNode)
pathItmsSet := make([][]string, len(vals)) // prepare the path for order update
for i := range vals {
pathItmsSet[i] = append(CloneStringSlice(fullPath.PathSlice), strconv.Itoa(i)) // clone the slice as we will append an index
pathItmsSet[i] = append(slices.Clone(fullPath.PathSlice), strconv.Itoa(i)) // clone the slice as we will append an index
}
path := stripIdxFromLastPathElm(fullPath.Path)
if !addedNew { // cleanup old references since the value is being overwritten
@@ -204,7 +205,7 @@ func (onm *OrderedNavigableMap) Append(fullPath *FullPath, val *DataLeaf) (err e
// add the path to order
onm.orderRef[fullPath.Path] = append(onm.orderRef[fullPath.Path],
onm.orderIdx.PushBack(
append(CloneStringSlice(fullPath.PathSlice), // clone the slice as we will append an index
append(slices.Clone(fullPath.PathSlice), // clone the slice as we will append an index
strconv.Itoa(idx))))
return
}
@@ -222,7 +223,7 @@ func (onm *OrderedNavigableMap) Compose(fullPath *FullPath, val *DataLeaf) (err
if _, hasRef := onm.orderRef[fullPath.Path]; !hasRef { // the element is new so append to order
onm.orderRef[fullPath.Path] = append(onm.orderRef[fullPath.Path],
onm.orderIdx.PushBack(
append(CloneStringSlice(fullPath.PathSlice), // clone the slice as we will append an index
append(slices.Clone(fullPath.PathSlice), // clone the slice as we will append an index
"0")))
} else { // move element in the back of order list
onm.orderIdx.MoveToBack(onm.orderRef[fullPath.Path][len(onm.orderRef[fullPath.Path])-1])

View File

@@ -19,23 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package utils
import (
"sort"
"strings"
)
// Binary string search in slice
func IsSliceMember(ss []string, s string) bool {
sort.Strings(ss)
return SliceHasMember(ss, s)
}
// SliceHasMember searches within a *sorted* slice
// useful to search in shared vars (no slice sort)
func SliceHasMember(ss []string, s string) bool {
i := sort.SearchStrings(ss, s)
return i < len(ss) && ss[i] == s
}
// PrefixSliceItems iterates through slice and add a prefix before every element
func PrefixSliceItems(prfx string, slc []string) (out []string) {
out = make([]string, 0, len(slc))
@@ -56,13 +42,6 @@ func SliceStringToIface(slc []string) (ifc []any) {
return
}
// Float64SliceHasMember searches within a *sorted* slice
// useful to search in shared vars (no slice sort)
func Float64SliceHasMember(ss []float64, s float64) bool {
i := sort.SearchFloat64s(ss, s)
return i < len(ss) && ss[i] == s
}
// HasPrefixSlice iterates over slice members and returns true if one the element has that prefix
func HasPrefixSlice(prfxs []string, el string) bool {
for _, prfx := range prfxs {
@@ -72,23 +51,3 @@ func HasPrefixSlice(prfxs []string, el string) bool {
}
return false
}
func CloneStringSlice(in []string) (cl []string) {
if in != nil {
cl = make([]string, len(in))
copy(cl, in)
}
return
}
func SliceStringEqual(v1, v2 []string) bool {
if len(v1) != len(v2) {
return false
}
for i := range v1 {
if v1[i] != v2[i] {
return false
}
}
return true
}

View File

@@ -23,33 +23,6 @@ import (
"testing"
)
func TestIsSliceMember(t *testing.T) {
if !IsSliceMember([]string{"1", "2", "3", "4", "5"}, "5") {
t.Error("Expecting: true, received: false")
}
if IsSliceMember([]string{"1", "2", "3", "4", "5"}, "6") {
t.Error("Expecting: true, received: false")
}
}
func TestSliceHasMember(t *testing.T) {
if !SliceHasMember([]string{"1", "2", "3", "4", "5"}, "5") {
t.Error("Expecting: true, received: false")
}
if SliceHasMember([]string{"1", "2", "3", "4", "5"}, "6") {
t.Error("Expecting: true, received: false")
}
}
func TestFlaot64SliceHasMember(t *testing.T) {
if !Float64SliceHasMember([]float64{1, 2, 3, 4, 5}, 5) {
t.Error("Expecting: true, received: false")
}
if Float64SliceHasMember([]float64{1, 2, 3, 4, 5}, 6) {
t.Error("Expecting: true, received: false")
}
}
func TestHasPrefixSlice(t *testing.T) {
if !HasPrefixSlice([]string{"1", "2", "3", "4", "5"}, "123") {
t.Error("Expecting: true, received: false")
@@ -74,23 +47,3 @@ func TestSliceStringToIface(t *testing.T) {
t.Errorf("Expected: %s ,received: %s", ToJSON(exp), ToJSON(rply))
}
}
func TestSliceStringEqual(t *testing.T) {
v1 := []string{"*V1field1", "*V1field2"}
v2 := []string{"*V2field1"}
//When the length don't match
if rcv := SliceStringEqual(v1, v2); rcv {
t.Error("The length should not match")
}
v2 = append(v2, "*V2field2")
if rcv := SliceStringEqual(v1, v2); rcv {
t.Error("The values should not match")
}
v2 = v1
if rcv := SliceStringEqual(v1, v2); !rcv {
t.Error("The slices should match")
}
}