avoid redundant AttributesWithName call in radiusDP

This commit is contained in:
armirveliaj
2026-01-28 07:48:42 -05:00
committed by Dan Christian Bogos
parent 0400f4a177
commit 84f0338605
2 changed files with 46 additions and 2 deletions

View File

@@ -83,8 +83,8 @@ func (pk *radiusDP) FieldAsInterface(fldPath []string) (data any, err error) {
} else {
return // data found in cache
}
if len(pk.req.AttributesWithName(fldPath[0], "")) != 0 {
data = pk.req.AttributesWithName(fldPath[0], "")[0].GetStringValue()
if attrs := pk.req.AttributesWithName(fldPath[0], ""); len(attrs) != 0 {
data = attrs[0].GetStringValue()
}
pk.cache.Set(fldPath, data)
return

View File

@@ -141,3 +141,47 @@ func TestRadiusDPFieldAsString(t *testing.T) {
t.Errorf("Expecting: flopsy, received: <%s>", data)
}
}
func TestRadiusDPFieldAsInterfaceCached(t *testing.T) {
pkt := radigo.NewPacket(radigo.AccountingRequest, 1, dictRad, coder, "CGRateS.org")
if err := pkt.AddAVPWithName("User-Name", "cgr1", ""); err != nil {
t.Error(err)
}
if err := pkt.AddAVPWithName("Acct-Session-Time", "3600", ""); err != nil {
t.Error(err)
}
if err := pkt.AddAVPWithName("Password", "pass123", ""); err != nil {
t.Error(err)
}
dp := newRADataProvider(pkt)
if data, err := dp.FieldAsInterface([]string{"User-Name"}); err != nil {
t.Error(err)
} else if data != "cgr1" {
t.Errorf("Expecting: cgr1, received: <%v>", data)
}
if data, err := dp.FieldAsInterface([]string{"Acct-Session-Time"}); err != nil {
t.Error(err)
} else if data != "3600" {
t.Errorf("Expecting: 3600, received: <%v>", data)
}
if data, err := dp.FieldAsInterface([]string{"Password"}); err != nil {
t.Error(err)
} else if data != "pass123" {
t.Errorf("Expecting: pass123, received: <%v>", data)
}
if data, err := dp.FieldAsInterface([]string{"Non-Existent-Field"}); err != nil {
t.Error(err)
} else if data != nil {
t.Errorf("Expecting: nil, received: <%v>", data)
}
if _, err := dp.FieldAsInterface([]string{"Field1", "Field2"}); err != utils.ErrNotFound {
t.Errorf("Expecting: ErrNotFound, received: <%v>", err)
}
}