Skip to content
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

fix(generate): template nested prose with params #780

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/pkg/common/oscal/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func ControlToImplementedRequirement(control *oscalTypes_1_1_2.Control, targetRe
if contains(targetRemarks, part.Name) {
controlDescription += fmt.Sprintf("%s:\n", strings.ToTitle(part.Name))
if part.Prose != "" && strings.Contains(part.Prose, "{{ insert: param,") {
controlDescription += replaceParams(part.Prose, paramMap)
controlDescription += replaceParams(part.Prose, paramMap, false)
} else {
controlDescription += part.Prose
}
Expand Down Expand Up @@ -602,7 +602,7 @@ func addPart(part *[]oscalTypes_1_1_2.Part, paramMap map[string]parameter, level
if prose == "" {
result += fmt.Sprintf("%s%s\n", tabs, label)
} else if strings.Contains(prose, "{{ insert: param,") {
result += fmt.Sprintf("%s%s %s\n", tabs, label, replaceParams(prose, paramMap))
result += fmt.Sprintf("%s%s %s\n", tabs, label, replaceParams(prose, paramMap, false))
} else {
result += fmt.Sprintf("%s%s %s\n", tabs, label, prose)
}
Expand All @@ -616,15 +616,30 @@ func addPart(part *[]oscalTypes_1_1_2.Part, paramMap map[string]parameter, level
return result
}

func replaceParams(input string, params map[string]parameter) string {
func replaceParams(input string, params map[string]parameter, nested bool) string {
brandtkeller marked this conversation as resolved.
Show resolved Hide resolved
re := regexp.MustCompile(`{{\s*insert:\s*param,\s*([^}\s]+)\s*}}`)
result := re.ReplaceAllStringFunc(input, func(match string) string {
paramName := strings.TrimSpace(re.FindStringSubmatch(match)[1])
if param, ok := params[paramName]; ok {
if param.Select == nil {
if nested {
// If we know there is no information that requires prepending
return param.Label
} else if param.Select == nil {
// Standard assignment given a label
return fmt.Sprintf("[Assignment: organization-defined %s]", param.Label)
} else {
return fmt.Sprintf("[Selection: (%s) organization-defined %s]", param.Select.HowMany, strings.Join(param.Select.Choice, "; "))
// Join many choices into a single item of prose
prose := fmt.Sprintf("[Selection: (%s) organization-defined", param.Select.HowMany)
for _, choice := range param.Select.Choice {
// Handle potential nested parameter use
if strings.Contains(choice, "{{ insert: param,") {
replaced := replaceParams(choice, params, true)
prose += fmt.Sprintf(" %s;", replaced)
} else {
prose += fmt.Sprintf(" %s;", choice)
}
}
return fmt.Sprintf("%s]", prose)
}
}
return match
Expand Down