Skip to content

Commit

Permalink
Resolve insert nested elements
Browse files Browse the repository at this point in the history
  • Loading branch information
isimluk committed Oct 6, 2020
1 parent 971ede9 commit 580fe48
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
11 changes: 10 additions & 1 deletion types/oscal/catalog/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Control) partToMarkdown(part *Part, textPrefix string) string {
}
}

result = textPrefix + label + part.Prose.Raw
result = textPrefix + label + part.ResolveInserts(c)
if result[len(result)-1] != '\n' {
result += "\n"
}
Expand All @@ -49,3 +49,12 @@ func (c *Control) partToMarkdown(part *Part, textPrefix string) string {

return result
}

func (c *Control) FindParam(id string) *Param {
for idx, param := range c.Parameters {
if param.Id == id {
return &c.Parameters[idx]
}
}
return nil
}
30 changes: 30 additions & 0 deletions types/oscal/nominal_catalog/param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package nominal_catalog

import (
"fmt"
"strings"
)

func (param *Param) TextRepresentation() string {
if param.Label != "" {
return fmt.Sprintf("[Assignment: %s]", param.Label)

}
if param.Select != nil {
howMany := ""
if len(param.Select.HowMany) != 0 {
howMany = fmt.Sprintf(" (%s)", param.Select.HowMany)
}

choicesList := make([]string, len(param.Select.Alternatives))
for i, v := range param.Select.Alternatives {
choicesList[i] = string(v)
}
choices := strings.Join(choicesList, ", ")

return fmt.Sprintf("[Selection%s: %s]", howMany, choices)
}

return "[TODO: Not implemented parameter]"

}
25 changes: 25 additions & 0 deletions types/oscal/nominal_catalog/part.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nominal_catalog

import (
"regexp"
"strings"
)

type ParamResolver interface {
FindParam(id string) *Param
}

func (p *Part) ResolveInserts(resolver ParamResolver) string {
re := regexp.MustCompile(`<insert param-id="([a-z0-9._-]*)"/>`)
resolved := p.Prose.Raw

for {
match := re.FindStringSubmatch(resolved)
if len(match) == 0 {
break
}
resolved = strings.ReplaceAll(resolved, match[0], resolver.FindParam(match[1]).TextRepresentation())
}

return resolved
}

0 comments on commit 580fe48

Please sign in to comment.