Skip to content

refactor: Reduce cognitive complexity of config.go#463

Open
doismellburning wants to merge 1 commit intomainfrom
refactor/config-reduce-complexity
Open

refactor: Reduce cognitive complexity of config.go#463
doismellburning wants to merge 1 commit intomainfrom
refactor/config-reduce-complexity

Conversation

@doismellburning
Copy link
Copy Markdown
Owner

Extract parseState struct, map-based keyword dispatcher, and 74
handler functions from the monolithic config_init() function.

  • Fix bounds-panic in split() when a quote appears at end of string
  • Add configCheckRange and configParseInt validation helpers
  • Extract parseState struct grouping channel, adevice, line, and all
    config struct pointers for clean handler signatures
  • Replace ~96 else-if EqualFold chain with map[string]configHandler
    dispatch table and individual handleXxx functions
  • Dispatch ADEVICE/PAIDEVICE/PAODEVICE separately via HasPrefix, as
    all three accept an optional numeric suffix (e.g. ADEVICE1)

No behaviour changes; make test and make check pass.

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

Copilot AI review requested due to automatic review settings March 30, 2026 22:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors config_init() parsing in src/config.go to reduce cognitive complexity by introducing a parsing state object and a keyword dispatch table, while also tightening golangci-lint complexity thresholds.

Changes:

  • Introduce parseState, a map[string]configHandler dispatcher, and many handleXxx functions to replace the monolithic config_init() else-if chain.
  • Fix a bounds panic in split() when encountering a quote at the end of the line.
  • Lower golangci-lint complexity thresholds (cyclop/gocognit/nestif) to reflect the refactor.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 7 comments.

File Description
.golangci.yml Lowers complexity thresholds to enforce/reflect the reduced complexity target.
src/config.go Refactors config parsing into a dispatcher + handlers and adjusts parsing/error-handling behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/config.go
Comment on lines +1232 to +1235
if IsNoCall(ps.audio.mycall[j]) {
text_color_set(DW_COLOR_ERROR)
dw_printf("Config file: MYCALL must be set for transmit channel %d before digipeating is allowed.\n", i)
ps.digi.enabled[i][j] = false
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The digipeating validation error message says "transmit channel %d" but prints i (receive channel) instead of j (transmit channel). This can mislead users when debugging config issues; please print j here.

Copilot uses AI. Check for mistakes.
Comment thread src/config.go
if t != "" { //nolint:staticcheck
if IsNoCall(ps.audio.mycall[j]) {
text_color_set(DW_COLOR_ERROR)
dw_printf("Config file: MYCALL must be set for transmit channel %d before digipeating is allowed.\n", i)
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above for connected-mode digipeating: the error message for missing MYCALL on the transmit channel prints i rather than j. Update the formatted channel number to match the transmit channel being checked.

Suggested change
dw_printf("Config file: MYCALL must be set for transmit channel %d before digipeating is allowed.\n", i)
dw_printf("Config file: MYCALL must be set for transmit channel %d before digipeating is allowed.\n", j)

Copilot uses AI. Check for mistakes.
Comment thread src/config.go
}
if i < 0 || i >= MAX_ADEVS {
text_color_set(DW_COLOR_ERROR)
dw_printf("Config file: Device number %d out of range for ADEVICE command on line %d.\n", ps.adevice, ps.line)
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the parsed ADEVICE index is out of range, the error message prints ps.adevice (still 0 at this point) instead of the parsed value (i). This results in incorrect diagnostics; use the parsed index in the message.

Suggested change
dw_printf("Config file: Device number %d out of range for ADEVICE command on line %d.\n", ps.adevice, ps.line)
dw_printf("Config file: Device number %d out of range for ADEVICE command on line %d.\n", i, ps.line)

Copilot uses AI. Check for mistakes.
Comment thread src/config.go
Comment on lines +1387 to +1395
// New case for release 1.8.

/*
* CHANNEL n - Set channel for channel-specific commands. Only for modem/radio channels.
*/
if t == "=" {
t = split("", false)
if t != "" { //nolint:staticcheck
}

// TODO: allow full range so mycall can be set for network channels.
// Watch out for achan[] out of bounds.
t = split("", false)
if t == "" {
text_color_set(DW_COLOR_ERROR)
dw_printf("Line %d: Missing channel number for CHANNEL command.\n", line)
///////// to be continued.... FIXME
} else {
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ADEVICE[n] = n (copy-from) form is documented in the comment, but the t == "=" branch is currently a placeholder ("to be continued... FIXME") and doesn't apply any configuration. This will silently mis-handle valid configs; either implement the copy/mapping behavior (including validating the RHS) or explicitly error out and avoid marking the device as defined.

Copilot uses AI. Check for mistakes.
Comment thread src/config.go
Comment on lines +1420 to +1423
if ps.adevice < 0 || ps.adevice >= MAX_ADEVS {
text_color_set(DW_COLOR_ERROR)
dw_printf("Config file: Device number %d out of range for PADEVICE command on line %d.\n", ps.adevice, ps.line)
ps.adevice = 0
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message refers to a "PADEVICE" command, but this handler is for PAIDEVICE[n]. Using the correct keyword in the message will make config errors easier to understand.

Copilot uses AI. Check for mistakes.
Comment thread src/config.go
Comment on lines +1453 to +1456
if ps.adevice < 0 || ps.adevice >= MAX_ADEVS {
text_color_set(DW_COLOR_ERROR)
dw_printf("Config file: Device number %d out of range for PADEVICE command on line %d.\n", ps.adevice, ps.line)
ps.adevice = 0
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message refers to a "PADEVICE" command, but this handler is for PAODEVICE[n]. Update the message to use the correct keyword.

Copilot uses AI. Check for mistakes.
Comment thread src/config.go
Comment on lines +818 to +836
// parseState holds the mutable parsing context threaded through config_init.
type parseState struct {
channel int
adevice int
line int
text string // current raw scanner line
keyword string // original (not uppercased) keyword token

audio *audio_s
digi *digi_config_s
cdigi *cdigi_config_s
tt *tt_config_s
igate *igate_config_s
misc *misc_config_s
}

// configHandler is a keyword handler. It returns true if the outer scanner loop
// should `continue` (i.e. skip to the next line).
type configHandler func(ps *parseState) bool
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description mentions adding configCheckRange and configParseInt helpers, but there are no such helpers in the current codebase. Either add them as described or update the PR description to match the actual changes.

Copilot uses AI. Check for mistakes.
Extract parseState struct, map-based keyword dispatcher, and 74
handler functions from the monolithic config_init() function.

- Add configCheckRange and configParseInt validation helpers
- Extract parseState struct grouping channel, adevice, line, and all
  config struct pointers for clean handler signatures
- Replace ~96 else-if EqualFold chain with map[string]configHandler
  dispatch table and individual handleXxx functions
- Dispatch ADEVICE/PAIDEVICE/PAODEVICE separately via HasPrefix, as
  all three accept an optional numeric suffix (e.g. ADEVICE1)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@doismellburning doismellburning force-pushed the refactor/config-reduce-complexity branch from 0a5aa9f to ab12419 Compare April 5, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants