diff --git a/utils/accountprofile_test.go b/utils/accountprofile_test.go index 9c6e9ca8c..ccb023c2b 100644 --- a/utils/accountprofile_test.go +++ b/utils/accountprofile_test.go @@ -453,7 +453,7 @@ func TestAPAccountProfiles(t *testing.T) { }, Weights: nil, Balances: map[string]*Balance{ - "testBalance1": &Balance{ + "testBalance1": { ID: "testBalance1", Type: MetaAbstract, Units: &Decimal{decimal.New(0, 0)}, @@ -474,7 +474,7 @@ func TestAPAccountProfiles(t *testing.T) { }, Weights: nil, Balances: map[string]*Balance{ - "testBalance2": &Balance{ + "testBalance2": { ID: "testBalance2", Type: MetaAbstract, Units: &Decimal{decimal.New(0, 0)}, @@ -510,7 +510,7 @@ func TestAPLockIDs(t *testing.T) { }, Weights: nil, Balances: map[string]*Balance{ - "testBalance1": &Balance{ + "testBalance1": { ID: "testBalance1", Type: MetaAbstract, Units: &Decimal{decimal.New(0, 0)}, @@ -531,7 +531,7 @@ func TestAPLockIDs(t *testing.T) { }, Weights: nil, Balances: map[string]*Balance{ - "testBalance2": &Balance{ + "testBalance2": { ID: "testBalance2", Type: MetaAbstract, Units: &Decimal{decimal.New(0, 0)}, @@ -567,7 +567,7 @@ func TestAPTenantIDs(t *testing.T) { }, Weights: nil, Balances: map[string]*Balance{ - "testBalance1": &Balance{ + "testBalance1": { ID: "testBalance1", Type: MetaAbstract, Units: &Decimal{decimal.New(0, 0)}, @@ -588,7 +588,7 @@ func TestAPTenantIDs(t *testing.T) { }, Weights: nil, Balances: map[string]*Balance{ - "testBalance2": &Balance{ + "testBalance2": { ID: "testBalance2", Type: MetaAbstract, Units: &Decimal{decimal.New(0, 0)}, diff --git a/utils/apitpdata.go b/utils/apitpdata.go index cdc05e0c6..0852ab00b 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -69,12 +69,7 @@ func (pgnt *Paginator) PaginateStringSlice(in []string) (out []string) { if limit == 0 || limit > len(in) { limit = len(in) } - ret := in[offset:limit] - out = make([]string, len(ret)) - for i, itm := range ret { - out[i] = itm - } - return + return CloneStringSlice(in[offset:limit]) } // Clone creates a clone of the object @@ -647,8 +642,8 @@ func (fltr *CDRsFilter) Prepare() { // sort.Strings(fltr.DestinationPrefixes) // sort.Strings(fltr.NotDestinationPrefixes) - sort.Sort(sort.Float64Slice(fltr.Costs)) - sort.Sort(sort.Float64Slice(fltr.NotCosts)) + sort.Float64s(fltr.Costs) + sort.Float64s(fltr.NotCosts) } diff --git a/utils/apitpdata_test.go b/utils/apitpdata_test.go index 4f24cfbd9..1ec45df56 100644 --- a/utils/apitpdata_test.go +++ b/utils/apitpdata_test.go @@ -111,7 +111,7 @@ func TestNewRateSlot(t *testing.T) { t.Errorf("Expected: %+v ,received: %+v ", eOut, rcv) } eOut.RateUnit = "a" - rcv, err = NewRateSlot(eOut.ConnectFee, eOut.Rate, eOut.RateUnit, eOut.RateIncrement, eOut.GroupIntervalStart) + _, err = NewRateSlot(eOut.ConnectFee, eOut.Rate, eOut.RateUnit, eOut.RateIncrement, eOut.GroupIntervalStart) //must receive from time an error: "invalid duration a" if err == nil || err.Error() != "time: invalid duration \"a\"" { t.Error(err) @@ -409,20 +409,20 @@ func TestAttrGetCdrsAsCDRsFilter(t *testing.T) { t.Errorf("Nil struct expected") } //check with wrong time-zone - rcv, err = attrGetCdrs.AsCDRsFilter("wrongtimezone") + _, err = attrGetCdrs.AsCDRsFilter("wrongtimezone") if err == nil { t.Errorf("ParseTimeDetectLayout error") } //check with wrong TimeStart attrGetCdrs.TimeStart = "wrongtimeStart" - rcv, err = attrGetCdrs.AsCDRsFilter("") + _, err = attrGetCdrs.AsCDRsFilter("") if err == nil { t.Errorf("Wrong AnswerTimeStart not processed") } //check with wrong TimeEnd attrGetCdrs.TimeStart = "2020-04-04T11:45:26.371Z" attrGetCdrs.TimeEnd = "wrongtimeEnd" - rcv, err = attrGetCdrs.AsCDRsFilter("") + _, err = attrGetCdrs.AsCDRsFilter("") if err == nil { t.Errorf("Wrong AnswerTimeEnd not processed") } @@ -482,7 +482,7 @@ func TestNewTAFromAccountKey(t *testing.T) { t.Errorf("Expected: %s ,received: %s ", ToJSON(eOut), ToJSON(rcv)) } //check with wrong TenantAccount - rcv, err = NewTAFromAccountKey("") + _, err = NewTAFromAccountKey("") if err == nil { t.Errorf("Unsupported format not processed") } @@ -600,53 +600,53 @@ func TestRPCCDRsFilterAsCDRsFilter(t *testing.T) { } rpcCDRsFilter.ExtraArgs[MaxCost] = "notFloat64" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("MaxCost should not be processed") } rpcCDRsFilter.ExtraArgs[MinCost] = "notFloat64" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("MinCost should not be processed") } rpcCDRsFilter.ExtraArgs[OrderIDEnd] = "notInt64" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("OrderIDEnd should not be processed") } rpcCDRsFilter.ExtraArgs[OrderIDStart] = "notInt64" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("OrderIDStart should not be processed") } rpcCDRsFilter.UpdatedAtEnd = "wrongUpdatedAtEnd" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("UpdatedAtEnd should not be processed") } rpcCDRsFilter.UpdatedAtStart = "wrongUpdatedAtStart" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("UpdatedAtStart should not be processed") } rpcCDRsFilter.CreatedAtEnd = "wrongCreatedAtEnd" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("CreatedAtEnd should not be processed") } rpcCDRsFilter.CreatedAtStart = "wrongCreatedAtStart" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("CreatedAtStart should not be processed") } rpcCDRsFilter.AnswerTimeEnd = "wrongAnswerTimeEnd" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("AnswerTimeEnd should not be processed") } rpcCDRsFilter.AnswerTimeStart = "wrongAnswerTimeStart" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("AnswerTimeStart should not be processed") } rpcCDRsFilter.SetupTimeEnd = "wrongSetupTimeEnd" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("SetupTimeEnd should not be processed") } rpcCDRsFilter.SetupTimeStart = "wrongSetupTimeStart" - if rcv, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { + if _, err = rpcCDRsFilter.AsCDRsFilter(""); err == nil { t.Errorf("SetupTimeStart should not be processed") } } @@ -719,12 +719,12 @@ func TestTPActivationIntervalAsActivationInterval(t *testing.T) { } //check with wrong time tPActivationInterval.ExpiryTime = "wrongExpiryTime" - rcv, err = tPActivationInterval.AsActivationInterval("") + _, err = tPActivationInterval.AsActivationInterval("") if err == nil { t.Errorf("Wrong ExpiryTime not processed") } tPActivationInterval.ActivationTime = "wrongActivationTime" - rcv, err = tPActivationInterval.AsActivationInterval("") + _, err = tPActivationInterval.AsActivationInterval("") if err == nil { t.Errorf("Wrong ActivationTimes not processed") } diff --git a/utils/birpcint_client_test.go b/utils/birpcint_client_test.go index 0ed292cf9..aff464761 100644 --- a/utils/birpcint_client_test.go +++ b/utils/birpcint_client_test.go @@ -23,7 +23,6 @@ import ( "testing" "github.com/cenkalti/rpc2" - "github.com/cgrates/rpcclient" ) func TestNewBiJSONrpcClient(t *testing.T) { @@ -43,23 +42,9 @@ func TestNewBiJSONrpcClient(t *testing.T) { "": func(*rpc2.Client, *struct{}, *string) error { return nil }, } - rcv, err = NewBiJSONrpcClient(addr, handlers) + _, err = NewBiJSONrpcClient(addr, handlers) if err != nil { t.Error(err) } l.Close() } - -type testBiRPCServer struct { - metod string - args interface{} - reply interface{} -} - -func (*testBiRPCServer) Call(string, interface{}, interface{}) error { return nil } -func (t *testBiRPCServer) CallBiRPC(_ rpcclient.ClientConnector, metod string, args interface{}, reply interface{}) error { - t.metod = metod - t.args = args - t.reply = reply - return nil -} diff --git a/utils/cgrevent_test.go b/utils/cgrevent_test.go index 6cbd9c919..19566dbee 100644 --- a/utils/cgrevent_test.go +++ b/utils/cgrevent_test.go @@ -121,7 +121,7 @@ func TestLibRoutesUsage(t *testing.T) { if answ != se.Event[Usage] { t.Errorf("Expecting: %+v, received: %+v", se.Event[Usage], answ) } - answ, err = seErr.FieldAsDuration(Usage) + _, err = seErr.FieldAsDuration(Usage) if err != ErrNotFound { t.Error(err) } diff --git a/utils/cgrrecordwriter.go b/utils/cgrrecordwriter.go index 3bb083a8c..cbdff840f 100644 --- a/utils/cgrrecordwriter.go +++ b/utils/cgrrecordwriter.go @@ -51,6 +51,4 @@ func (rw *CgrIORecordWriter) Write(record []string) error { // Flush only to implement CgrRecordWriter // ToDo: make sure we properly handle this method -func (*CgrIORecordWriter) Flush() { - return -} +func (*CgrIORecordWriter) Flush() {} diff --git a/utils/coreutils.go b/utils/coreutils.go index 2ff9f1eea..0d188c30b 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -651,11 +651,7 @@ func (h HierarchyPath) Clone() (cln HierarchyPath) { if h == nil { return } - cln = make(HierarchyPath, len(h)) - for i, p := range h { - cln[i] = p - } - return + return CloneStringSlice(h) } // Mask a number of characters in the suffix of the destination @@ -728,7 +724,7 @@ func GetCGRVersion() (vers string, err error) { } func NewTenantID(tntID string) *TenantID { - if strings.Index(tntID, ConcatenatedKeySep) == -1 { // no :, ID without Tenant + if !strings.Contains(tntID, ConcatenatedKeySep) { // no :, ID without Tenant return &TenantID{ID: tntID} } tIDSplt := strings.SplitN(tntID, ConcatenatedKeySep, 2) diff --git a/utils/coreutils_test.go b/utils/coreutils_test.go index 078f18cd2..dc467915f 100644 --- a/utils/coreutils_test.go +++ b/utils/coreutils_test.go @@ -333,7 +333,7 @@ func TestParseTimeDetectLayout(t *testing.T) { } if nowTm, err := ParseTimeDetectLayout(MetaNow, ""); err != nil { t.Error(err) - } else if time.Now().Sub(nowTm) > 10*time.Millisecond { + } else if time.Since(nowTm) > 10*time.Millisecond { t.Errorf("Unexpected time parsed: %v", nowTm) } eamonTmStr := "31/05/2015 14:46:00" @@ -1286,8 +1286,8 @@ func (tRPC *TestRPC) Call(args interface{}, reply interface{}) error { return nil } -func (tRPC *TestRPC) V1Error2(args interface{}, reply interface{}) (error, int) { - return nil, 0 +func (tRPC *TestRPC) V1Error2(args interface{}, reply interface{}) (int, error) { + return 0, nil } func (tRPC *TestRPC) V1Error3(args interface{}, reply interface{}) int { @@ -1334,8 +1334,8 @@ func (tRPC *TestRPC2) Call(args interface{}, reply interface{}) error { return nil } -func (tRPC *TestRPC2) Error2(args interface{}, reply interface{}) (error, int) { - return nil, 0 +func (tRPC *TestRPC2) Error2(args interface{}, reply interface{}) (int, error) { + return 0, nil } func (tRPC *TestRPC2) Error3(args interface{}, reply interface{}) int { diff --git a/utils/dataconverter_test.go b/utils/dataconverter_test.go index 2494c2009..d4d29b124 100644 --- a/utils/dataconverter_test.go +++ b/utils/dataconverter_test.go @@ -97,7 +97,7 @@ func TestNewDataConverter(t *testing.T) { if !reflect.DeepEqual(a, b) { t.Error("Error reflect") } - if a, err = NewDataConverter(MetaMultiply); err == nil || err != ErrMandatoryIeMissingNoCaps { + if _, err = NewDataConverter(MetaMultiply); err == nil || err != ErrMandatoryIeMissingNoCaps { t.Error(err) } a, err = NewDataConverter("*multiply:3.3") @@ -111,7 +111,7 @@ func TestNewDataConverter(t *testing.T) { if !reflect.DeepEqual(a, b) { t.Error("Error reflect") } - if a, err = NewDataConverter(MetaDivide); err == nil || err != ErrMandatoryIeMissingNoCaps { + if _, err = NewDataConverter(MetaDivide); err == nil || err != ErrMandatoryIeMissingNoCaps { t.Error(err) } a, err = NewDataConverter("*divide:3.3") @@ -125,7 +125,7 @@ func TestNewDataConverter(t *testing.T) { if !reflect.DeepEqual(a, b) { t.Error("Error reflect") } - if a, err = NewDataConverter(MetaLibPhoneNumber); err == nil || err.Error() != "unsupported *libphonenumber converter parameters: <>" { + if _, err = NewDataConverter(MetaLibPhoneNumber); err == nil || err.Error() != "unsupported *libphonenumber converter parameters: <>" { t.Error(err) } a, err = NewDataConverter("*libphonenumber:US") @@ -139,7 +139,8 @@ func TestNewDataConverter(t *testing.T) { if !reflect.DeepEqual(a, b) { t.Error("Error reflect") } - if _, err := NewDataConverter("unsupported"); err == nil || err.Error() != "unsupported converter definition: " { + if _, err = NewDataConverter("unsupported"); err == nil || err.Error() != "unsupported converter definition: " { + t.Error(err) } hex, err := NewDataConverter(MetaString2Hex) @@ -244,7 +245,6 @@ func TestNewRoundConverter(t *testing.T) { } else if !reflect.DeepEqual(rcv, eOut) { t.Errorf("Expected %+v received: %+v", eOut, rcv) } - eOut = &RoundConverter{} if rcv, err := NewRoundConverter("12:*middle:wrong_length"); err == nil || err.Error() != "unsupported *round converter parameters: <12:*middle:wrong_length>" { t.Error(err) } else if !reflect.DeepEqual(rcv, nil) { diff --git a/utils/datanode.go b/utils/datanode.go index 39e23821f..c09c48733 100644 --- a/utils/datanode.go +++ b/utils/datanode.go @@ -234,8 +234,8 @@ func (n *DataNode) Set(path []string, val interface{}) (addedNew bool, err error // IsEmpty return if the node is empty/ has no data func (n DataNode) IsEmpty() bool { - return n.Value == nil || - len(n.Map) == 0 || + return n.Value == nil && + len(n.Map) == 0 && len(n.Slice) == 0 } diff --git a/utils/dataprovider_test.go b/utils/dataprovider_test.go index 98cefcd70..ba386895b 100644 --- a/utils/dataprovider_test.go +++ b/utils/dataprovider_test.go @@ -23,11 +23,11 @@ import ( ) func TestDPDynamicInterface(t *testing.T) { - nm := NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, + nm := MapStorage{ + "Field1": "1001", + "Field2": "1003", + "Field3": MapStorage{"Field4": "Val"}, + "Field5": []interface{}{10, 101}, } var expected interface{} = "Field5[1]" if rply, err := DPDynamicInterface("Field5[1]", nm); err != nil { @@ -46,11 +46,11 @@ func TestDPDynamicInterface(t *testing.T) { } func TestDPDynamicString(t *testing.T) { - nm := NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, + nm := MapStorage{ + "Field1": "1001", + "Field2": "1003", + "Field3": MapStorage{"Field4": "Val"}, + "Field5": []interface{}{10, 101}, } var expected interface{} = "Field5[1]" if rply, err := DPDynamicString("Field5[1]", nm); err != nil { @@ -70,127 +70,67 @@ func TestDPDynamicString(t *testing.T) { func TestAppendNavMapVal(t *testing.T) { onm := NewOrderedNavigableMap() - nm := NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - } + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field2": NewLeafNode("1003"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }} onm.nm = nm - expected := NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101), NewNMData(18)}, - } - if err := AppendNavMapVal(onm, &FullPath{Path: "Field5", PathItems: PathItems{{Field: "Field5"}}}, NewNMData(18)); err != nil { + expected := &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field2": NewLeafNode("1003"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101), NewLeafNode(18)}}, + }} + if err := onm.Append(&FullPath{Path: "Field5", PathItems: []string{"Field5"}}, NewLeafNode(18).Value); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, onm.nm) { t.Errorf("Expected %v ,received: %v", expected, onm.nm) } - if err := AppendNavMapVal(onm, &FullPath{}, NewNMData(18)); err != ErrWrongPath { + if err := onm.Append(&FullPath{}, NewLeafNode(18).Value); err != ErrWrongPath { t.Errorf("Expected error: %s received: %v", ErrWrongPath, err) } } func TestComposeNavMapVal(t *testing.T) { onm := NewOrderedNavigableMap() - nm := NavigableMap{ - "Field4": &NMSlice{}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - } + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field4": {Type: NMSliceType, Slice: []*DataNode{}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }} onm.nm = nm - if err := ComposeNavMapVal(onm, &FullPath{Path: "Field4", PathItems: PathItems{{Field: "Field4"}}}, NewNMData(18)); err != ErrWrongPath { + if err := onm.Compose(&FullPath{Path: "Field4", PathItems: []string{"Field4", "10"}}, NewLeafNode(18).Value); err != ErrNotFound { t.Error(err) } - expected := NavigableMap{ - "Field4": &NMSlice{}, - "Field5": &NMSlice{NewNMData(10), NewNMData("10118")}, - } - if err := ComposeNavMapVal(onm, &FullPath{Path: "Field5", PathItems: PathItems{{Field: "Field5"}}}, NewNMData(18)); err != nil { + expected := &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field4": {Type: NMSliceType, Slice: []*DataNode{}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode("10118")}}, + }} + if err := onm.Compose(&FullPath{Path: "Field5", PathItems: []string{"Field5"}}, NewLeafNode(18).Value); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, nm) { t.Errorf("Expected %v ,received: %v", expected, nm) } - expected = NavigableMap{ - "Field4": &NMSlice{}, - "Field5": &NMSlice{NewNMData(10), NewNMData("10118")}, - "Field6": &NMSlice{NewNMData(10)}, - } - if err := ComposeNavMapVal(onm, &FullPath{Path: "Field6", PathItems: PathItems{{Field: "Field6"}}}, NewNMData(10)); err != nil { + expected = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field4": {Type: NMSliceType, Slice: []*DataNode{}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode("10118")}}, + "Field6": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10)}}, + }} + if err := onm.Compose(&FullPath{Path: "Field6", PathItems: []string{"Field6"}}, NewLeafNode(10).Value); err != nil { t.Error(err) } else if !reflect.DeepEqual(expected, nm) { t.Errorf("Expected %v ,received: %v", expected, nm) } - onm.nm = NavigableMap{ - "Field4": NewNMData(1), - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - } - if err := ComposeNavMapVal(onm, &FullPath{Path: "Field4", PathItems: PathItems{{Field: "Field4"}}}, NewNMData(10)); err != ErrNotImplemented { - t.Error(err) - } + onm.nm = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field4": NewLeafNode(1), + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }} - if err := ComposeNavMapVal(onm, &FullPath{Path: "Field5", PathItems: PathItems{{Field: "Field5"}}}, &mockNMInterface{data: 10}); err != ErrNotImplemented { - t.Error(err) - } - - if err := ComposeNavMapVal(onm, &FullPath{}, NewNMData(18)); err != ErrWrongPath { + if err := onm.Compose(&FullPath{}, NewLeafNode(18).Value); err != ErrWrongPath { t.Errorf("Expected error: %s received: %v", ErrWrongPath, err) } } - -// mock NMInterface structure - -type mockNMInterface struct{ data interface{} } - -func (nmi *mockNMInterface) String() string { - return IfaceAsString(nmi.data) -} - -// Interface returns the wraped interface -func (nmi *mockNMInterface) Interface() interface{} { - return nmi.data -} - -// Field not implemented only used in order to implement the NM interface -func (nmi *mockNMInterface) Field(path PathItems) (val NMInterface, err error) { - return nil, ErrNotImplemented -} - -// Set not implemented -func (nmi *mockNMInterface) Set(path PathItems, val NMInterface) (a bool, err error) { - return false, ErrNotImplemented -} - -// Remove not implemented only used in order to implement the NM interface -func (nmi *mockNMInterface) Remove(path PathItems) (err error) { - return ErrNotImplemented -} - -// Type returns the type of the NM interface -func (nmi *mockNMInterface) Type() NMType { - return NMDataType -} - -// Empty returns true if the NM is empty(no data) -func (nmi *mockNMInterface) Empty() bool { - return nmi == nil || nmi.data == nil -} - -// GetField not implemented only used in order to implement the NM interface -func (nmi *mockNMInterface) GetField(path PathItem) (val NMInterface, err error) { - return nil, ErrNotImplemented -} - -// SetField not implemented -func (nmi *mockNMInterface) SetField(path PathItem, val NMInterface) (err error) { - return ErrNotImplemented -} - -// Len not implemented only used in order to implement the NM interface -func (nmi *mockNMInterface) Len() int { - return 0 -} diff --git a/utils/decimal_test.go b/utils/decimal_test.go index e08c6c073..4b79efb59 100644 --- a/utils/decimal_test.go +++ b/utils/decimal_test.go @@ -73,7 +73,6 @@ func TestUnmarshalMarshalBinary(t *testing.T) { } dec = nil - expected = NewDecimal(10, 0) if err := dec.UnmarshalBinary([]byte(`10`)); err != nil { t.Error(err) } diff --git a/utils/dynamicweight_test.go b/utils/dynamicweight_test.go index e54f01e26..3c5732f4a 100644 --- a/utils/dynamicweight_test.go +++ b/utils/dynamicweight_test.go @@ -111,8 +111,7 @@ func TestDynamicWeightString(t *testing.T) { } func TestCloneDynamicWeights(t *testing.T) { - dynWeigh := DynamicWeights{} - dynWeigh = nil + var dynWeigh DynamicWeights if rcv := dynWeigh.Clone(); len(rcv) != 0 { t.Errorf("Expected empty slice") } diff --git a/utils/librates_test.go b/utils/librates_test.go index db2ae9fe0..9c024b223 100644 --- a/utils/librates_test.go +++ b/utils/librates_test.go @@ -151,7 +151,9 @@ func TestLibratesRunTimes(t *testing.T) { } eTime = sTime.Add(24 * time.Hour) - received, err = rt.RunTimes(sTime, eTime, verbosity) + if received, err = rt.RunTimes(sTime, eTime, verbosity); err != nil { + t.Error(err) + } aT1, err := time.Parse(time.RFC3339, "2022-03-12T00:00:00Z") if err != nil { @@ -167,10 +169,6 @@ func TestLibratesRunTimes(t *testing.T) { aTsl = append(aTsl, aT1, aT2) expected = append(expected, aTsl) - if err != nil { - t.Errorf("\ndidn't expect error, got %v", err) - } - if !reflect.DeepEqual(received, expected) { t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) } diff --git a/utils/logger_test.go b/utils/logger_test.go index 3f72f0bda..5b63c8c96 100644 --- a/utils/logger_test.go +++ b/utils/logger_test.go @@ -302,8 +302,7 @@ func TestCloseLogger(t *testing.T) { if err != nil { t.Error(err) } - x := newLogger.GetSyslog() - x = newWriter + x := newWriter newLogger.SetSyslog(x) if err := newLogger.Close(); err != nil { diff --git a/utils/map.go b/utils/map.go index c83d07811..97aeea0e4 100644 --- a/utils/map.go +++ b/utils/map.go @@ -87,7 +87,7 @@ func (sm StringMap) Slice() []string { func (sm StringMap) IsEmpty() bool { return sm == nil || len(sm) == 0 || - sm[MetaAny] == true + sm[MetaAny] } func StringMapFromSlice(s []string) StringMap { @@ -180,15 +180,14 @@ func (fWp FlagParams) ParamValue(opt string) (ps string) { // Add adds the options to the flag func (fWp FlagParams) Add(opts []string) { switch len(opts) { - case 0: - case 1: - fWp[opts[0]] = []string{} default: // just in case we call this function with more elements than needed fallthrough case 2: fWp[opts[0]] = strings.Split(opts[1], ANDSep) + case 0: + case 1: + fWp[opts[0]] = []string{} } - return } // ParamsSlice returns the list of profiles for the subsystem @@ -217,10 +216,7 @@ func (fWp FlagParams) Clone() (cln FlagParams) { for flg, params := range fWp { var cprm []string if params != nil { - cprm = make([]string, len(params)) - for i, p := range params { - cprm[i] = p - } + cprm = CloneStringSlice(params) } cln[flg] = cprm } diff --git a/utils/net_test.go b/utils/net_test.go index 77496beb8..4d875744e 100644 --- a/utils/net_test.go +++ b/utils/net_test.go @@ -90,7 +90,7 @@ func TestDecodeServerRequest(t *testing.T) { if !reflect.DeepEqual(req, rcvReq) { t.Errorf("Expecting: %+v, received: %+v", req, rcvReq) } - if !reflect.DeepEqual(err, rcvErr) { + if err != rcvErr { t.Errorf("Expecting: %+v, received: %+v", err, rcvErr) } } diff --git a/utils/orderednavigablemap.go b/utils/orderednavigablemap.go index 204aa78a7..cd784c2e7 100644 --- a/utils/orderednavigablemap.go +++ b/utils/orderednavigablemap.go @@ -98,17 +98,6 @@ func (onm *OrderedNavigableMap) Set(fullPath *FullPath, val interface{}) (err er } path := stripIdxFromLastPathElm(fullPath.Path) - if !addedNew { // cleanup old references since the value is being overwritten - for idxPath, slcIdx := range onm.orderRef { - if !strings.HasPrefix(idxPath, path) { - continue - } - for _, el := range slcIdx { - onm.orderIdx.Remove(el) - } - delete(onm.orderRef, idxPath) - } - } _, hasRef := onm.orderRef[path] if addedNew || !hasRef { onm.orderRef[path] = append(onm.orderRef[path], onm.orderIdx.PushBack(fullPath.PathItems)) @@ -147,14 +136,10 @@ func (onm *OrderedNavigableMap) SetAsSlice(fullPath *FullPath, vals []*DataNode) } delete(onm.orderRef, idxPath) } + } - _, hasRef := onm.orderRef[path] for _, pathItms := range pathItmsSet { - if addedNew || !hasRef { - onm.orderRef[path] = append(onm.orderRef[path], onm.orderIdx.PushBack(pathItms)) - } else { - onm.orderIdx.MoveToBack(onm.orderRef[path][len(onm.orderRef[path])-1]) - } + onm.orderRef[path] = append(onm.orderRef[path], onm.orderIdx.PushBack(pathItms)) } return } @@ -192,7 +177,7 @@ func (onm *OrderedNavigableMap) OrderedFields() (flds []interface{}) { flds = make([]interface{}, 0, len(onm.nm.Map)) for el := onm.GetFirstElement(); el != nil; el = el.Next() { fld, _ := onm.Field(el.Value) - flds = append(flds, fld) + flds = append(flds, fld.Data) } return } diff --git a/utils/orderednavigablemap_test.go b/utils/orderednavigablemap_test.go index 886df2b35..d59ebb98c 100644 --- a/utils/orderednavigablemap_test.go +++ b/utils/orderednavigablemap_test.go @@ -19,6 +19,7 @@ package utils import ( "reflect" + "strings" "testing" ) @@ -27,59 +28,47 @@ func TestOrderedNavigableMap(t *testing.T) { onm.Set(&FullPath{ Path: "Field1", - PathItems: PathItems{{Field: "Field1"}}, - }, NewNMData(10)) - expOrder := []PathItems{ - {{Field: "Field1"}}, - } + PathItems: []string{"Field1"}, + }, NewLeafNode(10)) + expOrder := [][]string{{"Field1"}} if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } onm.Set(&FullPath{ Path: "Field2[0]", - PathItems: PathItems{{Field: "Field2", Index: []string{"0"}}}, - }, NewNMData("1001")) - expOrder = []PathItems{ - {{Field: "Field1"}}, - {{Field: "Field2", Index: []string{"0"}}}, + PathItems: []string{"Field2", "0"}, + }, NewLeafNode("1001")) + expOrder = [][]string{ + {"Field1"}, + {"Field2", "0"}, } if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } onm.Set(&FullPath{ - Path: "Field2[1].Account[0]", - PathItems: PathItems{ - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"0"}}}, - }, NewNMData(10)) - expOrder = []PathItems{ - {{Field: "Field1"}}, - {{Field: "Field2", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"0"}}}, + Path: "Field2[1].Account[0]", + PathItems: []string{"Field2", "1", "Account", "0"}, + }, NewLeafNode(10)) + expOrder = [][]string{ + {"Field1"}, + {"Field2", "0"}, + {"Field2", "1", "Account", "0"}, } if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } onm.Set(&FullPath{ - Path: "Field2[1].Account[1]", - PathItems: PathItems{ - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"1"}}}, - }, NewNMData(11)) - expOrder = []PathItems{ - {{Field: "Field1"}}, - {{Field: "Field2", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"1"}}}, + Path: "Field2[1].Account[1]", + PathItems: []string{"Field2", "1", "Account", "1"}, + }, NewLeafNode(11)) + expOrder = [][]string{ + {"Field1"}, + {"Field2", "0"}, + {"Field2", "1", "Account", "0"}, + {"Field2", "1", "Account", "1"}, } if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) @@ -87,301 +76,242 @@ func TestOrderedNavigableMap(t *testing.T) { onm.Set(&FullPath{ Path: "Field2[2]", - PathItems: PathItems{{Field: "Field2", Index: []string{"2"}}}, - }, NewNMData(111)) - expOrder = []PathItems{ - {{Field: "Field1"}}, - {{Field: "Field2", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"1"}}}, - {{Field: "Field2", Index: []string{"2"}}}, + PathItems: []string{"Field2", "2"}, + }, NewLeafNode(111)) + expOrder = [][]string{ + {"Field1"}, + {"Field2", "0"}, + {"Field2", "1", "Account", "0"}, + {"Field2", "1", "Account", "1"}, + {"Field2", "2"}, } if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } onm.Set(&FullPath{ - Path: "Field3.Field4.Field5", - PathItems: PathItems{ - {Field: "Field3"}, - {Field: "Field4"}, - {Field: "Field5"}}, - }, NewNMData(5)) - expOrder = []PathItems{ - {{Field: "Field1"}}, - {{Field: "Field2", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"0"}}}, - { - {Field: "Field2", Index: []string{"1"}}, - {Field: "Account", Index: []string{"1"}}}, - {{Field: "Field2", Index: []string{"2"}}}, - { - {Field: "Field3"}, - {Field: "Field4"}, - {Field: "Field5"}}, + Path: "Field3.Field4.Field5", + PathItems: []string{"Field3", "Field4", "Field5"}, + }, NewLeafNode(5)) + expOrder = [][]string{ + {"Field1"}, + {"Field2", "0"}, + {"Field2", "1", "Account", "0"}, + {"Field2", "1", "Account", "1"}, + {"Field2", "2"}, + {"Field3", "Field4", "Field5"}, } if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, ToJSON(onm.GetOrder())) } - var expnm NMInterface = NavigableMap{ - "Field1": NewNMData(10), - "Field2": &NMSlice{ - NewNMData("1001"), - NavigableMap{ - "Account": &NMSlice{NewNMData(10), NewNMData(11)}, + expnm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode(10), + "Field2": {Type: NMSliceType, Slice: []*DataNode{ + NewLeafNode("1001"), + {Type: NMMapType, Map: map[string]*DataNode{ + "Account": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(11)}}, + }}, + NewLeafNode(111), + }}, + "Field3": {Type: NMMapType, Map: map[string]*DataNode{ + "Field4": {Type: NMMapType, Map: map[string]*DataNode{ + "Field5": NewLeafNode(5), }, - NewNMData(111), - }, - "Field3": NavigableMap{ - "Field4": NavigableMap{ - "Field5": NewNMData(5), }, - }, - } + }}}} if onm.Empty() { t.Error("Expected not empty type") } - if onm.Type() != NMMapType { - t.Errorf("Expected %v ,received: %v", NMDataType, onm.Type()) + if onm.nm.Type != NMMapType { + t.Errorf("Expected %v ,received: %v", NMDataType, onm.nm.Type) } if !reflect.DeepEqual(expnm, onm.nm) { - t.Errorf("Expected %s ,received: %s", expnm, onm.nm) + t.Errorf("Expected %s ,received: %s", ToJSON(expnm), ToJSON(onm.nm)) } // sliceDeNM - exp := &NMSlice{NewNMData("500"), NewNMData("502")} - path := PathItems{{Field: "Field2"}} - if _, err := onm.Set(&FullPath{Path: path.String(), PathItems: path}, exp); err != nil { + exp := []*DataNode{NewLeafNode("500"), NewLeafNode("502")} + path := []string{"Field2"} + if err := onm.SetAsSlice(&FullPath{Path: path[0], PathItems: path}, exp); err != nil { t.Error(err) } - path = PathItems{{Field: "Field2"}} - if val, err := onm.Field(path); err != nil { + path = []string{"Field2"} + if val, err := onm.FieldAsInterface(path); err != nil { t.Error(err) } else if !reflect.DeepEqual(val, exp) { - t.Errorf("Expected %q ,received: %q", exp, val.Interface()) + t.Errorf("Expected %q ,received: %q", ToJSON(exp), ToJSON(val)) } - expOrder = []PathItems{ - {{Field: "Field1"}}, - { - {Field: "Field3"}, - {Field: "Field4"}, - {Field: "Field5"}}, - {{Field: "Field2", Index: []string{"0"}}}, - {{Field: "Field2", Index: []string{"1"}}}, + expOrder = [][]string{ + {"Field1"}, + {"Field3", "Field4", "Field5"}, + {"Field2", "0"}, + {"Field2", "1"}, } if !reflect.DeepEqual(expOrder, onm.GetOrder()) { t.Errorf("Expected %s ,received: %s", expOrder, onm.GetOrder()) } - path = PathItems{{Field: "Field2", Index: []string{"0"}}} + path = []string{"Field2", "0"} if val, err := onm.Field(path); err != nil { t.Error(err) - } else if val.Interface() != "500" { - t.Errorf("Expected %q ,received: %q", "500", val.Interface()) - } - expnm = NavigableMap{ - "Field1": NewNMData(10), - "Field3": NavigableMap{ - "Field4": NavigableMap{ - "Field5": NewNMData(5), - }, - }, - "Field2": &NMSlice{ - NewNMData("500"), - NewNMData("502"), - }, + } else if val.Data != "500" { + t.Errorf("Expected %q ,received: %q", "500", val.Data) } + expnm = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode(10), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{ + "Field4": {Type: NMMapType, Map: map[string]*DataNode{ + "Field5": NewLeafNode(5), + }}, + }}, + "Field2": {Type: NMSliceType, Slice: []*DataNode{ + NewLeafNode("500"), + NewLeafNode("502"), + }}, + }} if !reflect.DeepEqual(expnm, onm.nm) { - t.Errorf("Expected %s ,received: %s", expnm, onm.nm) - } -} - -func TestOrderedNavigableMapString(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{"Field1": NewNMData("1001")}} - expected := `{"Field1":1001}` - if rply := nm.String(); rply != expected { - t.Errorf("Expected %q ,received: %q", expected, rply) - } - nm = &OrderedNavigableMap{nm: NavigableMap{}} - expected = `{}` - if rply := nm.String(); rply != expected { - t.Errorf("Expected %q ,received: %q", expected, rply) - } -} - -func TestOrderedNavigableMapInterface(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{"Field1": NewNMData("1001"), "Field2": NewNMData("1003")}} - expected := NavigableMap{"Field1": NewNMData("1001"), "Field2": NewNMData("1003")} - if rply := nm.Interface(); !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected %s ,received: %s", ToJSON(expected), ToJSON(rply)) + t.Errorf("Expected %s ,received: %s", ToJSON(expnm), ToJSON(onm.nm)) } } func TestOrderedNavigableMapField(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{}} - if _, err := nm.Field(PathItems{{}}); err != ErrNotFound { + nm := &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{}}} + if _, err := nm.Field([]string{"Field1"}); err != ErrNotFound { t.Error(err) } - nm = &OrderedNavigableMap{nm: NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - }} + nm = &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field2": NewLeafNode("1003"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }}} if _, err := nm.Field(nil); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Field(PathItems{{Field: "NaN"}}); err != ErrNotFound { + if _, err := nm.Field([]string{"NaN"}); err != ErrNotFound { t.Error(err) } - if val, err := nm.Field(PathItems{{Field: "Field1"}}); err != nil { + if val, err := nm.Field([]string{"Field1"}); err != nil { t.Error(err) - } else if val.Interface() != "1001" { - t.Errorf("Expected %q ,received: %q", "1001", val.Interface()) + } else if val.Data != "1001" { + t.Errorf("Expected %q ,received: %q", "1001", val.Data) } - if _, err := nm.Field(PathItems{{Field: "Field1", Index: []string{"0"}}}); err != ErrNotFound { + if _, err := nm.Field([]string{"Field1", "0"}); err != ErrNotFound { t.Error(err) } - if val, err := nm.Field(PathItems{{Field: "Field5", Index: []string{"0"}}}); err != nil { + if val, err := nm.Field([]string{"Field5", "0"}); err != nil { t.Error(err) - } else if val.Interface() != 10 { - t.Errorf("Expected %q ,received: %q", 10, val.Interface()) + } else if val.Data != 10 { + t.Errorf("Expected %q ,received: %q", 10, val.Data) } - if _, err := nm.Field(PathItems{{Field: "Field3", Index: []string{"0"}}}); err != ErrNotFound { + if _, err := nm.Field([]string{"Field3", "0"}); err != ErrNotFound { t.Error(err) } - if val, err := nm.Field(PathItems{{Field: "Field3"}, {Field: "Field4"}}); err != nil { + if val, err := nm.Field([]string{"Field3", "Field4"}); err != nil { t.Error(err) - } else if val.Interface() != "Val" { - t.Errorf("Expected %q ,received: %q", "Val", val.Interface()) - } -} - -func TestOrderedNavigableMapType(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{}} - if nm.Type() != NMMapType { - t.Errorf("Expected %v ,received: %v", NMMapType, nm.Type()) + } else if val.Data != "Val" { + t.Errorf("Expected %q ,received: %q", "Val", val.Data) } } func TestOrderedNavigableMapEmpty(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{}} + nm := &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{}}} if !nm.Empty() { t.Error("Expected empty type") } - nm = &OrderedNavigableMap{nm: NavigableMap{"Field1": NewNMData("1001")}} + nm = &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{"Field1": NewLeafNode("1001")}}} if nm.Empty() { t.Error("Expected not empty type") } } -func TestOrderedNavigableMapLen(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{}} - if rply := nm.Len(); rply != 0 { - t.Errorf("Expected 0 ,received: %v", rply) - } - nm = &OrderedNavigableMap{nm: NavigableMap{"Field1": NewNMData("1001")}} - if rply := nm.Len(); rply != 1 { - t.Errorf("Expected 1 ,received: %v", rply) - } -} - func TestOrderedNavigableMapGetSet(t *testing.T) { nm := NewOrderedNavigableMap() nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Account", Index: []string{"0"}}}, + PathItems: []string{"Account", "0"}, Path: "Account", - }, NewNMData(1001)) + }, NewLeafNode(1001)) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Account", Index: []string{"1"}}}, + PathItems: []string{"Account", "1"}, Path: "Account", - }, NewNMData("account_on_new_branch")) + }, NewLeafNode("account_on_new_branch")) - expectedOrder := []PathItems{ - {{Field: "Account", Index: []string{"0"}}}, - {{Field: "Account", Index: []string{"1"}}}, + expectedOrder := [][]string{ + {"Account", "0"}, + {"Account", "1"}, } if receivedOrder := nm.GetOrder(); !reflect.DeepEqual(expectedOrder, receivedOrder) { t.Errorf("Expected %s ,received: %s", expectedOrder, receivedOrder) } nm = &OrderedNavigableMap{ - nm: NavigableMap{ - "Field1": NewNMData(10), - "Field2": &NMSlice{ - NewNMData("1001"), - NavigableMap{ - "Account": &NMSlice{NewNMData(10), NewNMData(11)}, - }, - }, - "Field3": NavigableMap{ - "Field4": NavigableMap{ - "Field5": NewNMData(5), - }, - }, - }, + nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode(10), + "Field2": {Type: NMSliceType, Slice: []*DataNode{ + NewLeafNode("1001"), + {Type: NMMapType, Map: map[string]*DataNode{ + "Account": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(11)}}, + }}, + }}, + "Field3": {Type: NMMapType, Map: map[string]*DataNode{ + "Field4": {Type: NMMapType, Map: map[string]*DataNode{ + "Field5": NewLeafNode(5), + }}, + }}, + }}, orderIdx: NewPathItemList(), orderRef: make(map[string][]*PathItemElement), } - path := PathItems{{Field: "Field1"}} + path := []string{"Field1"} if val, err := nm.Field(path); err != nil { t.Error(err) - } else if val.Interface() != 10 { - t.Errorf("Expected %q ,received: %q", 10, val.Interface()) + } else if val.Data != 10 { + t.Errorf("Expected %q ,received: %q", 10, val.Data) } - path = PathItems{{Field: "Field3"}, {Field: "Field4"}, {Field: "Field5"}} + path = []string{"Field3", "Field4", "Field5"} if val, err := nm.Field(path); err != nil { t.Error(err) - } else if val.Interface() != 5 { - t.Errorf("Expected %q ,received: %q", 5, val.Interface()) + } else if val.Data != 5 { + t.Errorf("Expected %q ,received: %q", 5, val.Data) } - path = PathItems{{Field: "Field2", Index: []string{"2"}}} - if _, err := nm.Set(&FullPath{Path: path.String(), PathItems: path}, NewNMData("500")); err != nil { + path = []string{"Field2", "2"} + if err := nm.Set(&FullPath{Path: strings.Join(path, NestingSep), PathItems: path}, NewLeafNode("500")); err != nil { t.Error(err) } if val, err := nm.Field(path); err != nil { t.Error(err) - } else if val.Interface() != "500" { - t.Errorf("Expected %q ,received: %q", "500", val.Interface()) + } else if val.Data != "500" { + t.Errorf("Expected %q ,received: %q", "500", val.Data) } - path = PathItems{{Field: "Field2", Index: []string{"1"}}, {Field: "Account"}} - if _, err := nm.Set(&FullPath{Path: path.String(), PathItems: path}, NewNMData("5")); err != nil { + path = []string{"Field2", "1", "Account"} + if err := nm.Set(&FullPath{Path: strings.Join(path, NestingSep), PathItems: path}, NewLeafNode("5")); err != nil { t.Error(err) } - path = PathItems{{Field: "Field2", Index: []string{"1"}}, {Field: "Account"}} + path = []string{"Field2", "1", "Account"} if val, err := nm.Field(path); err != nil { t.Error(err) - } else if val.Interface() != "5" { - t.Errorf("Expected %q ,received: %q", "5", val.Interface()) + } else if val.Data != "5" { + t.Errorf("Expected %q ,received: %q", "5", val.Data) } - path = PathItems{{Field: "Field2", Index: []string{"1"}}, {Field: "Account", Index: []string{"0"}}} + path = []string{"Field2", "1", "Account", "0"} if _, err := nm.Field(path); err != ErrNotFound { t.Error(err) } } func TestOrderedNavigableMapFieldAsInterface(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - }} - if _, err := nm.FieldAsInterface(nil); err != ErrWrongPath { - t.Error(err) - } + nm := &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field2": NewLeafNode("1003"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }}} if val, err := nm.FieldAsInterface([]string{"Field3", "Field4"}); err != nil { t.Error(err) @@ -397,15 +327,12 @@ func TestOrderedNavigableMapFieldAsInterface(t *testing.T) { } func TestOrderedNavigableMapFieldAsString(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - }} - if _, err := nm.FieldAsString(nil); err != ErrWrongPath { - t.Error(err) - } + nm := &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field2": NewLeafNode("1003"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }}} if val, err := nm.FieldAsString([]string{"Field3", "Field4"}); err != nil { t.Error(err) @@ -423,26 +350,26 @@ func TestOrderedNavigableMapFieldAsString(t *testing.T) { func TestOrderedNavigableMapGetOrder(t *testing.T) { nm := NewOrderedNavigableMap() nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: []string{"0"}}}, + PathItems: []string{"Field1", "Field2", "0"}, Path: "Field1.Field2[0]", - }, NewNMData("1003")) + }, NewLeafNode("1003")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: []string{"1"}}}, + PathItems: []string{"Field1", "Field2", "1"}, Path: "Field1.Field2[1]", - }, NewNMData("Val")) + }, NewLeafNode("Val")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field3"}, {Field: "Field4"}, {Field: "Field5", Index: []string{"0"}}}, + PathItems: []string{"Field3", "Field4", "Field5", "0"}, Path: "Field3.Field4.Field5", - }, NewNMData("1001")) + }, NewLeafNode("1001")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: []string{"2"}}}, + PathItems: []string{"Field1", "Field2", "2"}, Path: "Field1.Field2[2]", - }, NewNMData(101)) - expected := []PathItems{ - {{Field: "Field1"}, {Field: "Field2", Index: []string{"0"}}}, - {{Field: "Field1"}, {Field: "Field2", Index: []string{"1"}}}, - {{Field: "Field3"}, {Field: "Field4"}, {Field: "Field5", Index: []string{"0"}}}, - {{Field: "Field1"}, {Field: "Field2", Index: []string{"2"}}}, + }, NewLeafNode(101)) + expected := [][]string{ + {"Field1", "Field2", "0"}, + {"Field1", "Field2", "1"}, + {"Field3", "Field4", "Field5", "0"}, + {"Field1", "Field2", "2"}, } if rply := nm.GetOrder(); !reflect.DeepEqual(rply, expected) { t.Errorf("Expected %s ,received: %s", expected, rply) @@ -453,295 +380,264 @@ func TestOrderedNavigableMapGetOrder(t *testing.T) { func TestOrderedNavigableMapSet(t *testing.T) { nm := NewOrderedNavigableMap() - if _, err := nm.Set(nil, nil); err != ErrWrongPath { + if err := nm.Set(nil, nil); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1", Index: []string{"10"}}}, - Path: "Field1[10]", - }, NewNMData("1001")); err != ErrWrongPath { - t.Error(err) - } - if _, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1", Index: []string{"10"}}, {Field: "Field2"}}, - Path: "Field1[10].Field2", - }, NewNMData("1001")); err != ErrWrongPath { - t.Error(err) - } - path := PathItems{{Field: "Field1", Index: []string{"0"}}} - if addedNew, err := nm.Set(&FullPath{ + + path := []string{"Field1", "0"} + if err := nm.Set(&FullPath{ PathItems: path, - Path: path.String(), - }, NewNMData("1001")); err != nil { + Path: strings.Join(path, NestingSep), + }, NewLeafNode("1001")); err != nil { t.Error(err) - } else if !addedNew { - t.Error("Expected the field to be added new") } - nMap := NavigableMap{"Field1": &NMSlice{NewNMData("1001")}} - order := []PathItems{path} + nMap := &DataNode{Type: NMMapType, Map: map[string]*DataNode{"Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1001")}}}} + order := [][]string{path} if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - if _, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1", Index: []string{"0"}}, {}}, + if err := nm.Set(&FullPath{ + PathItems: []string{"Field1", "0", ""}, Path: "Field1[0]", - }, NewNMData("1001")); err != ErrWrongPath { - t.Error(err) - } - if _, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1", Index: []string{"10"}}}, - Path: "Field1[10]", - }, NewNMData("1001")); err != ErrWrongPath { + }, NewLeafNode("1001")); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1", Index: []string{"0"}}, {}}, + if err := nm.SetAsSlice(&FullPath{ + PathItems: []string{"Field1", "0", ""}, Path: "Field1[0]", - }, &NMSlice{}); err != ErrWrongPath { + }, []*DataNode{}); err != ErrWrongPath { t.Error(err) } - if _, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1", Index: []string{"10"}}}, + if err := nm.SetAsSlice(&FullPath{ + PathItems: []string{"Field1", "10"}, Path: "Field[10]", - }, &NMSlice{}); err != ErrWrongPath { + }, []*DataNode{}); err != ErrNotFound { t.Error(err) } - nMap = NavigableMap{"Field1": &NMSlice{NewNMData("1002")}} - order = []PathItems{path} - if addedNew, err := nm.Set(&FullPath{ + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{"Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1002")}}}} + order = [][]string{path} + if err := nm.Set(&FullPath{ PathItems: path, - Path: path.String(), - }, NewNMData("1002")); err != nil { + Path: strings.Join(path, NestingSep), + }, NewLeafNode("1002")); err != nil { t.Error(err) - } else if addedNew { - t.Error("Expected the field to be only updated") } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - path = PathItems{{Field: "Field2"}} - nMap = NavigableMap{ - "Field1": &NMSlice{NewNMData("1002")}, - "Field2": NewNMData("1002"), - } + path = []string{"Field2"} + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1002")}}, + "Field2": NewLeafNode("1002"), + }} order = append(order, path) - if addedNew, err := nm.Set(&FullPath{ + if err := nm.Set(&FullPath{ PathItems: path, - Path: path.String(), - }, NewNMData("1002")); err != nil { + Path: strings.Join(path, NestingSep), + }, NewLeafNode("1002")); err != nil { t.Error(err) - } else if !addedNew { - t.Error("Expected the field to be added new") } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - path = PathItems{{Field: "Field1", Index: []string{"1"}}} - nMap = NavigableMap{ - "Field1": &NMSlice{NewNMData("1002"), NewNMData("1003")}, - "Field2": NewNMData("1002"), - } + path = []string{"Field1", "1"} + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1002"), NewLeafNode("1003")}}, + "Field2": NewLeafNode("1002"), + }} order = append(order, path) - if addedNew, err := nm.Set(&FullPath{ + if err := nm.Set(&FullPath{ PathItems: path, - Path: path.String(), - }, NewNMData("1003")); err != nil { + Path: strings.Join(path, NestingSep), + }, NewLeafNode("1003")); err != nil { t.Error(err) - } else if !addedNew { - t.Error("Expected the field to be added new") } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - path = PathItems{{Field: "Field3"}} - obj := &NMSlice{NewNMData("1004"), NewNMData("1005")} - nMap = NavigableMap{ - "Field1": &NMSlice{NewNMData("1002"), NewNMData("1003")}, - "Field2": NewNMData("1002"), + path = []string{"Field3"} + obj := &DataNode{Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1004"), NewLeafNode("1005")}} + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1002"), NewLeafNode("1003")}}, + "Field2": NewLeafNode("1002"), "Field3": obj, - } - order = append(order, PathItems{{Field: "Field3", Index: []string{"0"}}}, PathItems{{Field: "Field3", Index: []string{"1"}}}) - if addedNew, err := nm.Set(&FullPath{ + }} + order = append(order, []string{"Field3", "0"}, []string{"Field3", "1"}) + if err := nm.SetAsSlice(&FullPath{ PathItems: path, - Path: path.String(), - }, obj); err != nil { + Path: strings.Join(path, NestingSep), + }, obj.Slice); err != nil { t.Error(err) - } else if !addedNew { - t.Error("Expected the field to be added new") } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - obj = &NMSlice{NewNMData("1005"), NewNMData("1006")} - nMap = NavigableMap{ - "Field1": &NMSlice{NewNMData("1005"), NewNMData("1006")}, - "Field2": NewNMData("1002"), - "Field3": &NMSlice{NewNMData("1004"), NewNMData("1005")}, + obj = &DataNode{Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}} + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}}, + "Field2": NewLeafNode("1002"), + "Field3": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1004"), NewLeafNode("1005")}}, + }} + order = [][]string{ + {"Field2"}, + {"Field3", "0"}, + {"Field3", "1"}, + {"Field1", "0"}, + {"Field1", "1"}, } - order = []PathItems{ - {{Field: "Field2"}}, - {{Field: "Field3", Index: []string{"0"}}}, - {{Field: "Field3", Index: []string{"1"}}}, - {{Field: "Field1", Index: []string{"0"}}}, - {{Field: "Field1", Index: []string{"1"}}}, - } - if addedNew, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}}, + if err := nm.SetAsSlice(&FullPath{ + PathItems: []string{"Field1"}, Path: "Field1", - }, obj); err != nil { + }, obj.Slice); err != nil { t.Error(err) - } else if addedNew { - t.Error("Expected the field to be only updated") } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - obj = &NMSlice{NewNMData("1005"), NewNMData("1006")} - nMap = NavigableMap{ - "Field1": &NMSlice{NewNMData("1005"), NewNMData("1006")}, - "Field2": NewNMData("1002"), - "Field3": &NMSlice{NewNMData("1004"), NewNMData("1007")}, + obj = &DataNode{Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}} + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1005"), NewLeafNode("1006")}}, + "Field2": NewLeafNode("1002"), + "Field3": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode("1004"), NewLeafNode("1007")}}, + }} + order = [][]string{ + {"Field2"}, + {"Field3", "0"}, + {"Field1", "0"}, + {"Field1", "1"}, + {"Field3", "1"}, } - order = []PathItems{ - {{Field: "Field2"}}, - {{Field: "Field3", Index: []string{"0"}}}, - {{Field: "Field1", Index: []string{"0"}}}, - {{Field: "Field1", Index: []string{"1"}}}, - {{Field: "Field3", Index: []string{"1"}}}, - } - if addedNew, err := nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field3", Index: []string{"-1"}}}, + if err := nm.Set(&FullPath{ + PathItems: []string{"Field3", "-1"}, Path: "Field3[-1]", - }, NewNMData("1007")); err != nil { + }, NewLeafNode("1007")); err != nil { t.Error(err) - } else if addedNew { - t.Error("Expected the field to be only updated") } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { - t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) + t.Errorf("Expected %s ,received: %s", ToJSON(order), ToJSON(nm.GetOrder())) } } func TestOrderedNavigableMapRemove(t *testing.T) { nm := NewOrderedNavigableMap() nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field2"}}, + PathItems: []string{"Field2"}, Path: "Field2", - }, NewNMData("1003")) + }, NewLeafNode("1003")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field3"}, {Field: "Field4"}}, + PathItems: []string{"Field3", "Field4"}, Path: "Field3.Field4", - }, NewNMData("Val")) + }, NewLeafNode("Val")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}}, + PathItems: []string{"Field1"}, Path: "Field1", - }, NewNMData("1001")) - nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field5"}}, + }, NewLeafNode("1001")) + nm.SetAsSlice(&FullPath{ + PathItems: []string{"Field5"}, Path: "Field5", - }, &NMSlice{NewNMData(10), NewNMData(101)}) + }, []*DataNode{NewLeafNode(10), NewLeafNode(101)}) if err := nm.Remove(&FullPath{}); err != ErrWrongPath { t.Error(err) } - if err := nm.Remove(&FullPath{PathItems: PathItems{}}); err != ErrWrongPath { + if err := nm.Remove(&FullPath{PathItems: []string{}}); err != ErrWrongPath { t.Error(err) } - if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "field"}}, Path: "field"}); err != nil { + if err := nm.Remove(&FullPath{PathItems: []string{"field"}, Path: "field"}); err != nil { t.Error(err) } - if err := nm.Remove(&FullPath{PathItems: PathItems{{Index: []string{"-1"}}, {}}}); err != ErrWrongPath { + if err := nm.Remove(&FullPath{PathItems: []string{"-1", ""}}); err != ErrWrongPath { t.Error(err) } - nMap := NavigableMap{ - "Field1": NewNMData("1001"), - "Field2": NewNMData("1003"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - } - order := []PathItems{ - {{Field: "Field2"}}, - {{Field: "Field3"}, {Field: "Field4"}}, - {{Field: "Field1"}}, - {{Field: "Field5", Index: []string{"0"}}}, - {{Field: "Field5", Index: []string{"1"}}}, + nMap := &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field2": NewLeafNode("1003"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }} + order := [][]string{ + {"Field2"}, + {"Field3", "Field4"}, + {"Field1"}, + {"Field5", "0"}, + {"Field5", "1"}, } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - nMap = NavigableMap{ - "Field1": NewNMData("1001"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - "Field5": &NMSlice{NewNMData(10), NewNMData(101)}, - } - order = []PathItems{ - {{Field: "Field3"}, {Field: "Field4"}}, - {{Field: "Field1"}}, - {{Field: "Field5", Index: []string{"0"}}}, - {{Field: "Field5", Index: []string{"1"}}}, + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + "Field5": {Type: NMSliceType, Slice: []*DataNode{NewLeafNode(10), NewLeafNode(101)}}, + }} + order = [][]string{ + {"Field3", "Field4"}, + {"Field1"}, + {"Field5", "0"}, + {"Field5", "1"}, } - if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field2"}}, Path: "Field2"}); err != nil { + if err := nm.Remove(&FullPath{PathItems: []string{"Field2"}, Path: "Field2"}); err != nil { t.Error(err) } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - nMap = NavigableMap{ - "Field1": NewNMData("1001"), - "Field3": NavigableMap{"Field4": NewNMData("Val")}, - } - order = []PathItems{ - {{Field: "Field3"}, {Field: "Field4"}}, - {{Field: "Field1"}}, + nMap = &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": NewLeafNode("1001"), + "Field3": {Type: NMMapType, Map: map[string]*DataNode{"Field4": NewLeafNode("Val")}}, + }} + order = [][]string{ + {"Field3", "Field4"}, + {"Field1"}, } - if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field5"}}, Path: "Field5"}); err != nil { + if err := nm.Remove(&FullPath{PathItems: []string{"Field5"}, Path: "Field5"}); err != nil { t.Error(err) } if !reflect.DeepEqual(nm.nm, nMap) { - t.Errorf("Expected %s ,received: %s", nMap, nm) + t.Errorf("Expected %s ,received: %s", ToJSON(nMap), ToJSON(nm)) } if !reflect.DeepEqual(nm.GetOrder(), order) { t.Errorf("Expected %s ,received: %s", order, nm.GetOrder()) } - if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field1", Index: []string{"0"}}, {}}}); err != ErrWrongPath { + if err := nm.Remove(&FullPath{PathItems: []string{"Field1", "0", ""}}); err != ErrWrongPath { t.Error(err) } } func TestOrderedNavigableRemote(t *testing.T) { - nm := &OrderedNavigableMap{nm: NavigableMap{"Field1": NewNMData("1001")}} + nm := &OrderedNavigableMap{nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{"Field1": NewLeafNode("1001")}}} eOut := LocalAddr() if rcv := nm.RemoteHost(); !reflect.DeepEqual(eOut, rcv) { t.Errorf("Expecting: %+v, received: %+v", eOut, rcv) @@ -754,7 +650,7 @@ func BenchmarkOrderdNavigableMapSet(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { for _, data := range gen { - if _,err := nm.Set(data.pathItems, NewNMData(data.data)); err != nil { + if _, err := nm.Set(data.pathItems, NewLeafNode(data.data)); err != nil { b.Log(err, data.path) } } @@ -762,22 +658,22 @@ func BenchmarkOrderdNavigableMapSet(b *testing.B) { } func BenchmarkNavigableMapSet(b *testing.B) { - nm := NavigableMap{} + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{}} b.ResetTimer() for n := 0; n < b.N; n++ { for _, data := range gen { - if _,err := nm.Set(data.pathItems, NewNMData(data.data)); err != nil { + if _, err := nm.Set(data.pathItems, NewLeafNode(data.data)); err != nil { b.Log(err, data.path) } } } } func BenchmarkNavigableMapOldSet(b *testing.B) { - nm := NavigableMap{} + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{}} b.ResetTimer() for n := 0; n < b.N; n++ { for _, data := range gen { - if _,err := nm.Set(data.path, data.data); err != nil { + if _, err := nm.Set(data.path, data.data); err != nil { b.Log(err, data.path) } } @@ -787,7 +683,7 @@ func BenchmarkNavigableMapOldSet(b *testing.B) { func BenchmarkOrderdNavigableMapFieldAsInterface(b *testing.B) { nm := NewOrderedNavigableMap() for _, data := range gen { - if _,err := nm.Set(data.pathItems, NewNMData(data.data)); err != nil { + if _, err := nm.Set(data.pathItems, NewLeafNode(data.data)); err != nil { b.Log(err, data.path) } } @@ -805,9 +701,9 @@ func BenchmarkOrderdNavigableMapFieldAsInterface(b *testing.B) { } func BenchmarkNavigableMapFieldAsInterface(b *testing.B) { - nm := NavigableMap{} + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{}} for _, data := range gen { - if _,err := nm.Set(data.pathItems, NewNMData(data.data)); err != nil { + if _, err := nm.Set(data.pathItems, NewLeafNode(data.data)); err != nil { b.Log(err, data.path) } } @@ -825,9 +721,9 @@ func BenchmarkNavigableMapFieldAsInterface(b *testing.B) { } func BenchmarkNavigableMapOldFieldAsInterface(b *testing.B) { - nm := NavigableMap{} + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{}} for _, data := range gen { - if _,err := nm.Set(data.path, data.data); err != nil { + if _, err := nm.Set(data.path, data.data); err != nil { b.Log(err, data.path) } } @@ -865,7 +761,7 @@ func BenchmarkNavigableMapOld1FieldAsInterface(b *testing.B) { func BenchmarkOrderdNavigableMapField(b *testing.B) { nm := NewOrderedNavigableMap() for _, data := range gen { - if _,err := nm.Set(data.pathItems, NewNMData(data.data)); err != nil { + if _, err := nm.Set(data.pathItems, NewLeafNode(data.data)); err != nil { b.Log(err, data.path) } } @@ -883,9 +779,9 @@ func BenchmarkOrderdNavigableMapField(b *testing.B) { } func BenchmarkNavigableMapField(b *testing.B) { - nm := NavigableMap{} + nm := &DataNode{Type: NMMapType, Map: map[string]*DataNode{}} for _, data := range gen { - if _,err := nm.Set(data.pathItems, NewNMData(data.data)); err != nil { + if _, err := nm.Set(data.pathItems, NewLeafNode(data.data)); err != nil { b.Log(err, data.path) } } @@ -906,21 +802,21 @@ func BenchmarkNavigableMapField(b *testing.B) { func TestOrderedNavigableMapRemoveAll(t *testing.T) { nm := NewOrderedNavigableMap() nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field2"}}, + PathItems: []string{"Field2"}, Path: "Field2", - }, NewNMData("1003")) + }, NewLeafNode("1003")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field3"}, {Field: "Field4"}}, + PathItems: []string{"Field3", "Field4"}, Path: "Field3.Field4", - }, NewNMData("Val")) + }, NewLeafNode("Val")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}}, + PathItems: []string{"Field1"}, Path: "Field1", - }, NewNMData("1001")) - nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field5"}}, + }, NewLeafNode("1001")) + nm.SetAsSlice(&FullPath{ + PathItems: []string{"Field5"}, Path: "Field5", - }, &NMSlice{NewNMData(10), NewNMData(101)}) + }, []*DataNode{NewLeafNode(10), NewLeafNode(101)}) expected := NewOrderedNavigableMap() nm.RemoveAll() if !reflect.DeepEqual(nm, expected) { @@ -930,12 +826,12 @@ func TestOrderedNavigableMapRemoveAll(t *testing.T) { func TestOrderedNavigableMapRemove2(t *testing.T) { nm := &OrderedNavigableMap{ - nm: NavigableMap{ - "Field1": &NMSlice{}, - }, + nm: &DataNode{Type: NMMapType, Map: map[string]*DataNode{ + "Field1": {Type: NMSliceType, Slice: []*DataNode{}}, + }}, } expErr := `strconv.Atoi: parsing "nan": invalid syntax` - if err := nm.Remove(&FullPath{PathItems: PathItems{{Field: "Field1", Index: []string{"nan"}}, {}}, Path: "Field1[nan]"}); err == nil || err.Error() != expErr { + if err := nm.Remove(&FullPath{PathItems: []string{"Field1", "nan", ""}, Path: "Field1[nan]"}); err == nil || err.Error() != expErr { t.Errorf("Expected error: %s,received: %v", expErr, err) } } @@ -943,29 +839,29 @@ func TestOrderedNavigableMapRemove2(t *testing.T) { func TestOrderedNavigableMapOrderedFields(t *testing.T) { nm := NewOrderedNavigableMap() nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: []string{"0"}}}, + PathItems: []string{"Field1", "Field2", "0"}, Path: "Field1.Field2[0]", - }, NewNMData("1003")) + }, NewLeafNode("1003")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field1"}, {Field: "Field3", Index: []string{"0"}}}, + PathItems: []string{"Field1", "Field3", "0"}, Path: "Field1.Field3[0]", - }, NewNMData("1004")) + }, NewLeafNode("1004")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field5"}}, + PathItems: []string{"Field5"}, Path: "Field5", - }, NewNMData("1005")) + }, NewLeafNode("1005")) nm.Set(&FullPath{ - PathItems: PathItems{{Field: "Field6"}}, + PathItems: []string{"Field6"}, Path: "Field6", - }, NewNMData("1006")) + }, NewLeafNode("1006")) nm.Remove(&FullPath{ - PathItems: PathItems{{Field: "Field5"}}, + PathItems: []string{"Field5"}, Path: "Field5", }) exp := []interface{}{"1003", "1004", "1006"} rcv := nm.OrderedFields() if !reflect.DeepEqual(exp, rcv) { - t.Errorf("Expected %+v, received %+v", exp, rcv) + t.Errorf("Expected %+v<%T>, received %+v<%T>", exp, exp[0], rcv, rcv[0]) } exp2 := []string{"1003", "1004", "1006"} rcv2 := nm.OrderedFieldsAsStrings() diff --git a/utils/pathitem.go b/utils/pathitem.go index 55845cba6..2f8458762 100644 --- a/utils/pathitem.go +++ b/utils/pathitem.go @@ -89,19 +89,3 @@ func GetPathIndexString(spath string) (opath string, idx *string) { opath = spath[:idxStart] return opath, &idxVal } - -// GetPathIndexSlice returns the path and index as string if index present -// path[index]=>path,[index1,index2] -// path=>path,nil -func GetPathIndexSlice(spath string) (opath string, idx []string) { - idxStart := strings.Index(spath, IdxStart) - if idxStart == -1 || !strings.HasSuffix(spath, IdxEnd) { - return spath, nil - } - idxVal := spath[idxStart+1 : len(spath)-1] - opath = spath[:idxStart] - if strings.Index(idxVal, IdxCombination) == -1 { - return opath, []string{idxVal} - } - return opath, strings.Split(idxVal, IdxCombination) -} diff --git a/utils/pathitem_test.go b/utils/pathitem_test.go index 9555e393d..0664257ad 100644 --- a/utils/pathitem_test.go +++ b/utils/pathitem_test.go @@ -19,7 +19,6 @@ package utils import ( "reflect" - "strings" "testing" ) @@ -54,83 +53,6 @@ func TestStripIdxFromLastPathElm(t *testing.T) { } } -func TestNewPathItems(t *testing.T) { - pathSlice := strings.Split("*req.Field1[0].Account", NestingSep) - expected := PathItems{{Field: MetaReq}, {Field: "Field1", Index: []string{"0"}}, {Field: AccountField}} - if rply := NewPathItems(pathSlice); !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } - pathSlice = []string{} - expected = PathItems{} - if rply := NewPathItems(pathSlice); !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } - - pathSlice = strings.Split("*req.Field1[*raw][0].Account", NestingSep) - expected = PathItems{{Field: MetaReq}, {Field: "Field1", Index: []string{"*raw", "0"}}, {Field: AccountField}} - if rply := NewPathItems(pathSlice); !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } -} - -func TestPathItemString(t *testing.T) { - path := PathItem{Field: MetaReq} - expected := MetaReq - if rply := path.String(); expected != rply { - t.Errorf("Expected: %q, received: %q", expected, rply) - } - path = PathItem{Field: MetaReq, Index: []string{"10"}} - expected = MetaReq + "[10]" - if rply := path.String(); expected != rply { - t.Errorf("Expected: %q, received: %q", expected, rply) - } -} - -func TestPathItemClone(t *testing.T) { - path := PathItem{Field: MetaReq, Index: []string{"0"}} - expected := PathItem{Field: MetaReq, Index: []string{"0"}} - rply := path.Clone() - path.Index[0] = "1" - if !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } - - var path2 PathItem - expected = PathItem{} - rply = path2.Clone() - if !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } -} - -func TestPathItemsString(t *testing.T) { - expected := "*req.Field1[0].Account" - path := NewPathItems(strings.Split(expected, NestingSep)) - if rply := path.String(); expected != rply { - t.Errorf("Expected: %q, received: %q", expected, rply) - } - expected = "" - path = nil - if rply := path.String(); expected != rply { - t.Errorf("Expected: %q, received: %q", expected, rply) - } -} - -func TestPathItemsClone(t *testing.T) { - path := NewPathItems(strings.Split("*req.Field1[0].Account", NestingSep)) - expected := NewPathItems(strings.Split("*req.Field1[0].Account", NestingSep)) - rply := path.Clone() - path[0] = PathItem{} - if !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } - expected = nil - path = nil - rply = path.Clone() - if !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %s, received: %s", ToJSON(expected), ToJSON(rply)) - } -} func TestGetPathWithoutIndex(t *testing.T) { expected := "field" if rply := GetPathWithoutIndex("field[index]"); expected != rply { @@ -142,27 +64,12 @@ func TestGetPathWithoutIndex(t *testing.T) { } } -func TestPathItemsSlice(t *testing.T) { - expected := []string{"*req", "Field1[0]", "Account"} - path := NewPathItems(expected) - if rply := path.Slice(); !reflect.DeepEqual(expected, rply) { - t.Errorf("Expected: %q, received: %q", expected, rply) - } -} - func TestNewFullPath(t *testing.T) { expected := &FullPath{ - PathItems: []PathItem{ - { - Field: "test", - }, - { - Field: "path", - }, - }, - Path: "test.path", + PathItems: []string{"test", "path"}, + Path: "test.path", } - if rcv := NewFullPath("test.path", NestingSep); !reflect.DeepEqual(rcv, expected) { + if rcv := NewFullPath("test.path"); !reflect.DeepEqual(rcv, expected) { t.Errorf("Expected %+v \n, received %+v", ToJSON(expected), ToJSON(rcv)) } } diff --git a/utils/pathitemlist_test.go b/utils/pathitemlist_test.go index 6dd82ae7c..c9a1c5eef 100644 --- a/utils/pathitemlist_test.go +++ b/utils/pathitemlist_test.go @@ -27,14 +27,14 @@ import ( func TestPathItemListNext(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushFront(node3) list.PushFront(node2) list.PushFront(node1) - if !reflect.DeepEqual("path2", list.Front().Next().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Next().Value.String()) + if !reflect.DeepEqual("path2", strings.Join(list.Front().Next().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Next().Value, NestingSep)) } } @@ -49,14 +49,14 @@ func TestPathItemListNextNil(t *testing.T) { func TestPathItemListPrev(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushFront(node3) list.PushFront(node2) list.PushFront(node1) - if !reflect.DeepEqual("path2", list.Back().Prev().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Prev().Value.String()) + if !reflect.DeepEqual("path2", strings.Join(list.Back().Prev().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Prev().Value, NestingSep)) } } @@ -72,9 +72,9 @@ func TestPathItemListPrevNil(t *testing.T) { func TestPathItemListInit(t *testing.T) { list := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushFront(node3) list.PushFront(node2) list.PushFront(node1) @@ -93,9 +93,9 @@ func TestPathItemListNewPathItemList(t *testing.T) { func TestPathItemListLen(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushFront(node3) list.PushFront(node2) list.PushFront(node1) @@ -106,12 +106,12 @@ func TestPathItemListLen(t *testing.T) { func TestPathItemListFront(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list.PushBack(node1) list.PushBack(node2) - if !reflect.DeepEqual("path1", list.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Value, NestingSep)) } } @@ -126,12 +126,12 @@ func TestPathItemListFrontNil(t *testing.T) { func TestPathItemListBack(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list.PushBack(node1) list.PushBack(node2) - if !reflect.DeepEqual("path2", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + if !reflect.DeepEqual("path2", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } @@ -156,143 +156,143 @@ func TestPathItemListLazyInit(t *testing.T) { func TestPathItemListInsert(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list.PushBack(node1) list.PushBack(node2) list.insert(list.Front(), list.Back()) - if !reflect.DeepEqual("path1", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } func TestPathItemListInsertValue(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list.PushBack(node1) list.PushBack(node2) - list.insertValue(PathItems{{Field: "path3"}}, list.Back()) - if !reflect.DeepEqual("path3", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + list.insertValue([]string{"path3"}, list.Back()) + if !reflect.DeepEqual("path3", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } func TestPathItemListRemoveLowerCase(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.remove(list.Back()) - if !reflect.DeepEqual("path2", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + if !reflect.DeepEqual("path2", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } func TestPathItemListRemoveUpperCase1(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.Remove(list.Front().Next()) - if !reflect.DeepEqual("path3", list.Front().Next().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Next().Value.String()) + if !reflect.DeepEqual("path3", strings.Join(list.Front().Next().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Next().Value, NestingSep)) } } func TestPathItemListRemoveUpperCase2(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"pathA"}) - node2 := NewPathItems([]string{"pathB"}) + node1 := []string{"pathA"} + node2 := []string{"pathB"} list1.PushBack(node1) list2.PushBack(node2) list1.Remove(list2.Front()) - if !reflect.DeepEqual("pathA", list1.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Front().Value.String()) + if !reflect.DeepEqual("pathA", strings.Join(list1.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Front().Value, NestingSep)) } } func TestPathItemListMoveEqual(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.move(list.Back(), list.Back()) - if !reflect.DeepEqual("path3", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + if !reflect.DeepEqual("path3", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } func TestPathItemListMove(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.move(list.Front(), list.Back()) - if !reflect.DeepEqual("path3", list.Front().next.Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().next.Value.String()) + if !reflect.DeepEqual("path3", strings.Join(list.Front().next.Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().next.Value, NestingSep)) } - if !reflect.DeepEqual("path1", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } - if !reflect.DeepEqual("path2", list.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Value.String()) + if !reflect.DeepEqual("path2", strings.Join(list.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Value, NestingSep)) } } func TestPathItemListPushFront(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) - list.PushFront(PathItems{{Field: "path4"}}) - if !reflect.DeepEqual("path4", list.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Value.String()) + list.PushFront([]string{"path4"}) + if !reflect.DeepEqual("path4", strings.Join(list.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Value, NestingSep)) } } func TestPathItemListPushBack(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) - list.PushBack(PathItems{{Field: "path4"}}) - if !reflect.DeepEqual("path4", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + list.PushBack([]string{"path4"}) + if !reflect.DeepEqual("path4", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } func TestPathItemListInsertBefore(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) - list.InsertBefore(PathItems{{Field: "path4"}}, list.Back()) - if !reflect.DeepEqual("path4", list.Back().Prev().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Prev().Value.String()) + list.InsertBefore([]string{"path4"}, list.Back()) + if !reflect.DeepEqual("path4", strings.Join(list.Back().Prev().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Prev().Value, NestingSep)) } } @@ -301,11 +301,11 @@ func TestPathItemListInsertBeforeNil(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() var received *PathItemElement - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list1.PushBack(node1) list2.PushBack(node2) - received = list1.InsertBefore(PathItems{{Field: "path4"}}, list2.Back()) + received = list1.InsertBefore([]string{"path4"}, list2.Back()) if received != nil { t.Errorf("Expecting: <%+v>, received: <%+v>", nil, received) } @@ -313,15 +313,15 @@ func TestPathItemListInsertBeforeNil(t *testing.T) { func TestPathItemListInsertAfter(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) - list.InsertAfter(PathItems{{Field: "path4"}}, list.Front()) - if !reflect.DeepEqual("path4", list.Front().Next().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Next().Value.String()) + list.InsertAfter([]string{"path4"}, list.Front()) + if !reflect.DeepEqual("path4", strings.Join(list.Front().Next().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Next().Value, NestingSep)) } } @@ -329,11 +329,11 @@ func TestPathItemListInsertAfterNil(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() var received *PathItemElement - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list1.PushBack(node1) list2.PushBack(node2) - received = list1.InsertAfter(PathItems{{Field: "path4"}}, list2.Back()) + received = list1.InsertAfter([]string{"path4"}, list2.Back()) if received != nil { t.Errorf("Expecting: <%+v>, received: <%+v>", nil, received) } @@ -342,84 +342,84 @@ func TestPathItemListInsertAfterNil(t *testing.T) { func TestPathItemListMoveToFrontCase1(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list1.PushBack(node1) list2.PushBack(node2) list1.MoveToFront(list2.Back()) - if !reflect.DeepEqual("path1", list1.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Front().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list1.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Front().Value, NestingSep)) } } func TestPathItemListMoveToFrontCase2(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.MoveToFront(list.Back()) - if !reflect.DeepEqual("path3", list.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Value.String()) + if !reflect.DeepEqual("path3", strings.Join(list.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Value, NestingSep)) } } func TestPathItemListMoveToBackCase1(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list1.PushBack(node1) list2.PushBack(node2) list1.MoveToBack(list2.Back()) - if !reflect.DeepEqual("path1", list1.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Back().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list1.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Back().Value, NestingSep)) } } func TestPathItemListMoveToBackCase2(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.MoveToBack(list.Front()) - if !reflect.DeepEqual("path1", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Front().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Front().Value, NestingSep)) } } func TestPathItemListMoveBeforeCase1(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) + node1 := []string{"path1"} + node2 := []string{"path2"} list1.PushBack(node1) list2.PushBack(node2) list1.MoveBefore(list2.Front(), list1.Back()) - if !reflect.DeepEqual("path1", list1.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Front().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list1.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Front().Value, NestingSep)) } } func TestPathItemListMoveBeforeCase2(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.MoveBefore(list.Front(), list.Back()) - if !reflect.DeepEqual("path1", list.Back().Prev().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Prev().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list.Back().Prev().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Prev().Value, NestingSep)) } } @@ -427,28 +427,28 @@ func TestPathItemListMoveBeforeCase2(t *testing.T) { func TestPathItemListMoveAfterCase1(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"pathA"}) - node2 := NewPathItems([]string{"pathB"}) + node1 := []string{"pathA"} + node2 := []string{"pathB"} list1.PushBack(node1) list2.PushBack(node2) list1.MoveAfter(list2.Front(), list1.Back()) - if !reflect.DeepEqual("pathA", list1.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Back().Value.String()) + if !reflect.DeepEqual("pathA", strings.Join(list1.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Back().Value, NestingSep)) } } func TestPathItemListMoveAfterCase2(t *testing.T) { list := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - node3 := NewPathItems([]string{"path3"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + node3 := []string{"path3"} list.PushBack(node1) list.PushBack(node2) list.PushBack(node3) list.MoveAfter(list.Front(), list.Back()) - if !reflect.DeepEqual("path1", list.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list.Back().Value.String()) + if !reflect.DeepEqual("path1", strings.Join(list.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list.Back().Value, NestingSep)) } } @@ -456,40 +456,40 @@ func TestPathItemListMoveAfterCase2(t *testing.T) { func TestPathItemListPushBackList(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - nodeA := NewPathItems([]string{"pathA"}) - nodeB := NewPathItems([]string{"pathB"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + nodeA := []string{"pathA"} + nodeB := []string{"pathB"} list1.PushBack(node1) list1.PushBack(node2) list2.PushBack(nodeA) list2.PushBack(nodeB) list1.PushBackList(list2) - if !reflect.DeepEqual("pathB", list1.Back().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Back().Value.String()) + if !reflect.DeepEqual("pathB", strings.Join(list1.Back().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Back().Value, NestingSep)) } - if !reflect.DeepEqual("pathA", list1.Back().Prev().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Back().Prev().Value.String()) + if !reflect.DeepEqual("pathA", strings.Join(list1.Back().Prev().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Back().Prev().Value, NestingSep)) } } func TestPathItemListPushFrontList(t *testing.T) { list1 := NewPathItemList() list2 := NewPathItemList() - node1 := NewPathItems([]string{"path1"}) - node2 := NewPathItems([]string{"path2"}) - nodeA := NewPathItems([]string{"pathA"}) - nodeB := NewPathItems([]string{"pathB"}) + node1 := []string{"path1"} + node2 := []string{"path2"} + nodeA := []string{"pathA"} + nodeB := []string{"pathB"} list1.PushBack(node1) list1.PushBack(node2) list2.PushBack(nodeA) list2.PushBack(nodeB) list1.PushFrontList(list2) - if !reflect.DeepEqual("pathA", list1.Front().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Front().Value.String()) + if !reflect.DeepEqual("pathA", strings.Join(list1.Front().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Front().Value, NestingSep)) } - if !reflect.DeepEqual("pathB", list1.Front().Next().Value.String()) { - t.Errorf("Expecting: , received: <%+v>", list1.Front().Next().Value.String()) + if !reflect.DeepEqual("pathB", strings.Join(list1.Front().Next().Value, NestingSep)) { + t.Errorf("Expecting: , received: <%+v>", strings.Join(list1.Front().Next().Value, NestingSep)) } } @@ -572,11 +572,12 @@ func BenchmarkGetPathIndexSlice(b *testing.B) { } } +/* func BenchmarkGetPathIndexSliceSplit(b *testing.B) { for i := 0; i < b.N; i++ { GetPathIndexSlice(benchPath) } -} +}*/ func BenchmarkGetPathIndexSliceStringsIndex(b *testing.B) { for i := 0; i < b.N; i++ { GetPathIndexSliceStringsIndex(benchPath) diff --git a/utils/reflect.go b/utils/reflect.go index 13986d23f..7fa738000 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -117,11 +117,11 @@ func ReflectFieldAsString(intf interface{}, fldName, extraFieldsLabel string) (s } func IfaceAsTime(itm interface{}, timezone string) (t time.Time, err error) { - switch itm.(type) { + switch v := itm.(type) { case time.Time: - return itm.(time.Time), nil + return v, nil case string: - return ParseTimeDetectLayout(itm.(string), timezone) + return ParseTimeDetectLayout(v, timezone) default: err = fmt.Errorf("cannot convert field: %+v to time.Time", itm) } @@ -279,17 +279,17 @@ func IfaceAsFloat64(itm interface{}) (f float64, err error) { } func IfaceAsBool(itm interface{}) (b bool, err error) { - switch itm.(type) { + switch v := itm.(type) { case bool: - return itm.(bool), nil + return v, nil case string: - return strconv.ParseBool(itm.(string)) + return strconv.ParseBool(v) case int: - return itm.(int) > 0, nil + return v > 0, nil case int64: - return itm.(int64) > 0, nil + return v > 0, nil case float64: - return itm.(float64) > 0, nil + return v > 0, nil default: err = fmt.Errorf("cannot convert field: %+v to bool", itm) } diff --git a/utils/reflect_test.go b/utils/reflect_test.go index 8a73a3af1..2cfd5f400 100644 --- a/utils/reflect_test.go +++ b/utils/reflect_test.go @@ -733,18 +733,18 @@ type TestA struct { type TestASlice []*TestA -func (_ *TestA) TestFunc() string { +func (*TestA) TestFunc() string { return "This is a test function on a structure" } -func (_ *TestA) TestFuncWithParam(param string) string { +func (*TestA) TestFuncWithParam(param string) string { return "Invalid" } -func (_ *TestA) TestFuncWithError() (string, error) { +func (*TestA) TestFuncWithError() (string, error) { return "TestFuncWithError", nil } -func (_ *TestA) TestFuncWithError2() (string, error) { +func (*TestA) TestFuncWithError2() (string, error) { return "TestFuncWithError2", ErrPartiallyExecuted } @@ -756,7 +756,7 @@ func TestReflectFieldMethodInterface(t *testing.T) { } else if ifValue != "TestStructField" { t.Errorf("Expecting: TestStructField, received: %+v", ifValue) } - ifValue, err = ReflectFieldMethodInterface(a, "InexistentField") + _, err = ReflectFieldMethodInterface(a, "InexistentField") if err != ErrNotFound { t.Error(err) } @@ -772,7 +772,7 @@ func TestReflectFieldMethodInterface(t *testing.T) { } else if ifValue != "TestFuncWithError" { t.Errorf("Expecting: TestFuncWithError, received: %+v", ifValue) } - ifValue, err = ReflectFieldMethodInterface(a, "TestFuncWithError2") + _, err = ReflectFieldMethodInterface(a, "TestFuncWithError2") if err == nil || err != ErrPartiallyExecuted { t.Error(err) } @@ -988,8 +988,7 @@ func TestIfaceAsDurationDefaultError(t *testing.T) { } func TestIfaceAsDurationCaseUInt(t *testing.T) { - var test uint - test = 127 + var test uint = 127 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "127ns") { t.Errorf("Expected <127ns> ,received: <%+v>", response) @@ -997,8 +996,7 @@ func TestIfaceAsDurationCaseUInt(t *testing.T) { } func TestIfaceAsDurationCaseInt8(t *testing.T) { - var test int8 - test = 127 + var test int8 = 127 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "127ns") { t.Errorf("Expected <127ns> ,received: <%+v>", response) @@ -1006,8 +1004,7 @@ func TestIfaceAsDurationCaseInt8(t *testing.T) { } func TestIfaceAsDurationCaseNegInt8(t *testing.T) { - var test int8 - test = -127 + var test int8 = -127 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "-127ns") { t.Errorf("Expected <-127ns> ,received: <%+v>", response) @@ -1015,8 +1012,7 @@ func TestIfaceAsDurationCaseNegInt8(t *testing.T) { } func TestIfaceAsDurationCaseUInt8(t *testing.T) { - var test uint8 - test = 127 + var test uint8 = 127 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "127ns") { t.Errorf("Expected <127ns> ,received: <%+v>", response) @@ -1024,8 +1020,7 @@ func TestIfaceAsDurationCaseUInt8(t *testing.T) { } func TestIfaceAsDurationCaseInt16(t *testing.T) { - var test int16 - test = 32767 + var test int16 = 32767 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "32.767µs") { t.Errorf("Expected <32.767µs> ,received: <%+v>", response) @@ -1033,8 +1028,7 @@ func TestIfaceAsDurationCaseInt16(t *testing.T) { } func TestIfaceAsDurationCaseNegInt16(t *testing.T) { - var test int16 - test = -32767 + var test int16 = -32767 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "-32.767µs") { t.Errorf("Expected <-32.767µs> ,received: <%+v>", response) @@ -1042,8 +1036,7 @@ func TestIfaceAsDurationCaseNegInt16(t *testing.T) { } func TestIfaceAsDurationCaseUInt16(t *testing.T) { - var test uint16 - test = 32767 + var test uint16 = 32767 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "32.767µs") { t.Errorf("Expected <32.767µs> ,received: <%+v>", response) @@ -1051,8 +1044,7 @@ func TestIfaceAsDurationCaseUInt16(t *testing.T) { } func TestIfaceAsDurationCaseInt32(t *testing.T) { - var test int32 - test = 2147483647 + var test int32 = 2147483647 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "2.147483647s") { t.Errorf("Expected <2.147483647s> ,received: <%+v>", response) @@ -1060,8 +1052,7 @@ func TestIfaceAsDurationCaseInt32(t *testing.T) { } func TestIfaceAsDurationCaseNegInt32(t *testing.T) { - var test int32 - test = -2147483647 + var test int32 = -2147483647 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "-2.147483647s") { t.Errorf("Expected <-2.147483647s> ,received: <%+v>", response) @@ -1069,8 +1060,7 @@ func TestIfaceAsDurationCaseNegInt32(t *testing.T) { } func TestIfaceAsDurationCaseUInt32(t *testing.T) { - var test uint32 - test = 2147483647 + var test uint32 = 2147483647 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "2.147483647s") { t.Errorf("Expected <2.147483647s> ,received: <%+v>", response) @@ -1078,8 +1068,7 @@ func TestIfaceAsDurationCaseUInt32(t *testing.T) { } func TestIfaceAsDurationCaseInt64(t *testing.T) { - var test int64 - test = 9223372036854775807 + var test int64 = 9223372036854775807 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "2562047h47m16.854775807s") { t.Errorf("Expected <2562047h47m16.854775807s> ,received: <%+v>", response) @@ -1087,8 +1076,7 @@ func TestIfaceAsDurationCaseInt64(t *testing.T) { } func TestIfaceAsDurationCaseNegInt64(t *testing.T) { - var test int64 - test = -9223372036854775807 + var test int64 = -9223372036854775807 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "-2562047h47m16.854775807s") { t.Errorf("Expected <-2562047h47m16.854775807s> ,received: <%+v>", response) @@ -1096,8 +1084,7 @@ func TestIfaceAsDurationCaseNegInt64(t *testing.T) { } func TestIfaceAsDurationCaseUInt64(t *testing.T) { - var test uint64 - test = 9223372036854775807 + var test uint64 = 9223372036854775807 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "2562047h47m16.854775807s") { t.Errorf("Expected <2562047h47m16.854775807s> ,received: <%+v>", response) @@ -1105,8 +1092,7 @@ func TestIfaceAsDurationCaseUInt64(t *testing.T) { } func TestIfaceAsDurationCaseFloat32(t *testing.T) { - var test float32 - test = 9.5555555 + var test float32 = 9.5555555 response, _ := IfaceAsDuration(test) if !reflect.DeepEqual(response.String(), "9ns") { t.Errorf("Expected <9ns> ,received: <%+v>", response) @@ -1114,10 +1100,8 @@ func TestIfaceAsDurationCaseFloat32(t *testing.T) { } func TestIfaceAsInt6432to64(t *testing.T) { - var test int32 - var expected int64 - test = 2147483647 - expected = 2147483647 + var test int32 = 2147483647 + var expected int64 = 2147483647 response, _ := IfaceAsInt64(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1125,8 +1109,7 @@ func TestIfaceAsInt6432to64(t *testing.T) { } func TestIfaceAsInt64Default(t *testing.T) { - var test bool - test = true + var test bool = true _, err := IfaceAsInt64(test) if err == nil || err.Error() != "cannot convert field: true to int" { t.Errorf("Expecting ,received: <%+v>", err) @@ -1134,8 +1117,7 @@ func TestIfaceAsInt64Default(t *testing.T) { } func TestIfaceAsInt64Nanosecs(t *testing.T) { - var test time.Duration - test = 2147483647 + var test time.Duration = 2147483647 response, _ := IfaceAsInt64(test) if !reflect.DeepEqual(response, test.Nanoseconds()) { t.Errorf("Expected <%+v> ,received: <%+v>", test.Nanoseconds(), response) @@ -1143,8 +1125,7 @@ func TestIfaceAsInt64Nanosecs(t *testing.T) { } func TestIfaceAsTInt64Default(t *testing.T) { - var test bool - test = true + var test bool = true _, err := IfaceAsTInt64(test) if err == nil || err.Error() != "cannot convert field: true to int" { t.Errorf("Expecting : true to int> ,received: <%+v>", err) @@ -1152,8 +1133,7 @@ func TestIfaceAsTInt64Default(t *testing.T) { } func TestIfaceAsTInt64Nanosecs(t *testing.T) { - var test time.Duration - test = 2147483647 + var test time.Duration = 2147483647 response, _ := IfaceAsTInt64(test) if !reflect.DeepEqual(response, test.Nanoseconds()) { t.Errorf("Expected <%+v> ,received: <%+v>", test.Nanoseconds(), response) @@ -1161,8 +1141,7 @@ func TestIfaceAsTInt64Nanosecs(t *testing.T) { } func TestIfaceAsBoolInt64(t *testing.T) { - var test int64 - test = 2147483647 + var test int64 = 2147483647 response, _ := IfaceAsBool(test) if !reflect.DeepEqual(response, true) { t.Errorf("Expected <%+v> ,received: <%+v>", true, response) @@ -1170,8 +1149,7 @@ func TestIfaceAsBoolInt64(t *testing.T) { } func TestIfaceAsBoolDefault(t *testing.T) { - var test uint64 - test = 2147483647 + var test uint64 = 2147483647 _, err := IfaceAsBool(test) if err == nil || err.Error() != "cannot convert field: 2147483647 to bool" { t.Errorf("Expecting ,received: <%+v>", err) @@ -1179,10 +1157,8 @@ func TestIfaceAsBoolDefault(t *testing.T) { } func TestIfaceAsStringInt32(t *testing.T) { - var test int32 - var expected string - test = 2147483647 - expected = "2147483647" + var test int32 = 2147483647 + expected := "2147483647" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1190,10 +1166,8 @@ func TestIfaceAsStringInt32(t *testing.T) { } func TestIfaceAsStringInt32Neg(t *testing.T) { - var test int32 - var expected string - test = -2147483647 - expected = "-2147483647" + var test int32 = -2147483647 + expected := "-2147483647" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1201,10 +1175,8 @@ func TestIfaceAsStringInt32Neg(t *testing.T) { } func TestIfaceAsStringInt64Neg(t *testing.T) { - var test int64 - var expected string - test = -9223372036854775807 - expected = "-9223372036854775807" + var test int64 = -9223372036854775807 + expected := "-9223372036854775807" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1212,10 +1184,8 @@ func TestIfaceAsStringInt64Neg(t *testing.T) { } func TestIfaceAsStringUInt32(t *testing.T) { - var test uint32 - var expected string - test = 2147483647 - expected = "2147483647" + var test uint32 = 2147483647 + expected := "2147483647" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1223,10 +1193,8 @@ func TestIfaceAsStringUInt32(t *testing.T) { } func TestIfaceAsStringUInt64(t *testing.T) { - var test uint64 - var expected string - test = 9223372036854775807 - expected = "9223372036854775807" + var test uint64 = 9223372036854775807 + expected := "9223372036854775807" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1234,10 +1202,8 @@ func TestIfaceAsStringUInt64(t *testing.T) { } func TestIfaceAsStringFloat32(t *testing.T) { - var test float32 - var expected string - test = 2.5 - expected = "2.5" + var test float32 = 2.5 + expected := "2.5" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) @@ -1245,27 +1211,16 @@ func TestIfaceAsStringFloat32(t *testing.T) { } func TestIfaceAsStringFloat32Neg(t *testing.T) { - var test float32 - var expected string - test = -2.5 - expected = "-2.5" + var test float32 = -2.5 + expected := "-2.5" response := IfaceAsString(test) if !reflect.DeepEqual(response, expected) { t.Errorf("Expected <%+v> ,received: <%+v>", expected, response) } } -func TestIfaceAsStringNMInterface(t *testing.T) { - var test NMInterface = NewNMData("1001") - response := IfaceAsString(test) - if !reflect.DeepEqual(response, "1001") { - t.Errorf("Expected <1001> ,received: <%+v>", response) - } -} - func TestGetBasicTypeUint(t *testing.T) { - var test interface{} - test = uint8(123) + var test interface{} = uint8(123) valItm := reflect.ValueOf(test) response := GetBasicType(test) if !reflect.DeepEqual(valItm.Uint(), response) { @@ -1389,8 +1344,7 @@ func TestEqualToError(t *testing.T) { } func TestIfaceAsStringDefault(t *testing.T) { - var test int8 - test = 2 + var test int8 = 2 response := IfaceAsString(test) if !reflect.DeepEqual(response, "2") { t.Errorf("Expected <2> ,received: <%+v>", response) @@ -1606,7 +1560,7 @@ func TestReflectFieldMethodInterfaceArrayDefault(t *testing.T) { } } -func (_ *TestA) TestFuncWithParamError2() (string, string, string) { +func (*TestA) TestFuncWithParamError2() (string, string, string) { return "Invalid", "invalid2", "invalid3" } @@ -1626,7 +1580,7 @@ func TestReflectFieldMethodInterfaceElseError2(t *testing.T) { } } -func (_ *TestA) TestFuncWithParamError3() (string, string) { +func (*TestA) TestFuncWithParamError3() (string, string) { return "Invalid", "invalid2" } diff --git a/utils/rsrfilters.go b/utils/rsrfilters.go index 71058618e..22fb14f26 100644 --- a/utils/rsrfilters.go +++ b/utils/rsrfilters.go @@ -127,7 +127,7 @@ func (rsrFltr *RSRFilter) Pass(val string) bool { } return gt != rsrFltr.negative } - return (strings.Index(val, rsrFltr.filterRule) != -1) != rsrFltr.negative // default is string index + return strings.Contains(val, rsrFltr.filterRule) != rsrFltr.negative // default is string index } func ParseRSRFilters(fldsStr, sep string) (RSRFilters, error) {