From ade4f818047dada4a37bef79a566e8be3a5a75b7 Mon Sep 17 00:00:00 2001 From: t-umeda Date: Mon, 5 Aug 2024 13:03:12 +0900 Subject: [PATCH] refactor: Improve code readability --- pkg/diameter/avp.go | 9 ++++++++- pkg/diameter/avp_dict.go | 4 ++-- pkg/diameter/diameter.go | 38 ++++++++++++++++++-------------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/pkg/diameter/avp.go b/pkg/diameter/avp.go index 180ee4b..058d01c 100644 --- a/pkg/diameter/avp.go +++ b/pkg/diameter/avp.go @@ -15,7 +15,14 @@ type AVP struct { Value interface{} } -type AvpMeta struct { +type AVPMeta struct { + code uint32 + flag uint8 + vendor uint32 + value datatype.Type +} + +type AVPMetaC struct { code uint32 flag uint8 vendor uint32 diff --git a/pkg/diameter/avp_dict.go b/pkg/diameter/avp_dict.go index e5a142e..c6ba3ef 100644 --- a/pkg/diameter/avp_dict.go +++ b/pkg/diameter/avp_dict.go @@ -4,13 +4,13 @@ import ( "github.com/fiorix/go-diameter/v4/diam/avp" ) -var avpDict map[string]*AvpMeta +var avpDict map[string]*AVPMetaC const vendorId3GPP = 10415 func init() { // TODO: generate this part from XML dictionary files - avpDict = map[string]*AvpMeta{ + avpDict = map[string]*AVPMetaC{ "User-Name": {code: avp.UserName, flag: avp.Mbit, vendor: 0, converter: toUTF8String}, "Auth-Session-State": {code: avp.AuthSessionState, flag: avp.Mbit, vendor: 0, converter: toEnumerated}, "Visited-PLMN-Id": {code: avp.VisitedPLMNID, flag: avp.Vbit | avp.Mbit, vendor: vendorId3GPP, converter: toOctetString}, diff --git a/pkg/diameter/diameter.go b/pkg/diameter/diameter.go index 4cbbef4..13ce0f4 100644 --- a/pkg/diameter/diameter.go +++ b/pkg/diameter/diameter.go @@ -306,17 +306,16 @@ func (c *K6DiameterClient) SendAIR(options ConnectionOptions) (bool, error) { sid = c.generateSessionID() } m := diam.NewRequest(diam.AuthenticationInformation, diam.TGPP_S6A_APP_ID, dict.Default) - _, err = m.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String(sid)) - if err != nil { - return false, errors.WithMessage(err, "NewAVP SessionID failed") - } - _, err = m.NewAVP(avp.OriginHost, avp.Mbit, 0, c.cfg.OriginHost) - if err != nil { - return false, errors.WithMessage(err, "NewAVP OriginHost failed") + avps := []AVPMeta{ + {code: avp.SessionID, flag: avp.Mbit, vendor: 0, value: datatype.UTF8String(sid)}, + {code: avp.OriginHost, flag: avp.Mbit, vendor: 0, value: c.cfg.OriginHost}, + {code: avp.OriginRealm, flag: avp.Mbit, vendor: 0, value: c.cfg.OriginRealm}, } - _, err = m.NewAVP(avp.OriginRealm, avp.Mbit, 0, c.cfg.OriginRealm) - if err != nil { - return false, errors.WithMessage(err, "NewAVP OriginRealm failed") + for _, avp := range avps { + _, err = m.NewAVP(avp.code, avp.flag, avp.vendor, avp.value) + if err != nil { + return false, errors.WithMessage(err, "NewAVP failed") + } } if options.ProxiableFlag { m.Header.CommandFlags |= diam.ProxiableFlag @@ -365,17 +364,16 @@ func (c *K6DiameterClient) SendULR(options ConnectionOptions) (bool, error) { sid = c.generateSessionID() } m := diam.NewRequest(diam.UpdateLocation, diam.TGPP_S6A_APP_ID, dict.Default) - _, err = m.NewAVP(avp.SessionID, avp.Mbit, 0, datatype.UTF8String(sid)) - if err != nil { - return false, errors.WithMessage(err, "NewAVP SessionID failed") - } - _, err = m.NewAVP(avp.OriginHost, avp.Mbit, 0, c.cfg.OriginHost) - if err != nil { - return false, errors.WithMessage(err, "NewAVP OriginHost failed") + avps := []AVPMeta{ + {code: avp.SessionID, flag: avp.Mbit, vendor: 0, value: datatype.UTF8String(sid)}, + {code: avp.OriginHost, flag: avp.Mbit, vendor: 0, value: c.cfg.OriginHost}, + {code: avp.OriginRealm, flag: avp.Mbit, vendor: 0, value: c.cfg.OriginRealm}, } - _, err = m.NewAVP(avp.OriginRealm, avp.Mbit, 0, c.cfg.OriginRealm) - if err != nil { - return false, errors.WithMessage(err, "NewAVP OriginRealm failed") + for _, avp := range avps { + _, err = m.NewAVP(avp.code, avp.flag, avp.vendor, avp.value) + if err != nil { + return false, errors.WithMessage(err, "NewAVP failed") + } } if options.ProxiableFlag { m.Header.CommandFlags |= diam.ProxiableFlag