Skip to content

Commit

Permalink
feat: replace x/exp with stdlib functions (#2580)
Browse files Browse the repository at this point in the history
  • Loading branch information
kruskall authored Oct 10, 2024
1 parent beb0270 commit 821947e
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ require (
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
go.uber.org/zap/exp v0.1.1-0.20240530135403-273f9ea8c887
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/oauth2 v0.23.0
google.golang.org/api v0.199.0
Expand Down Expand Up @@ -161,6 +160,7 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.29.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/tools v0.25.0 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/fetching/fetchers/azure/assets_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
"context"
"errors"
"fmt"
"maps"
"slices"

"github.com/elastic/elastic-agent-libs/logp"
"golang.org/x/exp/maps"

"github.com/elastic/cloudbeat/internal/resources/fetching"
"github.com/elastic/cloudbeat/internal/resources/fetching/cycle"
Expand Down Expand Up @@ -98,7 +99,7 @@ func (f *AzureAssetsFetcher) Fetch(ctx context.Context, cycleMetadata cycle.Meta
var assets []inventory.AzureAsset
for _, assetGroup := range AzureAssetGroups {
// Fetching all types even if non-existent in asset group for simplicity
r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, maps.Keys(AzureAssetTypeToTypePair))
r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, slices.Collect(maps.Keys(AzureAssetTypeToTypePair)))
if err != nil {
f.log.Errorf("AzureAssetsFetcher.Fetch failed to fetch asset group %s: %s", assetGroup, err.Error())
errAgg = errors.Join(errAgg, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ package fetchers
import (
"context"
"errors"
"maps"
"testing"

"github.com/samber/lo"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"golang.org/x/exp/maps"

"github.com/elastic/cloudbeat/internal/resources/fetching"
"github.com/elastic/cloudbeat/internal/resources/fetching/cycle"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *AzureAssetsFetcherTestSuite) TestFetcher_Fetch() {
var flatMockAssets []inventory.AzureAsset
for _, assetGroup := range AzureAssetGroups {
var mockAssets []inventory.AzureAsset
for _, assetType := range maps.Keys(AzureAssetTypeToTypePair) {
for assetType := range maps.Keys(AzureAssetTypeToTypePair) {
mockAssets = append(mockAssets,
inventory.AzureAsset{
Id: "id",
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/fetching/fetchers/azure/batch_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"context"
"errors"
"fmt"
"maps"
"slices"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/samber/lo"
"golang.org/x/exp/maps"

"github.com/elastic/cloudbeat/internal/resources/fetching"
"github.com/elastic/cloudbeat/internal/resources/fetching/cycle"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (f *AzureBatchAssetFetcher) Fetch(ctx context.Context, cycleMetadata cycle.
var errAgg error
assets := []inventory.AzureAsset{}
for _, assetGroup := range AzureBatchAssetGroups {
r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, maps.Keys(AzureBatchAssets))
r, err := f.provider.ListAllAssetTypesByName(ctx, assetGroup, slices.Collect(maps.Keys(AzureBatchAssets)))
if err != nil {
f.log.Errorf("AzureBatchAssetFetcher.Fetch failed to fetch asset group %s: %s", assetGroup, err.Error())
errAgg = errors.Join(errAgg, err)
Expand Down
10 changes: 5 additions & 5 deletions internal/resources/fetching/fetchers/azure/batch_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ package fetchers
import (
"context"
"fmt"
"maps"
"testing"

"github.com/samber/lo"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"golang.org/x/exp/maps"

"github.com/elastic/cloudbeat/internal/resources/fetching"
"github.com/elastic/cloudbeat/internal/resources/fetching/cycle"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (s *AzureBatchAssetFetcherTestSuite) TestFetcher_Fetch() {
var flatMockAssets []inventory.AzureAsset
for _, assetGroup := range AzureBatchAssetGroups {
var mockAssets []inventory.AzureAsset
for _, assetType := range maps.Keys(AzureBatchAssets) {
for assetType := range maps.Keys(AzureBatchAssets) {
mockId := fmt.Sprintf("%s-%s", AzureBatchAssets[assetType].SubType, "subId1")
mockAssets = append(mockAssets,
inventory.AzureAsset{
Expand Down Expand Up @@ -167,8 +167,8 @@ func (s *AzureBatchAssetFetcherTestSuite) TestFetcher_Fetch_Subscriptions() {

for _, assetGroup := range AzureBatchAssetGroups {
var mockAssets []inventory.AzureAsset
for _, assetType := range maps.Keys(AzureBatchAssets) {
for _, subKey := range maps.Keys(subMap) {
for assetType := range maps.Keys(AzureBatchAssets) {
for subKey := range maps.Keys(subMap) {
mockId := fmt.Sprintf("%s-%s", AzureBatchAssets[assetType].SubType, subKey)
mockAssets = append(mockAssets,
inventory.AzureAsset{
Expand Down Expand Up @@ -213,7 +213,7 @@ func (s *AzureBatchAssetFetcherTestSuite) TestFetcher_Fetch_Subscriptions() {
})
s.Len(expectedSubs, len(subMap))

for _, subKey := range maps.Keys(expectedSubs) {
for subKey := range maps.Keys(expectedSubs) {
typesPerSub := expectedSubs[subKey]
s.Len(typesPerSub, len(AzureBatchAssets))
for _, subTypeRes := range typesPerSub {
Expand Down
5 changes: 3 additions & 2 deletions internal/vulnerability/events_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"slices"
"strings"
"time"

Expand All @@ -31,7 +33,6 @@ import (
libevents "github.com/elastic/beats/v7/libbeat/beat/events"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"golang.org/x/exp/maps"

"github.com/elastic/cloudbeat/internal/config"
"github.com/elastic/cloudbeat/internal/dataprovider"
Expand Down Expand Up @@ -414,7 +415,7 @@ func getReference(vul trivyTypes.DetectedVulnerability) string {

func getCVSSValue[T comparable](vul trivyTypes.DetectedVulnerability, value func(cvss dbTypes.CVSS) T, zeroVal T) T {
// Get all the sources
sources := maps.Keys(vul.CVSS)
sources := slices.Collect(maps.Keys(vul.CVSS))
if len(sources) == 0 {
return zeroVal
}
Expand Down
1 change: 0 additions & 1 deletion scripts/update_assets_md/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23.0
require (
github.com/ettle/strcase v0.2.0
github.com/xuri/excelize/v2 v2.8.1
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
)

require (
Expand Down
2 changes: 0 additions & 2 deletions scripts/update_assets_md/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 h1:qhbILQo1K3mphbwKh1vNm4
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
Expand Down
8 changes: 3 additions & 5 deletions scripts/update_assets_md/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (
"go/ast"
"go/parser"
"go/token"
"maps"
"os"
"slices"
"strings"

"github.com/ettle/strcase"
"github.com/xuri/excelize/v2"
"golang.org/x/exp/maps"
)

const (
Expand Down Expand Up @@ -195,8 +195,7 @@ func writeSummary(plannedByProvider, implementedByProvider *ByProvider, filepath
for providerNo, provider := range []string{AWS_PREFIX, AZURE_PREFIX, GCP_PREFIX} {
planned := plannedByProvider.Get(provider)
implemented := implementedByProvider.Get(provider)
sortedKeys := maps.Keys(planned)
slices.Sort(sortedKeys)
sortedKeys := slices.Sorted(maps.Keys(planned))

// stats
totalImplemented := 0
Expand Down Expand Up @@ -241,8 +240,7 @@ func writeSummary(plannedByProvider, implementedByProvider *ByProvider, filepath
fmt.Sprintf("**Progress: %d%% (%d/%d)**\n", percentage, totalImplemented, len(planned)),
)

sortedCategories := maps.Keys(plannedByCategory)
slices.Sort(sortedCategories)
sortedCategories := slices.Sorted(maps.Keys(plannedByCategory))

for _, category := range sortedCategories {
plannedCount := plannedByCategory[category]
Expand Down
1 change: 0 additions & 1 deletion tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
_ "github.com/pierrre/gotestcover"
_ "github.com/tsg/go-daemon"
_ "go.elastic.co/go-licence-detector"
_ "golang.org/x/exp/maps"
_ "golang.org/x/lint/golint"
_ "gotest.tools/gotestsum/cmd"
)

0 comments on commit 821947e

Please sign in to comment.