@@ -658,55 +658,64 @@ func (c *CdxDoc) parseAuthors() {
658
658
}
659
659
660
660
func (c * CdxDoc ) parseSupplier () {
661
- if c .doc .Metadata == nil {
661
+ // Early return if required nested fields are nil
662
+ if c .doc .Metadata == nil || c .doc .Metadata .Supplier == nil {
662
663
return
663
664
}
664
665
665
- if c .doc .Metadata .Supplier == nil {
666
- return
666
+ // Initialize supplier with known fields
667
+ supplier := Supplier {
668
+ Name : c .doc .Metadata .Supplier .Name ,
667
669
}
668
670
669
- supplier := Supplier {}
670
-
671
- supplier .Name = c . doc . Metadata . Supplier . Name
672
- supplier . URL = lo . FromPtr ( c . doc . Metadata . Supplier . URL )[ 0 ]
671
+ // Safely handle URL
672
+ if urls := lo . FromPtr ( c . doc . Metadata . Supplier . URL ); len ( urls ) > 0 {
673
+ supplier .URL = urls [ 0 ]
674
+ }
673
675
676
+ // Handle contacts array
674
677
if c .doc .Metadata .Supplier .Contact != nil {
675
- for _ , cydxContact := range lo .FromPtr (c .doc .Metadata .Supplier .Contact ) {
676
- ctt := Contact {}
677
- ctt .Name = cydxContact .Name
678
- ctt .Email = cydxContact .Email
679
- supplier .Contacts = append (supplier .Contacts , ctt )
678
+ contacts := lo .FromPtr (c .doc .Metadata .Supplier .Contact )
679
+ if len (contacts ) > 0 {
680
+ // Pre-allocate contacts slice with known capacity
681
+ supplier .Contacts = make ([]Contact , 0 , len (contacts ))
682
+
683
+ // Process each contact
684
+ for _ , cydxContact := range contacts {
685
+ supplier .Contacts = append (supplier .Contacts , Contact {
686
+ Name : cydxContact .Name ,
687
+ Email : cydxContact .Email ,
688
+ })
689
+ }
680
690
}
681
691
}
682
692
683
693
c .CdxSupplier = supplier
684
694
}
685
695
686
696
func (c * CdxDoc ) parseManufacturer () {
687
- if c .doc .Metadata == nil {
697
+ if c .doc .Metadata == nil || c . doc . Metadata . Manufacture == nil {
688
698
return
689
699
}
690
700
691
- if c .doc .Metadata .Manufacture == nil {
692
- return
693
- }
694
-
695
- m := Manufacturer {}
701
+ manufacturer := Manufacturer {Name : c .doc .Metadata .Manufacture .Name }
696
702
697
- m .Name = c .doc .Metadata .Manufacture .Name
698
- m .URL = lo .FromPtr (c .doc .Metadata .Manufacture .URL )[0 ]
703
+ if urls := lo .FromPtr (c .doc .Metadata .Manufacture .URL ); len (urls ) > 0 {
704
+ manufacturer .URL = urls [0 ]
705
+ }
699
706
700
- if c .doc .Metadata .Manufacture .Contact != nil {
701
- for _ , cydxContact := range lo .FromPtr (c .doc .Metadata .Manufacture .Contact ) {
702
- ctt := Contact {}
703
- ctt .Name = cydxContact .Name
704
- ctt .Email = cydxContact .Email
705
- m .Contacts = append (m .Contacts , ctt )
707
+ contacts := lo .FromPtr (c .doc .Metadata .Manufacture .Contact )
708
+ if len (contacts ) > 0 {
709
+ manufacturer .Contacts = make ([]Contact , len (contacts ))
710
+ for i , contact := range contacts {
711
+ manufacturer .Contacts [i ] = Contact {
712
+ Name : contact .Name ,
713
+ Email : contact .Email ,
714
+ }
706
715
}
707
716
}
708
717
709
- c .CdxManufacturer = m
718
+ c .CdxManufacturer = manufacturer
710
719
}
711
720
712
721
func (c * CdxDoc ) parsePrimaryCompAndRelationships () {
@@ -746,90 +755,47 @@ func (c *CdxDoc) parsePrimaryCompAndRelationships() {
746
755
c .PrimaryComponent .Dependecies = totalDependencies
747
756
}
748
757
749
- // nolint
750
- func (c * CdxDoc ) parseComposition () {
751
- if c .doc .Metadata == nil {
752
- return
753
- }
754
- if c .doc .Compositions == nil {
755
- return
756
- }
757
- c .composition = make (map [string ]string )
758
-
759
- for _ , cp := range lo .FromPtr (c .doc .Compositions ) {
760
- state := compNormalise (cp .BOMRef )
761
- c .composition [cp .BOMRef ] = state
762
- }
763
- }
764
-
765
- // nolint
766
- func compNormalise (compID string ) string {
767
- switch cydx .CompositionAggregate (compID ) {
768
- case cydx .CompositionAggregateComplete :
769
- return "complete"
770
- case cydx .CompositionAggregateIncomplete :
771
- return "incomplete"
772
- case cydx .CompositionAggregateIncompleteFirstPartyOnly :
773
- return "incomplete-first-party-only"
774
- case cydx .CompositionAggregateIncompleteFirstPartyOpenSourceOnly :
775
- return "incomplete-first-party-open-source-only"
776
- case cydx .CompositionAggregateIncompleteFirstPartyProprietaryOnly :
777
- return "incomplete-first-party-proprietary-only"
778
- case cydx .CompositionAggregateIncompleteThirdPartyOnly :
779
- return "incomplete-third-party-only"
780
- case cydx .CompositionAggregateIncompleteThirdPartyOpenSourceOnly :
781
- return "incomplete-third-party-open-source-only"
782
- case cydx .CompositionAggregateIncompleteThirdPartyProprietaryOnly :
783
- return "incomplete-third-party-proprietary-only"
784
- case cydx .CompositionAggregateNotSpecified :
785
- return "not-specified"
786
- case cydx .CompositionAggregateUnknown :
787
- return "unknown"
788
- }
789
- return "not-specified"
790
- }
791
-
792
758
func (c * CdxDoc ) assignSupplier (comp * cydx.Component ) * Supplier {
793
759
if comp .Supplier == nil {
794
760
c .addToLogs (fmt .Sprintf ("cdx doc comp %s no supplier found" , comp .Name ))
795
761
return nil
796
762
}
797
763
798
- supplier := Supplier {}
799
-
800
- if comp .Supplier .Name != "" {
801
- supplier .Name = comp .Supplier .Name
802
- }
764
+ supplier := Supplier {Name : comp .Supplier .Name }
803
765
804
- if comp . Supplier . URL != nil && len ( lo .FromPtr (comp .Supplier .URL )) > 0 {
805
- supplier .URL = lo . FromPtr ( comp . Supplier . URL ) [0 ]
766
+ if urls := lo .FromPtr (comp .Supplier .URL ); len ( urls ) > 0 {
767
+ supplier .URL = urls [0 ]
806
768
}
807
769
808
- if comp .Supplier .Contact != nil {
809
- for _ , cydxContact := range lo .FromPtr (comp .Supplier .Contact ) {
810
- ctt := Contact {}
811
- ctt .Name = cydxContact .Name
812
- ctt .Email = cydxContact .Email
813
- supplier .Contacts = append (supplier .Contacts , ctt )
770
+ contacts := lo .FromPtr (comp .Supplier .Contact )
771
+ if len (contacts ) > 0 {
772
+ supplier .Contacts = make ([]Contact , len (contacts ))
773
+ for i , contact := range contacts {
774
+ supplier .Contacts [i ] = Contact {
775
+ Name : contact .Name ,
776
+ Email : contact .Email ,
777
+ }
814
778
}
815
779
}
816
780
817
781
return & supplier
818
782
}
819
783
820
784
func (c * CdxDoc ) parseCompositions () {
785
+ c .compositions = make (map [string ]string )
786
+
821
787
if c .doc .Compositions == nil {
822
- c .compositions = map [string ]string {}
823
788
return
824
789
}
825
790
826
- for _ , comp := range lo .FromPtr (c .doc .Compositions ) {
827
- if comp .Assemblies == nil {
791
+ for _ , composition := range lo .FromPtr (c .doc .Compositions ) {
792
+ assemblies := lo .FromPtr (composition .Assemblies )
793
+ if len (assemblies ) == 0 {
828
794
continue
829
795
}
830
796
831
- for _ , assembly := range lo . FromPtr ( comp . Assemblies ) {
832
- c .compositions [string (assembly )] = string (comp .Aggregate )
797
+ for _ , assembly := range assemblies {
798
+ c .compositions [string (assembly )] = string (composition .Aggregate )
833
799
}
834
800
}
835
801
}
0 commit comments