Skip to content

Commit 18ffab9

Browse files
chore: replace tftypes.Nullable* helpers with builtins (#83)
### what replace `tftypes.Nullable*` helpers, since builtin functions can be used instead ### why avoid code duplication ### testing acceptance tests ### docs n/a
1 parent 0eaefef commit 18ffab9

19 files changed

+47
-100
lines changed

internal/api/binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type BindingExecutionConfigResourceLimits struct {
8787

8888
// BindingExecutionConfigResourceLimit holds resource limits for a binding execution config.
8989
type BindingExecutionConfigResourceLimit struct {
90-
MaxCount *int `json:"maxCount,omitempty"`
90+
MaxCount *int32 `json:"maxCount,omitempty"`
9191
MaxPercentage *float32 `json:"maxPercentage,omitempty"`
9292
RequiresBoth bool `json:"requiresBoth"`
9393
}

internal/api/types.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,3 @@ func StringsList(l types.List) []string {
6060
}
6161
return sl
6262
}
63-
64-
// NullableInt conerts a types.Int32 to an int that can be null.
65-
func NullableInt(i types.Int32) *int {
66-
if i.IsNull() {
67-
return nil
68-
}
69-
70-
v := int(i.ValueInt32())
71-
return &v
72-
}

internal/datasources/account.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ func (d *accountDataSource) Read(ctx context.Context, req datasource.ReadRequest
104104
data.ID = types.StringValue(account.ID)
105105
data.Key = types.StringValue(account.Key)
106106
data.Name = types.StringValue(account.Name)
107-
data.ShortName = tftypes.NullableString(account.ShortName)
108-
data.Description = tftypes.NullableString(account.Description)
107+
data.ShortName = types.StringPointerValue(account.ShortName)
108+
data.Description = types.StringPointerValue(account.Description)
109109
data.CloudProvider = types.StringValue(string(account.Provider))
110-
data.Path = tftypes.NullableString(account.Path)
111-
data.Email = tftypes.NullableString(account.Email)
112-
data.SecurityContext = tftypes.NullableString(account.SecurityContext)
110+
data.Path = types.StringPointerValue(account.Path)
111+
data.Email = types.StringPointerValue(account.Email)
112+
data.SecurityContext = types.StringPointerValue(account.SecurityContext)
113113
variablesString, err := tftypes.JSONString(account.Variables)
114114
if err != nil {
115115
errors.AddDiagAttributeError(&resp.Diagnostics, "variables", err)

internal/datasources/account_group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (d *accountGroupDataSource) Read(ctx context.Context, req datasource.ReadRe
9494
data.ID = types.StringValue(account_group.ID)
9595
data.UUID = types.StringValue(account_group.UUID)
9696
data.Name = types.StringValue(account_group.Name)
97-
data.Description = tftypes.NullableString(account_group.Description)
97+
data.Description = types.StringPointerValue(account_group.Description)
9898
data.CloudProvider = types.StringValue(account_group.Provider)
9999
data.Regions = tftypes.StringsList(account_group.Regions)
100100

internal/datasources/binding.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ func (d *bindingDataSource) Read(ctx context.Context, req datasource.ReadRequest
166166
data.ID = types.StringValue(binding.ID)
167167
data.UUID = types.StringValue(binding.UUID)
168168
data.Name = types.StringValue(binding.Name)
169-
data.Description = tftypes.NullableString(binding.Description)
169+
data.Description = types.StringPointerValue(binding.Description)
170170
data.AutoDeploy = types.BoolValue(binding.AutoDeploy)
171-
data.Schedule = tftypes.NullableString(binding.Schedule)
171+
data.Schedule = types.StringPointerValue(binding.Schedule)
172172
data.AccountGroupUUID = types.StringValue(binding.AccountGroup.UUID)
173173
data.PolicyCollectionUUID = types.StringValue(binding.PolicyCollection.UUID)
174174
data.System = types.BoolValue(binding.System)
175-
data.DryRun = tftypes.NullableBool(binding.DryRun())
176-
data.SecurityContext = tftypes.NullableString(binding.SecurityContext())
175+
data.DryRun = types.BoolPointerValue(binding.DryRun())
176+
data.SecurityContext = types.StringPointerValue(binding.SecurityContext())
177177

178178
variablesString, err := tftypes.JSONString(binding.ExecutionConfig.Variables)
179179
if err != nil {
@@ -188,8 +188,8 @@ func (d *bindingDataSource) Read(ctx context.Context, req datasource.ReadRequest
188188
defLimit,
189189
func() (*models.BindingExecutionConfigResourceLimit, diag.Diagnostics) {
190190
return &models.BindingExecutionConfigResourceLimit{
191-
MaxCount: tftypes.NullableInt(defLimit.MaxCount),
192-
MaxPercentage: tftypes.NullableFloat(defLimit.MaxPercentage),
191+
MaxCount: types.Int32PointerValue(defLimit.MaxCount),
192+
MaxPercentage: types.Float32PointerValue(defLimit.MaxPercentage),
193193
RequiresBoth: types.BoolValue(defLimit.RequiresBoth),
194194
}, nil
195195
},
@@ -205,8 +205,8 @@ func (d *bindingDataSource) Read(ctx context.Context, req datasource.ReadRequest
205205
func(entry api.BindingExecutionConfigResourceLimitsPolicyOverrides) (map[string]attr.Value, diag.Diagnostics) {
206206
return map[string]attr.Value{
207207
"policy_name": types.StringValue(entry.PolicyName),
208-
"max_count": tftypes.NullableInt(entry.Limit.MaxCount),
209-
"max_percentage": tftypes.NullableFloat(entry.Limit.MaxPercentage),
208+
"max_count": types.Int32PointerValue(entry.Limit.MaxCount),
209+
"max_percentage": types.Float32PointerValue(entry.Limit.MaxPercentage),
210210
"requires_both": types.BoolValue(entry.Limit.RequiresBoth),
211211
}, nil
212212
},

internal/datasources/platform.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (d *platformDataSource) Read(ctx context.Context, req datasource.ReadReques
125125
}
126126

127127
data.ID = types.StringValue(platform.ID)
128-
data.ExternalID = tftypes.NullableString(platform.ExternalID)
128+
data.ExternalID = types.StringPointerValue(platform.ExternalID)
129129
data.ExecutionRegions = tftypes.StringsList(platform.ExecutionRegions)
130130
awsAccountCustomerConfig, diags := d.getCustomerConfig(ctx, platform.AWSAccountCustomerConfig)
131131
resp.Diagnostics.Append(diags...)

internal/datasources/policy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/stacklet/terraform-provider-stacklet/internal/models"
1616
"github.com/stacklet/terraform-provider-stacklet/internal/providerdata"
1717
"github.com/stacklet/terraform-provider-stacklet/internal/schemavalidate"
18-
tftypes "github.com/stacklet/terraform-provider-stacklet/internal/types"
1918
)
2019

2120
var (
@@ -130,7 +129,7 @@ func (d *policyDataSource) Read(ctx context.Context, req datasource.ReadRequest,
130129
data.ID = types.StringValue(policy.ID)
131130
data.UUID = types.StringValue(policy.UUID)
132131
data.Name = types.StringValue(policy.Name)
133-
data.Description = tftypes.NullableString(policy.Description)
132+
data.Description = types.StringPointerValue(policy.Description)
134133
data.CloudProvider = types.StringValue(policy.Provider)
135134
data.Version = types.Int32Value(int32(policy.Version))
136135
category, diag := types.ListValueFrom(ctx, types.StringType, policy.Category)

internal/datasources/policy_collection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (d *policyCollectionDataSource) Read(ctx context.Context, req datasource.Re
125125
data.ID = types.StringValue(policyCollection.ID)
126126
data.UUID = types.StringValue(policyCollection.UUID)
127127
data.Name = types.StringValue(policyCollection.Name)
128-
data.Description = tftypes.NullableString(policyCollection.Description)
128+
data.Description = types.StringPointerValue(policyCollection.Description)
129129
data.CloudProvider = types.StringValue(string(policyCollection.Provider))
130130
data.AutoUpdate = types.BoolValue(policyCollection.AutoUpdate)
131131
data.System = types.BoolValue(policyCollection.System)

internal/datasources/repository.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/stacklet/terraform-provider-stacklet/internal/errors"
1414
"github.com/stacklet/terraform-provider-stacklet/internal/models"
1515
"github.com/stacklet/terraform-provider-stacklet/internal/providerdata"
16-
tftypes "github.com/stacklet/terraform-provider-stacklet/internal/types"
1716
)
1817

1918
// Ensure provider defined types fully satisfy framework interfaces.
@@ -129,10 +128,10 @@ func (d *repositoryDataSource) Read(ctx context.Context, req datasource.ReadRequ
129128
data.URL = types.StringValue(repo.URL)
130129
data.Name = types.StringValue(repo.Name)
131130
data.WebhookURL = types.StringValue(repo.WebhookURL)
132-
data.Description = tftypes.NullableString(repo.Description)
133-
data.AuthUser = tftypes.NullableString(repo.Auth.AuthUser)
131+
data.Description = types.StringPointerValue(repo.Description)
132+
data.AuthUser = types.StringPointerValue(repo.Auth.AuthUser)
134133
data.HasAuthToken = types.BoolValue(repo.Auth.HasAuthToken)
135-
data.SSHPublicKey = tftypes.NullableString(repo.Auth.SSHPublicKey)
134+
data.SSHPublicKey = types.StringPointerValue(repo.Auth.SSHPublicKey)
136135
data.HasSSHPrivateKey = types.BoolValue(repo.Auth.HasSshPrivateKey)
137136
data.HasSSHPassphrase = types.BoolValue(repo.Auth.HasSshPassphrase)
138137
data.System = types.BoolValue(repo.System)

internal/resources/account.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ func (r accountResource) updateAccountModel(m *models.AccountResource, account *
238238
m.ID = types.StringValue(account.ID)
239239
m.Key = types.StringValue(account.Key)
240240
m.Name = types.StringValue(account.Name)
241-
m.ShortName = tftypes.NullableString(account.ShortName)
241+
m.ShortName = types.StringPointerValue(account.ShortName)
242242
m.CloudProvider = types.StringValue(string(account.Provider))
243-
m.Description = tftypes.NullableString(account.Description)
244-
m.Path = tftypes.NullableString(account.Path)
245-
m.Email = tftypes.NullableString(account.Email)
246-
m.SecurityContext = tftypes.NullableString(account.SecurityContext)
243+
m.Description = types.StringPointerValue(account.Description)
244+
m.Path = types.StringPointerValue(account.Path)
245+
m.Email = types.StringPointerValue(account.Email)
246+
m.SecurityContext = types.StringPointerValue(account.SecurityContext)
247247
variablesString, err := tftypes.JSONString(account.Variables)
248248
if err != nil {
249249
errors.AddDiagAttributeError(&diags, "variables", err)

0 commit comments

Comments
 (0)