From 741357ee96c4ea3fc1ad3eae2cc04102553f009c Mon Sep 17 00:00:00 2001 From: "Cole (Mike) Winberry" Date: Tue, 14 May 2024 09:46:27 -0700 Subject: [PATCH] refactor!(component): #388 update mergeComponents and ComponentFromCatalog to use pointers refactor(generate): update all refs to ComponentFromCatalog --- src/cmd/generate/generate.go | 2 +- src/pkg/common/oscal/component.go | 18 +++++++++++------- src/pkg/common/oscal/component_test.go | 4 ++-- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cmd/generate/generate.go b/src/cmd/generate/generate.go index 493aa7a4..a6f4e82f 100644 --- a/src/cmd/generate/generate.go +++ b/src/cmd/generate/generate.go @@ -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") } diff --git a/src/pkg/common/oscal/component.go b/src/pkg/common/oscal/component.go index fce76aff..c9febce6 100644 --- a/src/pkg/common/oscal/component.go +++ b/src/pkg/common/oscal/component.go @@ -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 { @@ -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) @@ -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{} diff --git a/src/pkg/common/oscal/component_test.go b/src/pkg/common/oscal/component_test.go index 7cea38fc..679a192b 100644 --- a/src/pkg/common/oscal/component_test.go +++ b/src/pkg/common/oscal/component_test.go @@ -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 @@ -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 {