Part of the lite `codersdk.Workspace` work ([GRU-82](https://linear.app/codercom/issue/GRU-82), [RFC](https://app.notion.com/p/coderhq/Lite-Workspace-API-Requests-3aad579be5928060b5e7cc65c539717f)). Stacked on #29539, which wired `include_related` into `GET /api/v2/workspaces/{workspace}`. Adds `include_related` support to the `codersdk` client, expressed with the typed selection tree so only supported selections are representable: - Moves the `wsrelated` package from `coderd/` to `codersdk/` so both the server and the client can share `wsrelated.Config`. `coderd` already depends on `codersdk`, so this only changes import paths on the server side. - Adds `Config.QueryParam`, which encodes a selection into the `include_related` value: the dotted path of each selected node that has no selected descendants. Selecting a node implies its ancestors, so `Parse` round-trips the result back into an equal `Config` (covered by a round-trip test). - `WorkspaceOptions` gains `IncludeRelated *wsrelated.Config`. A nil value omits the parameter (server returns everything, unchanged); a non-nil value is encoded via `QueryParam`, and a zero `Config` loads only the workspace. - `Client.Workspace` now accepts optional `WorkspaceOptions`, backward compatible with existing `client.Workspace(ctx, id)` calls. ### Testing - `codersdk` internal test asserts `WorkspaceOptions.asRequestOption` sets `include_related` from the selection, following the existing `WorkspaceFilter` test pattern rather than spinning up a server. - `wsrelated` tests cover `QueryParam` output and a `Parse`/`QueryParam` round trip. `make gen` regenerated the `WorkspaceOptions` TypeScript type. Because the wire representation of `include_related` is a comma-separated string, `scripts/apitypings` maps `wsrelated.Config` to `string` in TypeScript, so `include_related?: string` rather than a nested object the frontend cannot serialize. `api.getWorkspace` already forwards `WorkspaceOptions` as query params, and a frontend test in `api.test.ts` confirms `include_related` is sent through. --- *Generated by Coder Agents on behalf of @spikecurtis.*
180 lines
5.1 KiB
Go
180 lines
5.1 KiB
Go
package wsrelated
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// schema holds the root nodes of the include_related hierarchy. It is built once
|
|
// in init rather than on every Parse call, since it is immutable.
|
|
var schema []relatedNode
|
|
|
|
func init() {
|
|
schema = buildSchema()
|
|
}
|
|
|
|
// relatedNode describes one node in the related-data hierarchy for the purpose
|
|
// of parsing the include_related query parameter. selectInto marks this node as
|
|
// selected in the target tree; via the ensure* helpers it also creates the
|
|
// node's ancestors, so selecting a node implicitly selects its ancestors.
|
|
type relatedNode struct {
|
|
name string
|
|
selectInto func(*Config)
|
|
children []relatedNode
|
|
}
|
|
|
|
// buildSchema constructs the include_related hierarchy. Each node's name matches
|
|
// a segment in a dotted path, and its selectInto closure mirrors the
|
|
// corresponding node in Config.
|
|
func buildSchema() []relatedNode {
|
|
return []relatedNode{
|
|
{
|
|
name: "template",
|
|
selectInto: func(r *Config) { r.Template = true },
|
|
},
|
|
{
|
|
name: "latest_build",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild() },
|
|
children: []relatedNode{
|
|
{
|
|
name: "job",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureJob() },
|
|
children: []relatedNode{
|
|
{
|
|
name: "queue_position",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureJob().QueuePosition = true },
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "resources",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources() },
|
|
children: []relatedNode{
|
|
{
|
|
name: "metadata",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources().Metadata = true },
|
|
},
|
|
{
|
|
name: "agents",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources().ensureAgents() },
|
|
children: []relatedNode{
|
|
{
|
|
name: "apps",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources().ensureAgents().ensureApps() },
|
|
children: []relatedNode{
|
|
{
|
|
name: "statuses",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources().ensureAgents().ensureApps().Statuses = true },
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "scripts",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources().ensureAgents().Scripts = true },
|
|
},
|
|
{
|
|
name: "log_sources",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().ensureResources().ensureAgents().LogSources = true },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "template_version",
|
|
selectInto: func(r *Config) { r.ensureLatestBuild().TemplateVersion = true },
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Parse parses the include_related query parameter: a comma-separated list of
|
|
// dotted hierarchy paths, e.g. "template,latest_build.resources.agents.*". Each
|
|
// path may end in a single wildcard "*" that selects the node and all of its
|
|
// descendants; "*" on its own selects everything. Selecting a node implicitly
|
|
// selects its ancestors.
|
|
//
|
|
// Surrounding whitespace and empty entries are ignored. An empty string selects
|
|
// nothing but the workspace itself; callers that want the RFC's "absent
|
|
// parameter means everything" behavior should use All when the parameter is not
|
|
// present. An unknown path or a wildcard that is not the final segment returns
|
|
// an error.
|
|
func Parse(includeRelated string) (Config, error) {
|
|
var result Config
|
|
|
|
tokens := strings.Split(includeRelated, ",")
|
|
for i, raw := range tokens {
|
|
token := strings.TrimSpace(raw)
|
|
if token == "" {
|
|
// Reject empty entries such as "a,,b" or ", ,", but allow a single
|
|
// optional trailing comma (an empty final element).
|
|
if i == len(tokens)-1 {
|
|
continue
|
|
}
|
|
return Config{}, xerrors.New("empty include_related path")
|
|
}
|
|
|
|
if token == "*" {
|
|
for _, n := range schema {
|
|
selectSubtree(n, &result)
|
|
}
|
|
continue
|
|
}
|
|
|
|
path := token
|
|
wildcard := false
|
|
if strings.HasSuffix(path, ".*") {
|
|
wildcard = true
|
|
path = strings.TrimSuffix(path, ".*")
|
|
}
|
|
|
|
segments := strings.Split(path, ".")
|
|
for _, s := range segments {
|
|
if s == "" || s == "*" {
|
|
return Config{}, xerrors.Errorf("invalid include_related path %q", token)
|
|
}
|
|
}
|
|
|
|
node, ok := findRelatedNode(schema, segments)
|
|
if !ok {
|
|
return Config{}, xerrors.Errorf("unknown include_related path %q", token)
|
|
}
|
|
|
|
if wildcard {
|
|
selectSubtree(node, &result)
|
|
} else {
|
|
node.selectInto(&result)
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// findRelatedNode walks the schema following the dotted path segments, returning
|
|
// the matching node.
|
|
func findRelatedNode(nodes []relatedNode, segments []string) (relatedNode, bool) {
|
|
if len(segments) == 0 {
|
|
return relatedNode{}, false
|
|
}
|
|
for _, n := range nodes {
|
|
if n.name != segments[0] {
|
|
continue
|
|
}
|
|
if len(segments) == 1 {
|
|
return n, true
|
|
}
|
|
return findRelatedNode(n.children, segments[1:])
|
|
}
|
|
return relatedNode{}, false
|
|
}
|
|
|
|
// selectSubtree selects a node and all of its descendants.
|
|
func selectSubtree(n relatedNode, r *Config) {
|
|
n.selectInto(r)
|
|
for _, c := range n.children {
|
|
selectSubtree(c, r)
|
|
}
|
|
}
|