Skip to content

Commit

Permalink
chore: Remove convert.RefOf in favor of to.Ptr (#4193)
Browse files Browse the repository at this point in the history
Both `convert.RefOf` and `to.Ptr` do the same thing.  We invented
`convert.RefOf` while the Azure SDK team was adding `to.Ptr`.

Let's adopt `to.Ptr` and get rid of `convert.RefOf`, it's a shorter
name and used more across the Azure SDKs and samples.
  • Loading branch information
ellismg authored Aug 9, 2024
1 parent 33733fa commit a6a597f
Show file tree
Hide file tree
Showing 53 changed files with 401 additions and 417 deletions.
6 changes: 3 additions & 3 deletions cli/azd/internal/vsrpc/environment_service_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/infra"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
Expand Down Expand Up @@ -135,10 +135,10 @@ func (s *environmentService) refreshEnvironmentAsync(
resSvc := env.Services[svcIdx]

if len(resourceIds) > 0 {
resSvc.ResourceId = convert.RefOf(resourceIds[0])
resSvc.ResourceId = to.Ptr(resourceIds[0])
}

resSvc.Endpoint = convert.RefOf(s.serviceEndpoint(
resSvc.Endpoint = to.Ptr(s.serviceEndpoint(
ctx, subId, serviceConfig, c.resourceManager, c.serviceManager,
))
}
Expand Down
78 changes: 39 additions & 39 deletions cli/azd/pkg/account/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"github.com/azure/azure-dev/cli/azd/pkg/auth"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockarmresources"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockconfig"
Expand Down Expand Up @@ -538,22 +538,22 @@ func Test_HasDefaults(t *testing.T) {

var allTestSubscriptions []*armsubscriptions.Subscription = []*armsubscriptions.Subscription{
{
ID: convert.RefOf("subscriptions/SUBSCRIPTION_01"),
SubscriptionID: convert.RefOf("SUBSCRIPTION_01"),
DisplayName: convert.RefOf("Subscription 1"),
TenantID: convert.RefOf("TENANT_ID"),
ID: to.Ptr("subscriptions/SUBSCRIPTION_01"),
SubscriptionID: to.Ptr("SUBSCRIPTION_01"),
DisplayName: to.Ptr("Subscription 1"),
TenantID: to.Ptr("TENANT_ID"),
},
{
ID: convert.RefOf("subscriptions/SUBSCRIPTION_02"),
SubscriptionID: convert.RefOf("SUBSCRIPTION_02"),
DisplayName: convert.RefOf("Subscription 2"),
TenantID: convert.RefOf("TENANT_ID"),
ID: to.Ptr("subscriptions/SUBSCRIPTION_02"),
SubscriptionID: to.Ptr("SUBSCRIPTION_02"),
DisplayName: to.Ptr("Subscription 2"),
TenantID: to.Ptr("TENANT_ID"),
},
{
ID: convert.RefOf("subscriptions/SUBSCRIPTION_03"),
SubscriptionID: convert.RefOf("SUBSCRIPTION_03"),
DisplayName: convert.RefOf("Subscription 3"),
TenantID: convert.RefOf("TENANT_ID"),
ID: to.Ptr("subscriptions/SUBSCRIPTION_03"),
SubscriptionID: to.Ptr("SUBSCRIPTION_03"),
DisplayName: to.Ptr("Subscription 3"),
TenantID: to.Ptr("TENANT_ID"),
},
}

Expand All @@ -567,10 +567,10 @@ func setupGetSubscriptionMock(mockHttp *mockhttp.MockHttpClient, subscription *S
}

mockarmresources.MockGetSubscription(mockHttp, subscription.Id, armsubscriptions.Subscription{
ID: convert.RefOf(subscription.Id),
SubscriptionID: convert.RefOf(subscription.Id),
DisplayName: convert.RefOf(subscription.Name),
TenantID: convert.RefOf(subscription.TenantId),
ID: to.Ptr(subscription.Id),
SubscriptionID: to.Ptr(subscription.Id),
DisplayName: to.Ptr(subscription.Name),
TenantID: to.Ptr(subscription.TenantId),
})
}

Expand Down Expand Up @@ -615,8 +615,8 @@ func setupAccountMocks(mockHttp *mockhttp.MockHttpClient) {
mockarmresources.MockListTenants(mockHttp, armsubscriptions.TenantListResult{
Value: []*armsubscriptions.TenantIDDescription{
{
DisplayName: convert.RefOf("TENANT"),
TenantID: convert.RefOf("TENANT_ID"),
DisplayName: to.Ptr("TENANT"),
TenantID: to.Ptr("TENANT_ID"),
},
},
})
Expand All @@ -626,39 +626,39 @@ func setupAccountMocks(mockHttp *mockhttp.MockHttpClient) {
armsubscriptions.LocationListResult{
Value: []*armsubscriptions.Location{
{
ID: convert.RefOf("westus"),
Name: convert.RefOf("westus"),
DisplayName: convert.RefOf("West US"),
RegionalDisplayName: convert.RefOf("(US) West US"),
ID: to.Ptr("westus"),
Name: to.Ptr("westus"),
DisplayName: to.Ptr("West US"),
RegionalDisplayName: to.Ptr("(US) West US"),
Metadata: &armsubscriptions.LocationMetadata{
RegionType: convert.RefOf(armsubscriptions.RegionTypePhysical),
RegionType: to.Ptr(armsubscriptions.RegionTypePhysical),
},
},
{
ID: convert.RefOf("westus2"),
Name: convert.RefOf("westus2"),
DisplayName: convert.RefOf("West US 2"),
RegionalDisplayName: convert.RefOf("(US) West US 2"),
ID: to.Ptr("westus2"),
Name: to.Ptr("westus2"),
DisplayName: to.Ptr("West US 2"),
RegionalDisplayName: to.Ptr("(US) West US 2"),
Metadata: &armsubscriptions.LocationMetadata{
RegionType: convert.RefOf(armsubscriptions.RegionTypePhysical),
RegionType: to.Ptr(armsubscriptions.RegionTypePhysical),
},
},
{
ID: convert.RefOf("eastus"),
Name: convert.RefOf("eastus"),
DisplayName: convert.RefOf("East US"),
RegionalDisplayName: convert.RefOf("(US) East US"),
ID: to.Ptr("eastus"),
Name: to.Ptr("eastus"),
DisplayName: to.Ptr("East US"),
RegionalDisplayName: to.Ptr("(US) East US"),
Metadata: &armsubscriptions.LocationMetadata{
RegionType: convert.RefOf(armsubscriptions.RegionTypePhysical),
RegionType: to.Ptr(armsubscriptions.RegionTypePhysical),
},
},
{
ID: convert.RefOf("eastus2"),
Name: convert.RefOf("eastus2"),
DisplayName: convert.RefOf("East US 2"),
RegionalDisplayName: convert.RefOf("(US) East US 2"),
ID: to.Ptr("eastus2"),
Name: to.Ptr("eastus2"),
DisplayName: to.Ptr("East US 2"),
RegionalDisplayName: to.Ptr("(US) East US 2"),
Metadata: &armsubscriptions.LocationMetadata{
RegionType: convert.RefOf(armsubscriptions.RegionTypePhysical),
RegionType: to.Ptr(armsubscriptions.RegionTypePhysical),
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions cli/azd/pkg/account/subscriptions_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"sort"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockarmresources"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockhttp"
Expand All @@ -37,7 +37,7 @@ func TestSubscriptionsManager_ListSubscriptions(t *testing.T) {
args: args{
principalInfo: &principalInfoProviderMock{
GetLoggedInServicePrincipalTenantIDFunc: func(context.Context) (*string, error) {
return convert.RefOf("TENANT_ID_1"), nil
return to.Ptr("TENANT_ID_1"), nil
},
},
tenants: generateTenants(1),
Expand Down Expand Up @@ -180,9 +180,9 @@ func generateTenants(total int) []*armsubscriptions.TenantIDDescription {
results := make([]*armsubscriptions.TenantIDDescription, 0, total)
for i := 1; i <= total; i++ {
results = append(results, &armsubscriptions.TenantIDDescription{
DisplayName: convert.RefOf(fmt.Sprintf("TENANT_%d", i)),
TenantID: convert.RefOf(fmt.Sprintf("TENANT_ID_%d", i)),
DefaultDomain: convert.RefOf(fmt.Sprintf("TENANT_DOMAIN_%d", i)),
DisplayName: to.Ptr(fmt.Sprintf("TENANT_%d", i)),
TenantID: to.Ptr(fmt.Sprintf("TENANT_ID_%d", i)),
DefaultDomain: to.Ptr(fmt.Sprintf("TENANT_DOMAIN_%d", i)),
})
}
return results
Expand All @@ -205,9 +205,9 @@ func generateSubscriptions(total int, tenantIDs ...string) map[string][]*armsubs
subs := make([]*armsubscriptions.Subscription, 0, total)
for i := 1; i <= total; i++ {
subs = append(subs, &armsubscriptions.Subscription{
ID: convert.RefOf(fmt.Sprintf("subscriptions/SUBSCRIPTION_%d", i)),
SubscriptionID: convert.RefOf(fmt.Sprintf("SUBSCRIPTION_%d_%s", i, tenantID)),
DisplayName: convert.RefOf(fmt.Sprintf("Subscription %d (%s)", i, tenantID)),
ID: to.Ptr(fmt.Sprintf("subscriptions/SUBSCRIPTION_%d", i)),
SubscriptionID: to.Ptr(fmt.Sprintf("SUBSCRIPTION_%d_%s", i, tenantID)),
DisplayName: to.Ptr(fmt.Sprintf("Subscription %d (%s)", i, tenantID)),
TenantID: &tenantID,
})
}
Expand Down
4 changes: 2 additions & 2 deletions cli/azd/pkg/auth/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
_ "embed"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
"github.com/azure/azure-dev/cli/azd/pkg/cloud"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/pkg/github"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockinput"
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestAuthFileConfigUpgrade(t *testing.T) {
userCfgMgr := newMemoryUserConfigManager()

err := userCfg.Set(cCurrentUserKey, &userProperties{
HomeAccountID: convert.RefOf("homeAccountID"),
HomeAccountID: to.Ptr("homeAccountID"),
})
require.NoError(t, err)

Expand Down
10 changes: 5 additions & 5 deletions cli/azd/pkg/azsdk/correlation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/azure/azure-dev/cli/azd/test/mocks/mockhttp"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -42,7 +42,7 @@ func Test_simpleCorrelationPolicy_Do(t *testing.T) {
context.Background(),
trace.SpanContext{}.WithTraceID(traceId),
),
expect: convert.RefOf(traceId.String()),
expect: to.Ptr(traceId.String()),
headerName: cMsCorrelationIdHeader,
correlationPolicyFunc: NewMsCorrelationPolicy,
},
Expand All @@ -53,7 +53,7 @@ func Test_simpleCorrelationPolicy_Do(t *testing.T) {
context.Background(),
trace.SpanContext{}.WithTraceID(invalidTraceId),
),
expect: convert.RefOf(""),
expect: to.Ptr(""),
headerName: cMsCorrelationIdHeader,
correlationPolicyFunc: NewMsCorrelationPolicy,
},
Expand All @@ -70,7 +70,7 @@ func Test_simpleCorrelationPolicy_Do(t *testing.T) {
context.Background(),
trace.SpanContext{}.WithTraceID(traceId),
),
expect: convert.RefOf(traceId.String()),
expect: to.Ptr(traceId.String()),
headerName: cMsGraphCorrelationIdHeader,
correlationPolicyFunc: NewMsGraphCorrelationPolicy,
},
Expand All @@ -81,7 +81,7 @@ func Test_simpleCorrelationPolicy_Do(t *testing.T) {
context.Background(),
trace.SpanContext{}.WithTraceID(invalidTraceId),
),
expect: convert.RefOf(""),
expect: to.Ptr(""),
headerName: cMsGraphCorrelationIdHeader,
correlationPolicyFunc: NewMsGraphCorrelationPolicy,
},
Expand Down
4 changes: 2 additions & 2 deletions cli/azd/pkg/azsdk/zip_deploy_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -105,7 +105,7 @@ func registerPollingMocks(mockContext *mocks.MockContext) {
Status: http.StatusAccepted,
StatusText: "Accepted",
Message: "Doing deploy things",
Progress: convert.RefOf("Running ORYX build"),
Progress: to.Ptr("Running ORYX build"),
Complete: false,
Active: false,
SiteName: "APP_NAME",
Expand Down
4 changes: 2 additions & 2 deletions cli/azd/pkg/azure/resource_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"regexp"
"strings"

"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

// SubscriptionFromRID returns the subscription id component of a resource or panics if the resource id does not
Expand Down Expand Up @@ -109,5 +109,5 @@ func GetResourceGroupName(resourceId string) *string {
return nil
}

return convert.RefOf(string(matches[1]))
return to.Ptr(string(matches[1]))
}
14 changes: 7 additions & 7 deletions cli/azd/pkg/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package compare
import (
"testing"

"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func Test_StringUtil_IsStringNilOrEmpty(t *testing.T) {
Expand All @@ -19,17 +19,17 @@ func Test_StringUtil_IsStringNilOrEmpty(t *testing.T) {
},
{
name: "empty",
value: convert.RefOf(""),
value: to.Ptr(""),
want: true,
},
{
name: "whitespace",
value: convert.RefOf(" "),
value: to.Ptr(" "),
want: true,
},
{
name: "non-empty",
value: convert.RefOf("foo"),
value: to.Ptr("foo"),
want: false,
},
}
Expand Down Expand Up @@ -57,19 +57,19 @@ func Test_StringUtil_PtrValueEquals(t *testing.T) {
},
{
name: "empty",
actual: convert.RefOf(""),
actual: to.Ptr(""),
expected: "foo",
want: false,
},
{
name: "whitespace",
actual: convert.RefOf(" "),
actual: to.Ptr(" "),
expected: "foo",
want: false,
},
{
name: "non-empty",
actual: convert.RefOf("foo"),
actual: to.Ptr("foo"),
expected: "foo",
want: true,
},
Expand Down
8 changes: 4 additions & 4 deletions cli/azd/pkg/containerapps/container_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcontainers/armappcontainers/v3"
azdinternal "github.com/azure/azure-dev/cli/azd/internal"
"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/alpha"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/pkg/httputil"
"github.com/benbjohnson/clock"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -292,8 +292,8 @@ func (cas *containerAppService) AddRevision(

// Update the revision with the new image name and suffix
revision := revisionResponse.Revision
revision.Properties.Template.RevisionSuffix = convert.RefOf(fmt.Sprintf("azd-%d", cas.clock.Now().Unix()))
revision.Properties.Template.Containers[0].Image = convert.RefOf(imageName)
revision.Properties.Template.RevisionSuffix = to.Ptr(fmt.Sprintf("azd-%d", cas.clock.Now().Unix()))
revision.Properties.Template.Containers[0].Image = to.Ptr(imageName)

// Update the container app with the new revision
containerApp.Properties.Template = revision.Properties.Template
Expand Down Expand Up @@ -390,7 +390,7 @@ func (cas *containerAppService) setTrafficWeights(
containerApp.Properties.Configuration.Ingress.Traffic = []*armappcontainers.TrafficWeight{
{
RevisionName: &revisionName,
Weight: convert.RefOf[int32](100),
Weight: to.Ptr[int32](100),
},
}

Expand Down
Loading

0 comments on commit a6a597f

Please sign in to comment.