Skip to content

Commit b7ff021

Browse files
cmdaltentacch
authored andcommitted
chore(branch-protection): add RequiresTrueIfConfigured for sets
Signed-off-by: Martin Weissbach <martin.weissbach@sva.de>
1 parent 68d25fd commit b7ff021

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

internal/provider/branch_protection_resource.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
forgejoBoolValidator "terraform-provider-forgejo/internal/provider/boolvalidator"
3131
forgejoListValidator "terraform-provider-forgejo/internal/provider/listvalidator"
32+
forgejoSetValidator "terraform-provider-forgejo/internal/provider/setvalidator"
3233
)
3334

3435
// Ensure the implementation satisfies the expected interfaces.
@@ -131,7 +132,7 @@ func (r *branchProtectionResource) Schema(ctx context.Context, req resource.Sche
131132
setvalidator.AlsoRequires(path.Expressions{
132133
path.MatchRoot("enable_push_whitelist"),
133134
}...),
134-
forgejoListValidator.RequiresTrueIfConfigured(path.Expressions{
135+
forgejoSetValidator.RequiresTrueIfConfigured(path.Expressions{
135136
path.MatchRoot("enable_push_whitelist"),
136137
}...),
137138
},
@@ -151,7 +152,7 @@ func (r *branchProtectionResource) Schema(ctx context.Context, req resource.Sche
151152
setvalidator.AlsoRequires(path.Expressions{
152153
path.MatchRoot("enable_push_whitelist"),
153154
}...),
154-
forgejoListValidator.RequiresTrueIfConfigured(path.Expressions{
155+
forgejoSetValidator.RequiresTrueIfConfigured(path.Expressions{
155156
path.MatchRoot("enable_push_whitelist"),
156157
}...),
157158
},
@@ -235,7 +236,7 @@ func (r *branchProtectionResource) Schema(ctx context.Context, req resource.Sche
235236
setvalidator.AlsoRequires(path.Expressions{
236237
path.MatchRoot("enable_merge_whitelist"),
237238
}...),
238-
forgejoListValidator.RequiresTrueIfConfigured(path.Expressions{
239+
forgejoSetValidator.RequiresTrueIfConfigured(path.Expressions{
239240
path.MatchRoot("enable_merge_whitelist"),
240241
}...),
241242
},
@@ -255,7 +256,7 @@ func (r *branchProtectionResource) Schema(ctx context.Context, req resource.Sche
255256
setvalidator.AlsoRequires(path.Expressions{
256257
path.MatchRoot("enable_merge_whitelist"),
257258
}...),
258-
forgejoListValidator.RequiresTrueIfConfigured(path.Expressions{
259+
forgejoSetValidator.RequiresTrueIfConfigured(path.Expressions{
259260
path.MatchRoot("enable_merge_whitelist"),
260261
}...),
261262
},
@@ -281,7 +282,7 @@ func (r *branchProtectionResource) Schema(ctx context.Context, req resource.Sche
281282
setvalidator.AlsoRequires(path.Expressions{
282283
path.MatchRoot("enable_approvals_whitelist"),
283284
}...),
284-
forgejoListValidator.RequiresTrueIfConfigured(path.Expressions{
285+
forgejoSetValidator.RequiresTrueIfConfigured(path.Expressions{
285286
path.MatchRoot("enable_approvals_whitelist"),
286287
}...),
287288
},
@@ -301,7 +302,7 @@ func (r *branchProtectionResource) Schema(ctx context.Context, req resource.Sche
301302
setvalidator.AlsoRequires(path.Expressions{
302303
path.MatchRoot("enable_approvals_whitelist"),
303304
}...),
304-
forgejoListValidator.RequiresTrueIfConfigured(path.Expressions{
305+
forgejoSetValidator.RequiresTrueIfConfigured(path.Expressions{
305306
path.MatchRoot("enable_approvals_whitelist"),
306307
}...),
307308
},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package setvalidator
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/path"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
7+
"terraform-provider-forgejo/internal/schemavalidator"
8+
)
9+
10+
// RequiresTrueIfConfigured checks that any Bool values in the paths described by the
11+
// path.Expression are true if the current attribute value is configured to a non-empty
12+
// list.
13+
func RequiresTrueIfConfigured(expressions ...path.Expression) validator.Set {
14+
return &schemavalidator.RequiresTrueIfConfiguredValidator{
15+
Expressions: expressions,
16+
}
17+
}

internal/schemavalidator/require_true_if_configured.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
var (
1717
_ validator.Bool = &RequiresTrueIfConfiguredValidator{}
1818
_ validator.List = &RequiresTrueIfConfiguredValidator{}
19+
_ validator.Set = &RequiresTrueIfConfiguredValidator{}
1920
)
2021

2122
// RequiresTrueIfConfiguredValidator is the underlying type implementing RequiresTrueIfConfigured.
@@ -56,6 +57,28 @@ func (v RequiresTrueIfConfiguredValidator) ValidateList(ctx context.Context, req
5657
v.validate(ctx, validateReq, &resp.Diagnostics, fmt.Sprintf("If %s is not empty, %%s must also be 'true'", req.Path))
5758
}
5859

60+
func (v RequiresTrueIfConfiguredValidator) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
61+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
62+
return
63+
}
64+
65+
opts := basetypes.CollectionLengthOptions{
66+
UnhandledNullAsZero: true,
67+
UnhandledUnknownAsZero: true,
68+
}
69+
if req.ConfigValue.Length(opts) == 0 {
70+
return
71+
}
72+
73+
validateReq := requiresTrueIfConfiguredValidatorRequest{
74+
Config: req.Config,
75+
Path: req.Path,
76+
PathExpression: req.PathExpression,
77+
}
78+
79+
v.validate(ctx, validateReq, &resp.Diagnostics, fmt.Sprintf("If %s is not empty, %%s must also be 'true'", req.Path))
80+
}
81+
5982
func (v RequiresTrueIfConfiguredValidator) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
6083
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
6184
return

0 commit comments

Comments
 (0)