Skip to content
This repository was archived by the owner on Aug 20, 2021. It is now read-only.

Add insert param template parsing in nested parts #63

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions types/oscal/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,29 @@ type Part struct {
Prose *Prose `xml:",any" json:"prose,omitempty"`
}

func traverseParts(part *Part, parameterID, parameterVal string) {
if part == nil {
return
}
if part.Prose == nil {
return
}

part.Prose.ReplaceInsertParams(parameterID, parameterVal)
if len(part.Parts) == 0 {
return
}
for i := range part.Parts {
traverseParts(&part.Parts[i], parameterID, parameterVal)
}
return
}

// ModifyProse modifies prose insert parameter template
func (part *Part) ModifyProse(parameterID, parameterVal string) {
traverseParts(part, parameterID, parameterVal)
}

// A group of reference descriptions
type References struct {

Expand Down
73 changes: 73 additions & 0 deletions types/oscal/catalog/catalog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package catalog

import (
"fmt"
"testing"
)

func TestModifyParts(t *testing.T) {
parameterIDToChange := "ac-10_prm_3"
parameterIDNotToChange := "ac-10_prm_2"
parameterVal := "[CHANGED]"
shouldChange := fmt.Sprintf(`this should change. <insert param-id="%s">`, parameterIDToChange)
shouldNotChange := fmt.Sprintf(`this should not change <insert param-id="%s">`, parameterIDNotToChange)
afterChange := fmt.Sprintf(`this should change. %s`, parameterVal)
prose := Prose{
P: []P{
P{
Raw: shouldChange,
},
},
}
nestedProse := Prose{
P: []P{
P{
Raw: shouldChange,
},
},
}
c := Catalog{
Groups: []Group{
Group{
Controls: []Control{
Control{
Parts: []Part{
Part{
Prose: &prose,
Parts: []Part{
Part{
Prose: &Prose{
P: []P{
P{
Raw: shouldNotChange,
},
},
},
Parts: []Part{
Part{
Prose: &nestedProse,
},
},
},
},
},
},
},
},
},
},
}

c.Groups[0].Controls[0].Parts[0].ModifyProse(parameterIDToChange, parameterVal)

if c.Groups[0].Controls[0].Parts[0].Prose.P[0].Raw != afterChange {
t.Error("part not modified")
}

if c.Groups[0].Controls[0].Parts[0].Parts[0].Prose.P[0].Raw != shouldNotChange {
t.Error("part got modified which shouldnt")
}
if c.Groups[0].Controls[0].Parts[0].Parts[0].Parts[0].Prose.P[0].Raw != afterChange {
t.Error("part not modified")
}
}
26 changes: 26 additions & 0 deletions types/oscal/catalog/prose.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ package catalog
import (
"encoding/json"
"encoding/xml"
"fmt"
"regexp"
"strings"
)

Expand All @@ -27,6 +29,30 @@ type Prose struct {
Pre []Pre
}

// ReplaceInsertParams replaces insert parameters
func (p *Prose) ReplaceInsertParams(parameterID, parameterValue string) error {

rs := fmt.Sprintf(`<insert param-id="%s">`, parameterID)
regex, err := regexp.Compile(rs)
if err != nil {
return err
}
for i := range p.P {
p.P[i].Raw = regex.ReplaceAllString(p.P[i].Raw, parameterValue)

}
for i := range p.OL {
p.OL[i].Raw = regex.ReplaceAllString(p.OL[i].Raw, parameterValue)
}
for i := range p.Pre {
p.Pre[i].Raw = regex.ReplaceAllString(p.Pre[i].Raw, parameterValue)
}
for i := range p.UL {
p.UL[i].Raw = regex.ReplaceAllString(p.UL[i].Raw, parameterValue)
}
return nil
}

// MarshalXML ...
func (p *Prose) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
raw := strings.Join(p.raw, "")
Expand Down