Skip to content

refactor(fn): factor RunFunction into focused helpers - #44

Merged
jbw976 merged 2 commits into
crossplane-contrib:mainfrom
jbw976:clean-go
Apr 6, 2026
Merged

refactor(fn): factor RunFunction into focused helpers#44
jbw976 merged 2 commits into
crossplane-contrib:mainfrom
jbw976:clean-go

Conversation

@jbw976

@jbw976 jbw976 commented Apr 6, 2026

Copy link
Copy Markdown
Member

Description of your changes

This PR is an attempt to clean up the fn.go part of the code base a bit, as a result of running some of @negz golang related skills.

The main RunFunction mixed orchestration with inline implementations, making the processing flow hard to follow and preventing independent testing of self-contained operations.

This PR restructures the code we own (fn.go, fn_test.go, main.go) without changing any behavior:

  • Add a NewFunction constructor that defaults nil loggers to no-op, preventing nil-pointer panics in tests
  • Convert stateless methods to package-level functions (requireSchemas, externalCollectionSelector, groupObservedByNodeID)
  • Extract collectGVKs, buildDesiredComposed, and buildDesiredXRStatus so RunFunction reads as a linear sequence of helper calls and error handling
  • Order helpers in the file to match the call sequence in RunFunction
  • Align error wrapping in structToSpecSchema with the rest of the file (errors.Wrap / "cannot X" instead of fmt.Errorf / "failed to X")

RunFunction no longer needs a gocognit suppression.

I have:

jbw976 added 2 commits April 6, 2026 10:45
Several helpers on the Function struct don't use any receiver state,
making them harder to test in isolation than they need to be. This
commit converts them from methods to package-level functions.

It also adds a NewFunction constructor that defaults a nil logger to
a no-op implementation, preventing nil-pointer panics if a caller
forgets to set the logger (e.g. in tests).

Finally, structToSpecSchema's error wrapping is aligned with the rest
of fn.go: fmt.Errorf with "failed to X" becomes errors.Wrap with
"cannot X".

Signed-off-by: Jared Watts <jbw976@gmail.com>
RunFunction mixed orchestration with inline implementation for
several self-contained operations. This made the overall processing
flow harder to follow and prevented testing those operations
independently.

This commit pulls the inline blocks into package-level functions so
RunFunction reads as a linear sequence of helper calls and error
handling. The helpers are ordered in the file to match the call
sequence, so the file reads top-to-bottom like the processing flow.
RunFunction no longer needs a gocognit suppression.

Signed-off-by: Jared Watts <jbw976@gmail.com>
@jbw976
jbw976 merged commit a15670b into crossplane-contrib:main Apr 6, 2026
6 checks passed
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.

1 participant