Fix golangci-lint findings and improve code quality

Move example files to separate packages to avoid main redeclaration. Fix cyclomatic complexity and variable shadowing. Address errcheck and wsl linting issues. Update tests to handle capabilities and fix panics. Apply consistent formatting with gofmt.
This commit is contained in:
Tobias Gesellchen
2026-01-11 00:39:58 +01:00
parent 1f47c763dc
commit 369ebc42fe
11 changed files with 388 additions and 161 deletions
+20
View File
@@ -129,6 +129,7 @@ func (adsp *AudioDSPControls) GetSupportedAudioModes() []string {
if adsp.SupportedAudioModes == "" {
return []string{}
}
return strings.Split(adsp.SupportedAudioModes, "|")
}
@@ -140,12 +141,14 @@ func (adsp *AudioDSPControls) IsAudioModeSupported(mode string) bool {
return true
}
}
return false
}
// String returns a human-readable string representation of DSP controls
func (adsp *AudioDSPControls) String() string {
supportedModes := strings.Join(adsp.GetSupportedAudioModes(), ", ")
return fmt.Sprintf("Audio Mode: %s, Video Sync Delay: %d ms, Supported Modes: [%s]",
adsp.AudioMode, adsp.VideoSyncAudioDelay, supportedModes)
}
@@ -171,6 +174,7 @@ func (bc *BassControlSetting) ValidateBass(value int) error {
if value < bc.MinValue || value > bc.MaxValue {
return fmt.Errorf("bass value %d is outside valid range [%d, %d]", value, bc.MinValue, bc.MaxValue)
}
return nil
}
@@ -179,9 +183,11 @@ func (bc *BassControlSetting) ClampValue(value int) int {
if value < bc.MinValue {
return bc.MinValue
}
if value > bc.MaxValue {
return bc.MaxValue
}
return value
}
@@ -190,6 +196,7 @@ func (tc *TrebleControlSetting) ValidateTreble(value int) error {
if value < tc.MinValue || value > tc.MaxValue {
return fmt.Errorf("treble value %d is outside valid range [%d, %d]", value, tc.MinValue, tc.MaxValue)
}
return nil
}
@@ -198,9 +205,11 @@ func (tc *TrebleControlSetting) ClampValue(value int) int {
if value < tc.MinValue {
return tc.MinValue
}
if value > tc.MaxValue {
return tc.MaxValue
}
return value
}
@@ -249,6 +258,7 @@ func (fc *FrontCenterLevelSetting) ValidateLevel(value int) error {
if value < fc.MinValue || value > fc.MaxValue {
return fmt.Errorf("front-center speaker level %d is outside valid range [%d, %d]", value, fc.MinValue, fc.MaxValue)
}
return nil
}
@@ -257,9 +267,11 @@ func (fc *FrontCenterLevelSetting) ClampLevel(value int) int {
if value < fc.MinValue {
return fc.MinValue
}
if value > fc.MaxValue {
return fc.MaxValue
}
return value
}
@@ -268,6 +280,7 @@ func (rs *RearSurroundLevelSetting) ValidateLevel(value int) error {
if value < rs.MinValue || value > rs.MaxValue {
return fmt.Errorf("rear-surround speaker level %d is outside valid range [%d, %d]", value, rs.MinValue, rs.MaxValue)
}
return nil
}
@@ -276,9 +289,11 @@ func (rs *RearSurroundLevelSetting) ClampLevel(value int) int {
if value < rs.MinValue {
return rs.MinValue
}
if value > rs.MaxValue {
return rs.MaxValue
}
return value
}
@@ -337,15 +352,19 @@ func (ac *AudioCapabilities) HasAdvancedAudioControls() bool {
// GetAvailableControls returns a list of available advanced audio controls
func (ac *AudioCapabilities) GetAvailableControls() []string {
var controls []string
if ac.DSPControls {
controls = append(controls, "DSP Controls")
}
if ac.ProductToneControls {
controls = append(controls, "Tone Controls")
}
if ac.ProductLevelControls {
controls = append(controls, "Level Controls")
}
return controls
}
@@ -356,5 +375,6 @@ func (ac *AudioCapabilities) String() string {
}
controls := ac.GetAvailableControls()
return fmt.Sprintf("Available controls: %s", strings.Join(controls, ", "))
}
+3
View File
@@ -160,6 +160,7 @@ func TestAudioDSPControlsRequest_Validate(t *testing.T) {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", tt.errorMsg, err.Error())
}
@@ -330,6 +331,7 @@ func TestAudioProductToneControlsRequest_Validate(t *testing.T) {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", tt.errorMsg, err.Error())
}
@@ -455,6 +457,7 @@ func TestAudioProductLevelControlsRequest_Validate(t *testing.T) {
t.Errorf("Expected error but got none")
return
}
if !strings.Contains(err.Error(), tt.errorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", tt.errorMsg, err.Error())
}
+2
View File
@@ -452,6 +452,7 @@ func (zsr *ZoneSlaveRequest) GetSlaveDeviceID() string {
if len(zsr.Members) > 0 {
return zsr.Members[0].DeviceID
}
return ""
}
@@ -460,6 +461,7 @@ func (zsr *ZoneSlaveRequest) GetSlaveIP() string {
if len(zsr.Members) > 0 {
return zsr.Members[0].IP
}
return ""
}
+8
View File
@@ -151,6 +151,7 @@ func TestZoneSlaveRequest_HelperMethods(t *testing.T) {
request.AddSlave("SLAVE456", "192.168.1.101")
deviceID := request.GetSlaveDeviceID()
expected := "SLAVE456"
if deviceID != expected {
t.Errorf("Expected device ID '%s', got '%s'", expected, deviceID)
@@ -171,6 +172,7 @@ func TestZoneSlaveRequest_HelperMethods(t *testing.T) {
request.AddSlave("SLAVE456", "192.168.1.101")
ip := request.GetSlaveIP()
expected := "192.168.1.101"
if ip != expected {
t.Errorf("Expected IP '%s', got '%s'", expected, ip)
@@ -208,6 +210,7 @@ func TestZoneSlaveRequest_String(t *testing.T) {
setup: func() *ZoneSlaveRequest {
req := NewZoneSlaveRequest("MASTER123")
req.AddSlave("SLAVE456", "192.168.1.101")
return req
},
expected: "Zone slave operation: master=MASTER123, slave=SLAVE456 (192.168.1.101)",
@@ -217,6 +220,7 @@ func TestZoneSlaveRequest_String(t *testing.T) {
setup: func() *ZoneSlaveRequest {
req := NewZoneSlaveRequest("MASTER123")
req.AddSlave("SLAVE456", "")
return req
},
expected: "Zone slave operation: master=MASTER123, slave=SLAVE456",
@@ -330,12 +334,14 @@ func TestZoneSlaveRequest_XMLUnmarshaling(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var request ZoneSlaveRequest
err := xml.Unmarshal([]byte(tt.xmlData), &request)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
@@ -381,6 +387,7 @@ func TestZoneSlaveEntry_XMLMarshaling(t *testing.T) {
}
xmlStr := string(xmlData)
expected := `<member ipaddress="192.168.1.101">SLAVE456</member>`
if xmlStr != expected {
t.Errorf("Expected XML '%s', got '%s'", expected, xmlStr)
@@ -399,6 +406,7 @@ func TestZoneSlaveEntry_XMLMarshaling(t *testing.T) {
}
xmlStr := string(xmlData)
expected := `<member>SLAVE456</member>`
if xmlStr != expected {
t.Errorf("Expected XML '%s', got '%s'", expected, xmlStr)