Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
refactor: core/to pkg (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgacsal authored Feb 9, 2024
1 parent edbf795 commit 8d9f19c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cli/families/rootkits/chkrootkit/utils/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,5 @@ func processAliensToRootkits(aliensResult string) ([]Rootkit, error) {
return nil, fmt.Errorf("failed to get output: %w", err)
}

return to.ValuesSlice(rootkits), nil
return to.Values(rootkits), nil
}
8 changes: 4 additions & 4 deletions cli/presenter/apimodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ func ConvertMalwareResultToMalwareAndMetadata(malwareResults *malware.MergedResu
for _, m := range malwareResults.DetectedMalware {
mal := m // Prevent loop variable pointer export
malwareList = append(malwareList, apitypes.Malware{
MalwareName: to.OmitEmpty(mal.MalwareName),
MalwareType: to.OmitEmpty(mal.MalwareType),
Path: to.OmitEmpty(mal.Path),
RuleName: to.OmitEmpty(mal.RuleName),
MalwareName: to.PtrOrNil(mal.MalwareName),
MalwareType: to.PtrOrNil(mal.MalwareType),
Path: to.PtrOrNil(mal.Path),
RuleName: to.PtrOrNil(mal.RuleName),
})
}

Expand Down
6 changes: 4 additions & 2 deletions core/to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func Ptr[T any](value T) *T {
return &value
}

func OmitEmpty[T comparable](t T) *T {
// PtrOrNil returns a pointer to t if it has a non-empty value otherwise nil.
func PtrOrNil[T comparable](t T) *T {
var empty T
if t == empty {
return nil
Expand Down Expand Up @@ -49,7 +50,8 @@ func Keys[K comparable, V any](m map[K]V) []K {
return s
}

func ValuesSlice[K comparable, V any](m map[K]V) []V {
// Values returns a slice of values from m map.
func Values[K comparable, V any](m map[K]V) []V {
s := make([]V, 0, len(m))
for _, v := range m {
s = append(s, v)
Expand Down
2 changes: 1 addition & 1 deletion core/to/to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Test_ValuesSlice(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValuesSlice(tt.args.m)
got := Values(tt.args.m)
if got != nil {
sort.Slice(got, func(i, j int) bool {
switch got[0].(type) {
Expand Down
2 changes: 1 addition & 1 deletion uibackend/server/dashboard_findings_impact.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func processFindings(findings []apitypes.Finding, findingAssetMap map[findingAss

// getSortedFindingInfoCountSlice will return a slice of findingInfoCount desc sorted by AssetCount.
func getSortedFindingInfoCountSlice(findingAssetMapCount map[string]findingInfoCount) []findingInfoCount {
findingInfoCountSlice := to.ValuesSlice(findingAssetMapCount)
findingInfoCountSlice := to.Values(findingAssetMapCount)
sort.Slice(findingInfoCountSlice, func(i, j int) bool {
return findingInfoCountSlice[i].AssetCount > findingInfoCountSlice[j].AssetCount
})
Expand Down

0 comments on commit 8d9f19c

Please sign in to comment.