generateRbacObjects will take the policy.go file, and send it as input to the go templates. Some AST of the Action enum is also included.
(templateSource string)
| 201 | // generateRbacObjects will take the policy.go file, and send it as input |
| 202 | // to the go templates. Some AST of the Action enum is also included. |
| 203 | func generateRbacObjects(templateSource string) ([]byte, error) { |
| 204 | // Parse the policy.go file for the action enums |
| 205 | f, err := parser.ParseFile(token.NewFileSet(), "./coderd/rbac/policy/policy.go", nil, parser.ParseComments) |
| 206 | if err != nil { |
| 207 | return nil, xerrors.Errorf("parsing policy.go: %w", err) |
| 208 | } |
| 209 | actionMap := fileActions(f) |
| 210 | actionList := make([]ActionDetails, 0) |
| 211 | for value, enum := range actionMap { |
| 212 | actionList = append(actionList, ActionDetails{ |
| 213 | Enum: enum, |
| 214 | Value: value, |
| 215 | }) |
| 216 | } |
| 217 | |
| 218 | // Sorting actions for auto gen consistency. |
| 219 | slices.SortFunc(actionList, func(a, b ActionDetails) int { |
| 220 | return strings.Compare(a.Enum, b.Enum) |
| 221 | }) |
| 222 | |
| 223 | var errorList []error |
| 224 | var x int |
| 225 | tpl, err := template.New("object.gotmpl").Funcs(template.FuncMap{ |
| 226 | "capitalize": utilstrings.Capitalize, |
| 227 | "pascalCaseName": pascalCaseName[string], |
| 228 | "actionsList": func() []ActionDetails { |
| 229 | return actionList |
| 230 | }, |
| 231 | "actionsOf": func(d Definition) []string { |
| 232 | // Extract and sort action string keys for deterministic output. |
| 233 | list := make([]string, 0, len(d.Actions)) |
| 234 | for a := range d.Actions { |
| 235 | list = append(list, string(a)) |
| 236 | } |
| 237 | slices.Sort(list) |
| 238 | return list |
| 239 | }, |
| 240 | "allCaseList": func(defs []Definition) string { |
| 241 | // Build a multi-line comma-separated list of all scope constants (including builtins) |
| 242 | // suitable for use in a `case ...:` clause, without a trailing comma. |
| 243 | var names []string |
| 244 | // Builtins first, sourced dynamically from the rbac package to avoid drift. |
| 245 | for _, n := range rbac.BuiltinScopeNames() { |
| 246 | // Use typed string literals to avoid relying on constant identifiers. |
| 247 | names = append(names, fmt.Sprintf("ScopeName(%q)", string(n))) |
| 248 | } |
| 249 | for _, d := range defs { |
| 250 | res := pascalCaseName[string](d.Type) |
| 251 | acts := make([]string, 0, len(d.Actions)) |
| 252 | for a := range d.Actions { |
| 253 | acts = append(acts, string(a)) |
| 254 | } |
| 255 | slices.Sort(acts) |
| 256 | for _, a := range acts { |
| 257 | names = append(names, "Scope"+res+pascalCaseName[string](a)) |
| 258 | } |
| 259 | } |
| 260 | return strings.Join(names, ",\n\t\t") |
no test coverage detected