Skip to content

Commit

Permalink
refactor!(component): #388 update mergeComponents and ComponentFromCa…
Browse files Browse the repository at this point in the history
…talog to use pointers

refactor(generate): update all refs to ComponentFromCatalog
  • Loading branch information
mike-winberry committed May 14, 2024
1 parent 8a5ecf3 commit 741357e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var generateComponentCmd = &cobra.Command{
}

// Create a component definition from the catalog given required context
comp, err := oscal.ComponentFromCatalog(source, *catalog, title, componentOpts.Requirements, remarks)
comp, err := oscal.ComponentFromCatalog(source, catalog, title, componentOpts.Requirements, remarks)
if err != nil {
message.Fatalf(fmt.Errorf("error creating component"), "error creating component")
}
Expand Down
18 changes: 11 additions & 7 deletions src/pkg/common/oscal/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func MergeComponentDefinitions(original *oscalTypes_1_1_2.ComponentDefinition, l
for key, value := range latestMap {
if comp, ok := originalMap[key]; ok {
// if the component exists - merge & append
comp = mergeComponents(comp, value)
comp = *mergeComponents(&comp, &value)
tempItems = append(tempItems, comp)
delete(originalMap, key)
} else {
Expand Down Expand Up @@ -101,17 +101,21 @@ func MergeComponentDefinitions(original *oscalTypes_1_1_2.ComponentDefinition, l

}

func mergeComponents(original oscalTypes_1_1_2.DefinedComponent, latest oscalTypes_1_1_2.DefinedComponent) oscalTypes_1_1_2.DefinedComponent {
func mergeComponents(original *oscalTypes_1_1_2.DefinedComponent, latest *oscalTypes_1_1_2.DefinedComponent) *oscalTypes_1_1_2.DefinedComponent {
originalMap := make(map[string]oscalTypes_1_1_2.ControlImplementationSet)

for _, item := range *original.ControlImplementations {
originalMap[item.Source] = item
if original.ControlImplementations != nil {
for _, item := range *original.ControlImplementations {
originalMap[item.Source] = item
}
}

latestMap := make(map[string]oscalTypes_1_1_2.ControlImplementationSet)

for _, item := range *latest.ControlImplementations {
latestMap[item.Source] = item
if latest.ControlImplementations != nil {
for _, item := range *latest.ControlImplementations {
latestMap[item.Source] = item
}
}

tempItems := make([]oscalTypes_1_1_2.ControlImplementationSet, 0)
Expand Down Expand Up @@ -226,7 +230,7 @@ func mergeLinks(orig []oscalTypes_1_1_2.Link, latest []oscalTypes_1_1_2.Link) *[
}

// Creates a component-definition from a catalog and identified (or all) controls. Allows for specification of what the content of the remarks section should contain.
func ComponentFromCatalog(source string, catalog oscalTypes_1_1_2.Catalog, componentTitle string, targetControls []string, targetRemarks []string) (*oscalTypes_1_1_2.ComponentDefinition, error) {
func ComponentFromCatalog(source string, catalog *oscalTypes_1_1_2.Catalog, componentTitle string, targetControls []string, targetRemarks []string) (*oscalTypes_1_1_2.ComponentDefinition, error) {
// store all of the implemented requirements
implmentedRequirements := make([]oscalTypes_1_1_2.ImplementedRequirementControlImplementation, 0)
var componentDefinition = &oscalTypes_1_1_2.ComponentDefinition{}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/common/oscal/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestComponentFromCatalog(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := oscal.ComponentFromCatalog(tt.source, tt.data, tt.title, tt.requirements, tt.remarks)
got, err := oscal.ComponentFromCatalog(tt.source, &tt.data, tt.title, tt.requirements, tt.remarks)
if (err != nil) != tt.wantErr {
t.Errorf("ComponentFromCatalog() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestMergeComponentDefinitions(t *testing.T) {
existingImplementedRequirementsMap[req.ControlId] = true
}

generated, _ := oscal.ComponentFromCatalog(tt.source, *catalog, tt.title, tt.requirements, tt.remarks)
generated, _ := oscal.ComponentFromCatalog(tt.source, catalog, tt.title, tt.requirements, tt.remarks)

merged, err := oscal.MergeComponentDefinitions(validComponent, generated)
if (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 741357e

Please sign in to comment.