Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: refactor dynamic parameters into dedicated package #18420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jun 20, 2025

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jun 17, 2025

Refactor dynamic parameters into dedicated package

This PR extracts dynamic parameter rendering logic from coderd/parameters.go into a new coderd/dynamicparameters package. Partly for organization and maintainability, but primarily to be reused in wsbuilder to be leveraged as validation.

Key changes:

  • Created new dynamicparameters package to handle dynamic parameter preview
  • Added support for both static and dynamic parameter rendering through unified interface
    • Older versions do not have the db state required for dynamic params. So it loads static params, and serves them through the same interface.
  • Added test utilities for testing dynamic parameters
    • Added more comprehensive tests for dynamic params.

The refactoring maintains backward compatibility while creating a cleaner separation of concerns for parameter handling logic. The next step is to use this logic in wsbuilder for validation purposes.

@Emyrk Emyrk changed the title Stevenmasley/dynamic param pkg chore: refactor dynamic parameters into dedicated package Jun 18, 2025
@Emyrk Emyrk marked this pull request as ready for review June 18, 2025 18:42
@Emyrk Emyrk force-pushed the stevenmasley/dynamic_param_pkg branch from 37bfb5c to 758cd26 Compare June 18, 2025 19:09
@Emyrk Emyrk requested a review from SasSwart June 18, 2025 19:20

// TestDynamicParameterTemplate uses a template with some dynamic elements, and
// tests the parameters, values, etc are all as expected.
func TestDynamicParameterTemplate(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This test is actually kinda dope. I like the resulting syntax

@@ -81,292 +68,49 @@ func (api *API) templateVersionDynamicParametersWebsocket(rw http.ResponseWriter
})(rw, r)
}

//nolint:revive // listen is a control flag
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is a control flag acceptable or desired here if the linter discourages it? Perhaps we should include that justification in the comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added 👍

Copy link
Contributor

@SasSwart SasSwart Jun 20, 2025

Choose a reason for hiding this comment

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

I understood as much as you've described in the new comment. My question is why this needs to be a "control flag" in this way instead of being extracted from the context inside this endpoint. They're functionally equivalent as far as I can tell. I'm not saying one approach is better.

I'm asking because I'm sure the linting rule was added for a reason and //nolint can be a smell unless it's well justified.

Copy link
Member Author

@Emyrk Emyrk Jun 20, 2025

Choose a reason for hiding this comment

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

As I read it, you are right. I could refactor this to remove the control flag now that the dynamicparameters package exists.

I'm going to keep it for now, and I'm going to make an issue to revisit this.
#18478

It is on the parameters board, so I can fix this up in my next maintenance cycle 👍

}

var protoOptions []*sdkproto.RichParameterOption
_ = json.Unmarshal(it.Options, &protoOptions) // Not going to make this fatal
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're going to comment this, can we document why we ignore this error, so that future contributors know when it may or may not be useful to reassess this? Is it simply because we're pretty sure this will be valid json since it comes from the DB? I'd like to avoid Chesterton's Fence.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I can throw this error on the parameter now as a diagnostic. I'm going to toss the error in there. 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Added it as a diagnostic on the param 👍

db612b3

@Emyrk Emyrk requested a review from Copilot June 20, 2025 11:45
Copy link
Contributor

@Copilot 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

This PR refactors dynamic parameter rendering out of coderd/parameters.go into a new coderd/dynamicparameters package, unifies static and dynamic parameter handling under a single interface, and adds test utilities and fixtures for exercising dynamic parameters.

  • Extract dynamic parameter logic into dynamicparameters package with Loader/Renderer
  • Update API handlers in coderd/parameters.go to use the new package
  • Add comprehensive tests and helpers for dynamic parameter rendering, plus a Terraform test fixture

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
enterprise/coderd/testdata/parameters/dynamic/main.tf New Terraform fixture demonstrating dynamic parameters
enterprise/coderd/parameters_test.go Tests updated to use SynchronousStream helper
enterprise/coderd/dynamicparameters_test.go End-to-end tests for dynamic parameters via WebSocket/API
coderd/util/slice/slice.go New Convert utility to map slices
coderd/parameters_test.go Refactored setup to use unified DynamicParameterTemplate
coderd/parameters.go Switched to dynamicparameters.Loader for rendering logic
coderd/dynamicparameters/static.go Static renderer implementation for legacy parameter flow
coderd/dynamicparameters/render.go Dynamic renderer and Renderer interface implementation
coderd/coderdtest/stream.go Added SynchronousStream test helper
coderd/coderdtest/dynamicparameters.go High-level helper for template/dynamic parameter tests

@Emyrk Emyrk requested a review from SasSwart June 20, 2025 15:19
Copy link
Member Author

Emyrk commented Jun 20, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
Contributor

@SasSwart SasSwart left a comment

Choose a reason for hiding this comment

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

I'm new to the dynamic parameter space, but I'm pretty confident I understand how this all fits together.

@Emyrk Emyrk merged commit 9b5d499 into main Jun 20, 2025
35 checks passed
@Emyrk Emyrk deleted the stevenmasley/dynamic_param_pkg branch June 20, 2025 18:00
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants