Remove redundant constructor, h/t sergio

This commit is contained in:
Pim van Pelt
2025-11-15 22:41:57 +01:00
parent 27c7a5bcae
commit a0d5c61643
12 changed files with 48 additions and 44 deletions

9
debian/changelog vendored
View File

@@ -1,3 +1,12 @@
govpp-snmp-agentx (1.1.7-1) bookworm; urgency=medium
* Refactor VPPClient constructor to use idiomatic Go patterns
* Remove redundant NewVPPClient() constructor function
* Update all code to use direct struct initialization (&VPPClient{})
* Improve code maintainability and follow Go best practices
-- Pim van Pelt <pim@ipng.ch> Fri, 15 Nov 2024 00:00:00 +0000
govpp-snmp-agentx (1.1.6-1) bookworm; urgency=medium govpp-snmp-agentx (1.1.6-1) bookworm; urgency=medium
* Replace forked go-agentx dependency with upstream github.com/posteo/go-agentx * Replace forked go-agentx dependency with upstream github.com/posteo/go-agentx

View File

@@ -16,7 +16,7 @@ import (
"govpp-snmp-agentx/vpp" "govpp-snmp-agentx/vpp"
) )
const Version = "1.1.6-1" const Version = "1.1.7-1"
func main() { func main() {
debug := flag.Bool("debug", false, "Enable debug logging") debug := flag.Bool("debug", false, "Enable debug logging")
@@ -46,7 +46,7 @@ func main() {
} }
// Create VPP client and managers // Create VPP client and managers
vppClient := vpp.NewVPPClient() vppClient := &vpp.VPPClient{}
interfaceManager := vpp.NewInterfaceManager(vppClient) interfaceManager := vpp.NewInterfaceManager(vppClient)
statsManager := vpp.NewStatsManager(vppClient) statsManager := vpp.NewStatsManager(vppClient)

View File

@@ -29,11 +29,6 @@ type VPPClient struct {
connected bool connected bool
} }
// NewVPPClient creates a new VPP client instance
func NewVPPClient() *VPPClient {
return &VPPClient{}
}
// Connect establishes connections to both VPP API and Stats sockets // Connect establishes connections to both VPP API and Stats sockets
func (c *VPPClient) Connect() error { func (c *VPPClient) Connect() error {
logger.Debugf("Connecting to VPP (API: %s, Stats: %s)", *ApiAddr, *StatsAddr) logger.Debugf("Connecting to VPP (API: %s, Stats: %s)", *ApiAddr, *StatsAddr)

View File

@@ -10,7 +10,7 @@ import (
) )
func TestNewInterfaceManager(t *testing.T) { func TestNewInterfaceManager(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
if manager == nil { if manager == nil {
@@ -36,7 +36,7 @@ func TestNewInterfaceManager(t *testing.T) {
} }
func TestInterfaceManagerSetEventCallback(t *testing.T) { func TestInterfaceManagerSetEventCallback(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
var callbackCalled bool var callbackCalled bool
@@ -82,7 +82,7 @@ func TestInterfaceManagerSetEventCallback(t *testing.T) {
} }
func TestInterfaceManagerGetAllInterfaceDetailsWithoutConnection(t *testing.T) { func TestInterfaceManagerGetAllInterfaceDetailsWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
_, err := manager.GetAllInterfaceDetails() _, err := manager.GetAllInterfaceDetails()
@@ -101,7 +101,7 @@ func TestInterfaceManagerGetAllInterfaceDetailsWithoutConnection(t *testing.T) {
} }
func TestInterfaceManagerStartEventWatcherWithoutConnection(t *testing.T) { func TestInterfaceManagerStartEventWatcherWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
err := manager.StartEventWatcher() err := manager.StartEventWatcher()
@@ -120,7 +120,7 @@ func TestInterfaceManagerStartEventWatcherWithoutConnection(t *testing.T) {
} }
func TestInterfaceManagerHandleInterfaceEventWithoutCallback(t *testing.T) { func TestInterfaceManagerHandleInterfaceEventWithoutCallback(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
// Should not panic when callback is nil // Should not panic when callback is nil
@@ -128,7 +128,7 @@ func TestInterfaceManagerHandleInterfaceEventWithoutCallback(t *testing.T) {
} }
func TestInterfaceManagerInitializeEventWatchingWithoutConnection(t *testing.T) { func TestInterfaceManagerInitializeEventWatchingWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
err := manager.InitializeEventWatching() err := manager.InitializeEventWatching()
@@ -220,7 +220,7 @@ func TestInterfaceEventCallback(t *testing.T) {
} }
func TestInterfaceManagerStartStopEventMonitoring(t *testing.T) { func TestInterfaceManagerStartStopEventMonitoring(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
if manager.running { if manager.running {
@@ -248,7 +248,7 @@ func TestInterfaceManagerStartStopEventMonitoring(t *testing.T) {
} }
func TestInterfaceManagerEventMonitoringWithConnectionChanges(t *testing.T) { func TestInterfaceManagerEventMonitoringWithConnectionChanges(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewInterfaceManager(client) manager := NewInterfaceManager(client)
// Set a callback to track calls // Set a callback to track calls

View File

@@ -10,7 +10,7 @@ import (
) )
func TestNewStatsManager(t *testing.T) { func TestNewStatsManager(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
if manager == nil { if manager == nil {
@@ -36,7 +36,7 @@ func TestNewStatsManager(t *testing.T) {
} }
func TestStatsManagerSetStatsCallback(t *testing.T) { func TestStatsManagerSetStatsCallback(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
var callbackCalled bool var callbackCalled bool
@@ -85,7 +85,7 @@ func TestStatsManagerSetStatsCallback(t *testing.T) {
} }
func TestStatsManagerSetPeriod(t *testing.T) { func TestStatsManagerSetPeriod(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
newPeriod := 5 * time.Second newPeriod := 5 * time.Second
@@ -97,7 +97,7 @@ func TestStatsManagerSetPeriod(t *testing.T) {
} }
func TestStatsManagerStartStopStatsRoutine(t *testing.T) { func TestStatsManagerStartStopStatsRoutine(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
if manager.running { if manager.running {
@@ -125,7 +125,7 @@ func TestStatsManagerStartStopStatsRoutine(t *testing.T) {
} }
func TestStatsManagerGetInterfaceStatsWithoutConnection(t *testing.T) { func TestStatsManagerGetInterfaceStatsWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
_, err := manager.GetInterfaceStats() _, err := manager.GetInterfaceStats()
@@ -206,7 +206,7 @@ func TestStatsCallback(t *testing.T) {
} }
func TestStatsManagerQueryAndReportStatsWithoutConnection(t *testing.T) { func TestStatsManagerQueryAndReportStatsWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
// Should return false when not connected // Should return false when not connected
@@ -216,7 +216,7 @@ func TestStatsManagerQueryAndReportStatsWithoutConnection(t *testing.T) {
} }
func TestStatsManagerWithShortPeriod(t *testing.T) { func TestStatsManagerWithShortPeriod(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
manager := NewStatsManager(client) manager := NewStatsManager(client)
// Set a very short period for testing // Set a very short period for testing

View File

@@ -7,7 +7,7 @@ import (
) )
func TestNewVPPClient(t *testing.T) { func TestNewVPPClient(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
if client == nil { if client == nil {
t.Fatal("NewVPPClient() returned nil") t.Fatal("NewVPPClient() returned nil")
@@ -27,7 +27,7 @@ func TestNewVPPClient(t *testing.T) {
} }
func TestVPPClientDisconnect(t *testing.T) { func TestVPPClientDisconnect(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
// Should be safe to call disconnect on unconnected client // Should be safe to call disconnect on unconnected client
client.Disconnect() client.Disconnect()
@@ -38,7 +38,7 @@ func TestVPPClientDisconnect(t *testing.T) {
} }
func TestVPPClientNewAPIChannelWithoutConnection(t *testing.T) { func TestVPPClientNewAPIChannelWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
_, err := client.NewAPIChannel() _, err := client.NewAPIChannel()
if err == nil { if err == nil {
@@ -56,7 +56,7 @@ func TestVPPClientNewAPIChannelWithoutConnection(t *testing.T) {
} }
func TestVPPClientCheckLivenessWithoutConnection(t *testing.T) { func TestVPPClientCheckLivenessWithoutConnection(t *testing.T) {
client := NewVPPClient() client := &VPPClient{}
if client.CheckLiveness() { if client.CheckLiveness() {
t.Error("CheckLiveness() should return false when not connected") t.Error("CheckLiveness() should return false when not connected")
@@ -86,7 +86,7 @@ func TestVPPClientConnectWithInvalidPaths(t *testing.T) {
*StatsAddr = origStatsAddr *StatsAddr = origStatsAddr
}() }()
client := NewVPPClient() client := &VPPClient{}
err := client.Connect() err := client.Connect()
if err == nil { if err == nil {