Skip to content

Commit 950bf82

Browse files
committed
Fix potential nil pointer errors in cloudflare_worker_version
1 parent 7358f25 commit 950bf82

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

internal/services/worker_version/resource.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,26 +169,30 @@ func (r *WorkerVersionResource) Read(ctx context.Context, req resource.ReadReque
169169
data.Assets = assets
170170

171171
// Refresh content_sha256 on each module
172-
moduleNameMap := make(map[string]*WorkerVersionModulesModel, len(*stateModules))
173-
for _, mod := range *stateModules {
174-
moduleNameMap[mod.Name.ValueString()] = mod
175-
}
176-
for _, mod := range *data.Modules {
177-
contentBase64 := mod.ContentBase64.ValueString()
178-
content, err := base64.StdEncoding.DecodeString(contentBase64)
179-
if err != nil {
180-
resp.Diagnostics.AddError("Refresh Error", err.Error())
181-
return
182-
}
183-
contentSHA256, err := calculateStringHash(string(content))
184-
if err != nil {
185-
resp.Diagnostics.AddError("Refresh Error", err.Error())
186-
return
172+
moduleNameMap := make(map[string]*WorkerVersionModulesModel)
173+
if stateModules != nil {
174+
for _, mod := range *stateModules {
175+
moduleNameMap[mod.Name.ValueString()] = mod
187176
}
177+
}
178+
if data.Modules != nil {
179+
for _, mod := range *data.Modules {
180+
contentBase64 := mod.ContentBase64.ValueString()
181+
content, err := base64.StdEncoding.DecodeString(contentBase64)
182+
if err != nil {
183+
resp.Diagnostics.AddError("Refresh Error", err.Error())
184+
return
185+
}
186+
contentSHA256, err := calculateStringHash(string(content))
187+
if err != nil {
188+
resp.Diagnostics.AddError("Refresh Error", err.Error())
189+
return
190+
}
188191

189-
mod.ContentSHA256 = types.StringValue(contentSHA256)
190-
if stateMod, ok := moduleNameMap[mod.Name.ValueString()]; ok {
191-
mod.ContentFile = stateMod.ContentFile
192+
mod.ContentSHA256 = types.StringValue(contentSHA256)
193+
if stateMod, ok := moduleNameMap[mod.Name.ValueString()]; ok {
194+
mod.ContentFile = stateMod.ContentFile
195+
}
192196
}
193197
}
194198

0 commit comments

Comments
 (0)