diff --git a/.github/workflows/adapter-code-coverage.yml b/.github/workflows/adapter-code-coverage.yml index 4d5751c27f8..37936a1d17d 100644 --- a/.github/workflows/adapter-code-coverage.yml +++ b/.github/workflows/adapter-code-coverage.yml @@ -1,10 +1,13 @@ -name: Adapter code coverage +name: Adapter Code Coverage + on: pull_request_target: paths: ["adapters/*/*.go"] + permissions: pull-requests: write contents: write + jobs: run-coverage: runs-on: ubuntu-latest @@ -12,16 +15,16 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: 1.20.5 + go-version: 1.22.3 - - name: Checkout pull request branch + - name: Checkout Code uses: actions/checkout@v4 with: fetch-depth: 0 ref: ${{github.event.pull_request.head.ref}} repository: ${{github.event.pull_request.head.repo.full_name}} - - name: Get adapter directories + - name: Discover Adapter Directories id: get_directories uses: actions/github-script@v7 with: @@ -40,7 +43,7 @@ jobs: // run coverage for maximum of 2 directories return (directories.length == 0 || directories.length > 2) ? "" : JSON.stringify(directories) - - name: Run coverage tests + - name: Run Coverage Tests id: run_coverage if: steps.get_directories.outputs.result != '' run: | @@ -67,14 +70,14 @@ jobs: cd .. rm -f -r ./* - - name: Checkout coverage-preview branch + - name: Checkout Coverage Preview Branch uses: actions/checkout@v4 with: fetch-depth: 0 ref: coverage-preview repository: prebid/prebid-server - - name: Commit coverage files to coverage-preview branch + - name: Upload Coverage Results if: steps.run_coverage.outputs.coverage_dir != '' id: commit_coverage run: | @@ -88,11 +91,11 @@ jobs: git push origin coverage-preview echo "remote_coverage_preview_dir=${directory}" >> $GITHUB_OUTPUT - - name: Checkout master branch + - name: Checkout Master Branch if: steps.get_directories.outputs.result != '' run: git checkout master - - name: Add coverage summary to pull request + - name: Add Coverage Summary To Pull Request if: steps.run_coverage.outputs.coverage_dir != '' && steps.commit_coverage.outputs.remote_coverage_preview_dir != '' uses: actions/github-script@v7 with: diff --git a/.github/workflows/semgrep.yml b/.github/workflows/semgrep.yml index b641d5667ed..55fba5ee287 100644 --- a/.github/workflows/semgrep.yml +++ b/.github/workflows/semgrep.yml @@ -1,21 +1,24 @@ -name: Adapter semgrep checks +name: Adapter Semgrep Check + on: pull_request_target: paths: ["adapters/*/*.go"] + permissions: pull-requests: write + jobs: semgrep-check: runs-on: ubuntu-latest steps: - - name: Checkout repo + - name: Checkout Code uses: actions/checkout@v4 with: fetch-depth: 0 ref: ${{github.event.pull_request.head.ref}} repository: ${{github.event.pull_request.head.repo.full_name}} - - name: Calculate diff + - name: Calculate Code Diff id: calculate_diff uses: actions/github-script@v7 with: @@ -29,7 +32,7 @@ jobs: const helper = utils.diffHelper({github, context, fileNameFilter, event: "${{github.event.action}}", testName: "${{github.job}}"}) return await helper.buildDiff() - - name: Should run semgrep + - name: Check For Changes id: should_run_semgrep run: | hasChanges=$(echo '${{ steps.calculate_diff.outputs.result }}' | jq .pullRequest.hasChanges) @@ -41,7 +44,7 @@ jobs: pip3 install semgrep==1.22.0 semgrep --version - - name: Run semgrep tests + - name: Run Semgrep id: run_semgrep_tests if: contains(steps.should_run_semgrep.outputs.hasChanges, 'true') run: | @@ -49,7 +52,7 @@ jobs: outputs=$(semgrep --gitlab-sast --config=.semgrep/adapter $unqouted_string | jq '[.vulnerabilities[] | {"file": .location.file, "severity": .severity, "start": .location.start_line, "end": .location.end_line, "message": (.message | gsub("\\n"; "\n"))}]' | jq -c | jq -R) echo "semgrep_result=${outputs}" >> "$GITHUB_OUTPUT" - - name: Add pull request comment + - name: Add Pull Request Comment id: add_pull_request_comment if: contains(steps.should_run_semgrep.outputs.hasChanges, 'true') uses: actions/github-script@v7 @@ -66,7 +69,7 @@ jobs: const { previousScan, currentScan } = await helper.addReviewComments() return previousScan.unAddressedComments + currentScan.newComments - - name: Adapter semgrep checks result + - name: Check Results if: contains(steps.should_run_semgrep.outputs.hasChanges, 'true') run: | if [ "${{steps.add_pull_request_comment.outputs.result}}" -ne "0" ]; then diff --git a/.github/workflows/validate-merge.yml b/.github/workflows/validate-merge.yml index c44b4002f4e..d58a924d984 100644 --- a/.github/workflows/validate-merge.yml +++ b/.github/workflows/validate-merge.yml @@ -12,7 +12,7 @@ jobs: - name: Install Go uses: actions/setup-go@v5 with: - go-version: 1.20.5 + go-version: 1.22.3 - name: Checkout Merged Branch uses: actions/checkout@v4 diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index a3914c3c868..e40b165b8b3 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -10,7 +10,7 @@ jobs: validate: strategy: matrix: - go-version: [1.19.x, 1.20.x] + go-version: [1.21.x, 1.22.x] os: [ubuntu-20.04] runs-on: ${{ matrix.os }} diff --git a/Dockerfile b/Dockerfile index bff68b614b3..17b6ce20387 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,8 @@ RUN apt-get update && \ apt-get -y upgrade && \ apt-get install -y wget WORKDIR /tmp -RUN wget https://dl.google.com/go/go1.20.5.linux-amd64.tar.gz && \ - tar -xf go1.20.5.linux-amd64.tar.gz && \ +RUN wget https://dl.google.com/go/go1.22.3.linux-amd64.tar.gz && \ + tar -xf go1.22.3.linux-amd64.tar.gz && \ mv go /usr/local RUN mkdir -p /app/prebid-server/ WORKDIR /app/prebid-server/ diff --git a/README.md b/README.md index 20cccf8a0ce..4a44ecd7c6a 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Ensure that you deploy the `/static` directory, as Prebid Server requires those ## Developing -Prebid Server requires [Go](https://go.dev) version 1.19 or newer. You can develop on any operating system that Go supports; however, please note that our helper scripts are written in bash. +Prebid Server requires [Go](https://go.dev) version 1.21 or newer. You can develop on any operating system that Go supports; however, please note that our helper scripts are written in bash. 1. Clone The Repository ``` bash diff --git a/adapters/appnexus/appnexus.go b/adapters/appnexus/appnexus.go index bfa14016b2a..5f0187ec4c4 100644 --- a/adapters/appnexus/appnexus.go +++ b/adapters/appnexus/appnexus.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net/http" "net/url" "strconv" @@ -13,7 +14,6 @@ import ( "github.com/prebid/openrtb/v20/adcom1" "github.com/prebid/openrtb/v20/openrtb2" "github.com/prebid/prebid-server/v2/config" - "github.com/prebid/prebid-server/v2/util/maputil" "github.com/prebid/prebid-server/v2/util/ptrutil" "github.com/prebid/prebid-server/v2/util/randomutil" @@ -315,7 +315,7 @@ func splitRequests(imps []openrtb2.Imp, request *openrtb2.BidRequest, requestExt errs = append(errs, err) } - requestExtClone := maputil.Clone(requestExt) + requestExtClone := maps.Clone(requestExt) requestExtClone["appnexus"] = appnexusExtJson request.Ext, err = json.Marshal(requestExtClone) diff --git a/go.mod b/go.mod index 3523d5ab768..c16acc331df 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/prebid/prebid-server/v2 -go 1.20 +go 1.21 require ( github.com/DATA-DOG/go-sqlmock v1.5.0 diff --git a/openrtb_ext/floors.go b/openrtb_ext/floors.go index 12c04c8ad16..d903fe971f0 100644 --- a/openrtb_ext/floors.go +++ b/openrtb_ext/floors.go @@ -1,9 +1,10 @@ package openrtb_ext import ( - "github.com/prebid/prebid-server/v2/util/maputil" + "maps" + "slices" + "github.com/prebid/prebid-server/v2/util/ptrutil" - "github.com/prebid/prebid-server/v2/util/sliceutil" ) // Defines strings for FetchStatus @@ -182,10 +183,10 @@ func (data *PriceFloorData) DeepCopy() *PriceFloorData { eachGroup.ModelWeight = ptrutil.Clone(data.ModelGroups[i].ModelWeight) eachGroup.ModelVersion = data.ModelGroups[i].ModelVersion eachGroup.SkipRate = data.ModelGroups[i].SkipRate - eachGroup.Values = maputil.Clone(data.ModelGroups[i].Values) + eachGroup.Values = maps.Clone(data.ModelGroups[i].Values) eachGroup.Default = data.ModelGroups[i].Default eachGroup.Schema = PriceFloorSchema{ - Fields: sliceutil.Clone(data.ModelGroups[i].Schema.Fields), + Fields: slices.Clone(data.ModelGroups[i].Schema.Fields), Delimiter: data.ModelGroups[i].Schema.Delimiter, } newModelGroups[i] = eachGroup diff --git a/openrtb_ext/regs.go b/openrtb_ext/regs.go index 7277bb86392..2b395d49954 100644 --- a/openrtb_ext/regs.go +++ b/openrtb_ext/regs.go @@ -1,8 +1,6 @@ package openrtb_ext -import ( - "github.com/prebid/prebid-server/v2/util/sliceutil" -) +import "slices" // ExtRegs defines the contract for bidrequest.regs.ext type ExtRegs struct { @@ -48,7 +46,7 @@ func (erd *ExtRegsDSA) Clone() *ExtRegsDSA { clonedTransparency := make([]ExtBidDSATransparency, len(erd.Transparency)) for i, transparency := range erd.Transparency { newTransparency := transparency - newTransparency.Params = sliceutil.Clone(transparency.Params) + newTransparency.Params = slices.Clone(transparency.Params) clonedTransparency[i] = newTransparency } clone.Transparency = clonedTransparency diff --git a/openrtb_ext/request.go b/openrtb_ext/request.go index 8533ccced9e..b4f29a704d9 100644 --- a/openrtb_ext/request.go +++ b/openrtb_ext/request.go @@ -3,12 +3,12 @@ package openrtb_ext import ( "encoding/json" "fmt" + "maps" + "slices" "github.com/prebid/openrtb/v20/openrtb2" "github.com/prebid/prebid-server/v2/util/jsonutil" - "github.com/prebid/prebid-server/v2/util/maputil" "github.com/prebid/prebid-server/v2/util/ptrutil" - "github.com/prebid/prebid-server/v2/util/sliceutil" ) // FirstPartyDataExtKey defines a field name within request.ext and request.imp.ext reserved for first party data. @@ -381,14 +381,14 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { } clone := *erp - clone.Aliases = maputil.Clone(erp.Aliases) - clone.AliasGVLIDs = maputil.Clone(erp.AliasGVLIDs) - clone.BidAdjustmentFactors = maputil.Clone(erp.BidAdjustmentFactors) + clone.Aliases = maps.Clone(erp.Aliases) + clone.AliasGVLIDs = maps.Clone(erp.AliasGVLIDs) + clone.BidAdjustmentFactors = maps.Clone(erp.BidAdjustmentFactors) if erp.BidderConfigs != nil { clone.BidderConfigs = make([]BidderConfig, len(erp.BidderConfigs)) for i, bc := range erp.BidderConfigs { - clonedBidderConfig := BidderConfig{Bidders: sliceutil.Clone(bc.Bidders)} + clonedBidderConfig := BidderConfig{Bidders: slices.Clone(bc.Bidders)} if bc.Config != nil { config := &Config{ORTB2: ptrutil.Clone(bc.Config.ORTB2)} clonedBidderConfig.Config = config @@ -417,7 +417,7 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { if erp.CurrencyConversions != nil { newConvRates := make(map[string]map[string]float64, len(erp.CurrencyConversions.ConversionRates)) for key, val := range erp.CurrencyConversions.ConversionRates { - newConvRates[key] = maputil.Clone(val) + newConvRates[key] = maps.Clone(val) } clone.CurrencyConversions = &ExtRequestCurrency{ConversionRates: newConvRates} if erp.CurrencyConversions.UsePBSRates != nil { @@ -426,13 +426,13 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { } if erp.Data != nil { - clone.Data = &ExtRequestPrebidData{Bidders: sliceutil.Clone(erp.Data.Bidders)} + clone.Data = &ExtRequestPrebidData{Bidders: slices.Clone(erp.Data.Bidders)} if erp.Data.EidPermissions != nil { newEidPermissions := make([]ExtRequestPrebidDataEidPermission, len(erp.Data.EidPermissions)) for i, eidp := range erp.Data.EidPermissions { newEidPermissions[i] = ExtRequestPrebidDataEidPermission{ Source: eidp.Source, - Bidders: sliceutil.Clone(eidp.Bidders), + Bidders: slices.Clone(eidp.Bidders), } } clone.Data.EidPermissions = newEidPermissions @@ -451,7 +451,7 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { for i, mulBid := range erp.MultiBid { newMulBid := &ExtMultiBid{ Bidder: mulBid.Bidder, - Bidders: sliceutil.Clone(mulBid.Bidders), + Bidders: slices.Clone(mulBid.Bidders), TargetBidderCodePrefix: mulBid.TargetBidderCodePrefix, } if mulBid.MaxBids != nil { @@ -465,7 +465,7 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { clone.SChains = make([]*ExtRequestPrebidSChain, len(erp.SChains)) for i, schain := range erp.SChains { newChain := *schain - newNodes := sliceutil.Clone(schain.SChain.Nodes) + newNodes := slices.Clone(schain.SChain.Nodes) for j, node := range newNodes { if node.HP != nil { newNodes[j].HP = ptrutil.ToPtr(*newNodes[j].HP) @@ -483,13 +483,13 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { if erp.Targeting != nil { newTargeting := &ExtRequestTargeting{ IncludeFormat: erp.Targeting.IncludeFormat, - DurationRangeSec: sliceutil.Clone(erp.Targeting.DurationRangeSec), + DurationRangeSec: slices.Clone(erp.Targeting.DurationRangeSec), PreferDeals: erp.Targeting.PreferDeals, AppendBidderNames: erp.Targeting.AppendBidderNames, } if erp.Targeting.PriceGranularity != nil { newPriceGranularity := &PriceGranularity{ - Ranges: sliceutil.Clone(erp.Targeting.PriceGranularity.Ranges), + Ranges: slices.Clone(erp.Targeting.PriceGranularity.Ranges), } newPriceGranularity.Precision = ptrutil.Clone(erp.Targeting.PriceGranularity.Precision) newTargeting.PriceGranularity = newPriceGranularity @@ -504,7 +504,7 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { clone.Targeting = newTargeting } - clone.NoSale = sliceutil.Clone(erp.NoSale) + clone.NoSale = slices.Clone(erp.NoSale) if erp.AlternateBidderCodes != nil { newAlternateBidderCodes := ExtAlternateBidderCodes{Enabled: erp.AlternateBidderCodes.Enabled} @@ -513,7 +513,7 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { for key, val := range erp.AlternateBidderCodes.Bidders { newBidders[key] = ExtAdapterAlternateBidderCodes{ Enabled: val.Enabled, - AllowedBidderCodes: sliceutil.Clone(val.AllowedBidderCodes), + AllowedBidderCodes: slices.Clone(val.AllowedBidderCodes), } } newAlternateBidderCodes.Bidders = newBidders @@ -531,8 +531,8 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { for i, pfmg := range erp.Floors.Data.ModelGroups { clonedData.ModelGroups[i] = pfmg clonedData.ModelGroups[i].ModelWeight = ptrutil.Clone(pfmg.ModelWeight) - clonedData.ModelGroups[i].Schema.Fields = sliceutil.Clone(pfmg.Schema.Fields) - clonedData.ModelGroups[i].Values = maputil.Clone(pfmg.Values) + clonedData.ModelGroups[i].Schema.Fields = slices.Clone(pfmg.Schema.Fields) + clonedData.ModelGroups[i].Values = maps.Clone(pfmg.Values) } } clonedFloors.Data = &clonedData @@ -554,12 +554,12 @@ func (erp *ExtRequestPrebid) Clone() *ExtRequestPrebid { clone.MultiBidMap = make(map[string]ExtMultiBid, len(erp.MultiBidMap)) for k, v := range erp.MultiBidMap { // Make v a deep copy of the ExtMultiBid struct - v.Bidders = sliceutil.Clone(v.Bidders) + v.Bidders = slices.Clone(v.Bidders) v.MaxBids = ptrutil.Clone(v.MaxBids) clone.MultiBidMap[k] = v } } - clone.AdServerTargeting = sliceutil.Clone(erp.AdServerTargeting) + clone.AdServerTargeting = slices.Clone(erp.AdServerTargeting) return &clone } diff --git a/openrtb_ext/request_wrapper.go b/openrtb_ext/request_wrapper.go index 3567625a53b..27c20e98f35 100644 --- a/openrtb_ext/request_wrapper.go +++ b/openrtb_ext/request_wrapper.go @@ -3,12 +3,12 @@ package openrtb_ext import ( "encoding/json" "errors" + "maps" + "slices" "github.com/prebid/openrtb/v20/openrtb2" "github.com/prebid/prebid-server/v2/util/jsonutil" - "github.com/prebid/prebid-server/v2/util/maputil" "github.com/prebid/prebid-server/v2/util/ptrutil" - "github.com/prebid/prebid-server/v2/util/sliceutil" ) // RequestWrapper wraps the OpenRTB request to provide a storage location for unmarshalled ext fields, so they @@ -682,7 +682,7 @@ func (ue *UserExt) Clone() *UserExt { return nil } clone := *ue - clone.ext = maputil.Clone(ue.ext) + clone.ext = maps.Clone(ue.ext) if ue.consent != nil { clonedConsent := *ue.consent @@ -691,14 +691,14 @@ func (ue *UserExt) Clone() *UserExt { if ue.prebid != nil { clone.prebid = &ExtUserPrebid{} - clone.prebid.BuyerUIDs = maputil.Clone(ue.prebid.BuyerUIDs) + clone.prebid.BuyerUIDs = maps.Clone(ue.prebid.BuyerUIDs) } if ue.eids != nil { clonedEids := make([]openrtb2.EID, len(*ue.eids)) for i, eid := range *ue.eids { newEid := eid - newEid.UIDs = sliceutil.Clone(eid.UIDs) + newEid.UIDs = slices.Clone(eid.UIDs) clonedEids[i] = newEid } clone.eids = &clonedEids @@ -708,7 +708,7 @@ func (ue *UserExt) Clone() *UserExt { clone.consentedProvidersSettingsIn = &ConsentedProvidersSettingsIn{ConsentedProvidersString: ue.consentedProvidersSettingsIn.ConsentedProvidersString} } if ue.consentedProvidersSettingsOut != nil { - clone.consentedProvidersSettingsOut = &ConsentedProvidersSettingsOut{ConsentedProvidersList: sliceutil.Clone(ue.consentedProvidersSettingsOut.ConsentedProvidersList)} + clone.consentedProvidersSettingsOut = &ConsentedProvidersSettingsOut{ConsentedProvidersList: slices.Clone(ue.consentedProvidersSettingsOut.ConsentedProvidersList)} } return &clone @@ -859,7 +859,7 @@ func (re *RequestExt) Clone() *RequestExt { } clone := *re - clone.ext = maputil.Clone(re.ext) + clone.ext = maps.Clone(re.ext) if re.prebid != nil { clone.prebid = re.prebid.Clone() @@ -1005,7 +1005,7 @@ func (de *DeviceExt) Clone() *DeviceExt { } clone := *de - clone.ext = maputil.Clone(de.ext) + clone.ext = maps.Clone(de.ext) if de.prebid != nil { clonedPrebid := *de.prebid @@ -1119,7 +1119,7 @@ func (ae *AppExt) Clone() *AppExt { } clone := *ae - clone.ext = maputil.Clone(ae.ext) + clone.ext = maps.Clone(ae.ext) clone.prebid = ptrutil.Clone(ae.prebid) @@ -1185,7 +1185,7 @@ func (de *DOOHExt) Clone() *DOOHExt { } clone := *de - clone.ext = maputil.Clone(de.ext) + clone.ext = maps.Clone(de.ext) return &clone } @@ -1353,7 +1353,7 @@ func (re *RegExt) Clone() *RegExt { } clone := *re - clone.ext = maputil.Clone(re.ext) + clone.ext = maps.Clone(re.ext) clone.gdpr = ptrutil.Clone(re.gdpr) @@ -1449,7 +1449,7 @@ func (se *SiteExt) Clone() *SiteExt { } clone := *se - clone.ext = maputil.Clone(se.ext) + clone.ext = maps.Clone(se.ext) clone.amp = ptrutil.Clone(se.amp) return &clone @@ -1552,7 +1552,7 @@ func (se *SourceExt) Clone() *SourceExt { } clone := *se - clone.ext = maputil.Clone(se.ext) + clone.ext = maps.Clone(se.ext) clone.schain = cloneSupplyChain(se.schain) @@ -1791,7 +1791,7 @@ func (e *ImpExt) Clone() *ImpExt { } clone := *e - clone.ext = maputil.Clone(e.ext) + clone.ext = maps.Clone(e.ext) if e.prebid != nil { clonedPrebid := *e.prebid @@ -1805,7 +1805,7 @@ func (e *ImpExt) Clone() *ImpExt { } } clonedPrebid.IsRewardedInventory = ptrutil.Clone(e.prebid.IsRewardedInventory) - clonedPrebid.Bidder = maputil.Clone(e.prebid.Bidder) + clonedPrebid.Bidder = maps.Clone(e.prebid.Bidder) clonedPrebid.Options = ptrutil.Clone(e.prebid.Options) clonedPrebid.Floors = ptrutil.Clone(e.prebid.Floors) clone.prebid = &clonedPrebid diff --git a/ortb/clone.go b/ortb/clone.go index fa55cbe124f..1e34795e05a 100644 --- a/ortb/clone.go +++ b/ortb/clone.go @@ -1,9 +1,10 @@ package ortb import ( + "slices" + "github.com/prebid/openrtb/v20/openrtb2" "github.com/prebid/prebid-server/v2/util/ptrutil" - "github.com/prebid/prebid-server/v2/util/sliceutil" ) func CloneDataSlice(s []openrtb2.Data) []openrtb2.Data { @@ -25,7 +26,7 @@ func CloneData(s openrtb2.Data) openrtb2.Data { // Deep Copy (Pointers) s.Segment = CloneSegmentSlice(s.Segment) - s.Ext = sliceutil.Clone(s.Ext) + s.Ext = slices.Clone(s.Ext) return s } @@ -48,7 +49,7 @@ func CloneSegment(s openrtb2.Segment) openrtb2.Segment { // - Implicitly created by the cloned array. // Deep Copy (Pointers) - s.Ext = sliceutil.Clone(s.Ext) + s.Ext = slices.Clone(s.Ext) return s } @@ -62,11 +63,11 @@ func CloneUser(s *openrtb2.User) *openrtb2.User { c := *s // Deep Copy (Pointers) - c.KwArray = sliceutil.Clone(s.KwArray) + c.KwArray = slices.Clone(s.KwArray) c.Geo = CloneGeo(s.Geo) c.Data = CloneDataSlice(s.Data) c.EIDs = CloneEIDSlice(s.EIDs) - c.Ext = sliceutil.Clone(s.Ext) + c.Ext = slices.Clone(s.Ext) return &c } @@ -87,7 +88,7 @@ func CloneDevice(s *openrtb2.Device) *openrtb2.Device { c.JS = ptrutil.Clone(s.JS) c.GeoFetch = ptrutil.Clone(s.GeoFetch) c.ConnectionType = ptrutil.Clone(s.ConnectionType) - c.Ext = sliceutil.Clone(s.Ext) + c.Ext = slices.Clone(s.Ext) return &c } @@ -108,7 +109,7 @@ func CloneUserAgent(s *openrtb2.UserAgent) *openrtb2.UserAgent { mobileCopy := *s.Mobile c.Mobile = &mobileCopy } - s.Ext = sliceutil.Clone(s.Ext) + s.Ext = slices.Clone(s.Ext) return &c } @@ -136,8 +137,8 @@ func CloneBrandVersion(s *openrtb2.BrandVersion) *openrtb2.BrandVersion { c := *s // Deep Copy (Pointers) - c.Version = sliceutil.Clone(s.Version) - c.Ext = sliceutil.Clone(s.Ext) + c.Version = slices.Clone(s.Version) + c.Ext = slices.Clone(s.Ext) return &c } @@ -153,7 +154,7 @@ func CloneSource(s *openrtb2.Source) *openrtb2.Source { // Deep Copy (Pointers) c.FD = ptrutil.Clone(s.FD) c.SChain = CloneSChain(s.SChain) - c.Ext = sliceutil.Clone(s.Ext) + c.Ext = slices.Clone(s.Ext) return &c } @@ -168,7 +169,7 @@ func CloneSChain(s *openrtb2.SupplyChain) *openrtb2.SupplyChain { // Deep Copy (Pointers) c.Nodes = CloneSupplyChainNodes(s.Nodes) - c.Ext = sliceutil.Clone(s.Ext) + c.Ext = slices.Clone(s.Ext) return &c } @@ -192,7 +193,7 @@ func CloneSupplyChainNode(s openrtb2.SupplyChainNode) openrtb2.SupplyChainNode { // Deep Copy (Pointers) s.HP = ptrutil.Clone(s.HP) - s.Ext = sliceutil.Clone(s.Ext) + s.Ext = slices.Clone(s.Ext) return s } @@ -208,7 +209,7 @@ func CloneGeo(s *openrtb2.Geo) *openrtb2.Geo { // Deep Copy (Pointers) c.Lat = ptrutil.Clone(s.Lat) c.Lon = ptrutil.Clone(s.Lon) - c.Ext = sliceutil.Clone(s.Ext) + c.Ext = slices.Clone(s.Ext) return &c } @@ -232,7 +233,7 @@ func CloneEID(s openrtb2.EID) openrtb2.EID { // Deep Copy (Pointers) s.UIDs = CloneUIDSlice(s.UIDs) - s.Ext = sliceutil.Clone(s.Ext) + s.Ext = slices.Clone(s.Ext) return s } @@ -255,7 +256,7 @@ func CloneUID(s openrtb2.UID) openrtb2.UID { // - Implicitly created by the cloned array. // Deep Copy (Pointers) - s.Ext = sliceutil.Clone(s.Ext) + s.Ext = slices.Clone(s.Ext) return s } diff --git a/util/jsonutil/merge_test.go b/util/jsonutil/merge_test.go index 217a980e8aa..b4e58197be1 100644 --- a/util/jsonutil/merge_test.go +++ b/util/jsonutil/merge_test.go @@ -2,10 +2,9 @@ package jsonutil import ( "encoding/json" + "slices" "testing" - "github.com/prebid/prebid-server/v2/util/sliceutil" - "github.com/prebid/openrtb/v20/openrtb2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -353,8 +352,8 @@ func TestMergeCloneExt(t *testing.T) { for _, test := range testCases { t.Run(test.name, func(t *testing.T) { // copy original values to check at the end for no modification - originalExisting := sliceutil.Clone(test.givenExisting) - originalIncoming := sliceutil.Clone(test.givenIncoming) + originalExisting := slices.Clone(test.givenExisting) + originalIncoming := slices.Clone(test.givenIncoming) // build request request := &openrtb2.BidRequest{Ext: test.givenExisting} @@ -388,8 +387,8 @@ func TestMergeCloneExt(t *testing.T) { // assert no modifications // - can't use `assert.Same`` comparison checks since that's expected if // either existing or incoming are nil / omitted / empty. - assert.Equal(t, originalExisting, []byte(test.givenExisting), "existing") - assert.Equal(t, originalIncoming, []byte(test.givenIncoming), "incoming") + assert.Equal(t, originalExisting, test.givenExisting, "existing") + assert.Equal(t, originalIncoming, test.givenIncoming, "incoming") }) } } diff --git a/util/maputil/maputil.go b/util/maputil/maputil.go index 19224801a14..49df0772ba6 100644 --- a/util/maputil/maputil.go +++ b/util/maputil/maputil.go @@ -48,17 +48,3 @@ func HasElement(m map[string]interface{}, k ...string) bool { return exists } - -// Clone creates an independent copy of a map, -func Clone[K comparable, V any](m map[K]V) map[K]V { - if m == nil { - return nil - } - - clone := make(map[K]V, len(m)) - for key, value := range m { - clone[key] = value - } - - return clone -} diff --git a/util/maputil/maputil_test.go b/util/maputil/maputil_test.go index e7c067224c7..114c23327bd 100644 --- a/util/maputil/maputil_test.go +++ b/util/maputil/maputil_test.go @@ -245,29 +245,3 @@ func TestHasElement(t *testing.T) { assert.Equal(t, test.expected, result, test.description) } } - -func TestCloneMap(t *testing.T) { - // Test we handle nils properly - t.Run("NilMap", func(t *testing.T) { - var testMap, copyMap map[string]string = nil, nil // copyMap is a manual copy of testMap - clone := Clone(testMap) - testMap = map[string]string{"foo": "bar"} - assert.Equal(t, copyMap, clone) - }) - // Test a simple string map - t.Run("StringMap", func(t *testing.T) { - var testMap, copyMap map[string]string = map[string]string{"foo": "bar", "first": "one"}, map[string]string{"foo": "bar", "first": "one"} - clone := Clone(testMap) - testMap["foo"] = "baz" - testMap["bozo"] = "the clown" - assert.Equal(t, copyMap, clone) - }) - // Test a simple map[string]int - t.Run("StringInt", func(t *testing.T) { - var testMap, copyMap map[string]int = map[string]int{"foo": 1, "first": 2}, map[string]int{"foo": 1, "first": 2} - clone := Clone(testMap) - testMap["foo"] = 7 - testMap["bozo"] = 13 - assert.Equal(t, copyMap, clone) - }) -} diff --git a/util/sliceutil/clone.go b/util/sliceutil/clone.go deleted file mode 100644 index 64faea32a4e..00000000000 --- a/util/sliceutil/clone.go +++ /dev/null @@ -1,12 +0,0 @@ -package sliceutil - -func Clone[T any](s []T) []T { - if s == nil { - return nil - } - - c := make([]T, len(s), cap(s)) - copy(c, s) - - return c -} diff --git a/util/sliceutil/clone_test.go b/util/sliceutil/clone_test.go deleted file mode 100644 index 6e905b5cbe1..00000000000 --- a/util/sliceutil/clone_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package sliceutil - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestCloneSlice(t *testing.T) { - testCases := []struct { - name string - given []int - }{ - { - name: "nil", - given: nil, - }, - { - name: "empty", - given: []int{}, - }, - { - name: "one", - given: []int{1}, - }, - { - name: "many", - given: []int{1, 2}, - }, - } - - for _, test := range testCases { - t.Run(test.name, func(t *testing.T) { - result := Clone(test.given) - assert.Equal(t, test.given, result, "equality") - assert.NotSame(t, test.given, result, "pointer") - }) - } -}