Skip to content

Commit 74aeaa1

Browse files
authored
feat: add With methods to customize CoreAttribute characteristics (#195)
Allow users to customize type-independent characteristics of pre-built schema attributes without rebuilding them from scratch. This enables use cases like making "emails" required in CoreUserSchema while reusing the existing attribute definition. Added methods: WithDescription, WithMutability, WithRequired, WithReturned. All return a copy, leaving the original unmodified.
1 parent fafae71 commit 74aeaa1

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

schema/core.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,30 @@ func (a CoreAttribute) ValidateSingular(attribute interface{}) (interface{}, *er
291291
}
292292
}
293293

294+
// WithDescription returns a copy of the attribute with the given description.
295+
func (a CoreAttribute) WithDescription(description optional.String) CoreAttribute {
296+
a.description = description
297+
return a
298+
}
299+
300+
// WithMutability returns a copy of the attribute with the given mutability.
301+
func (a CoreAttribute) WithMutability(mutability AttributeMutability) CoreAttribute {
302+
a.mutability = mutability.m
303+
return a
304+
}
305+
306+
// WithRequired returns a copy of the attribute with the given required value.
307+
func (a CoreAttribute) WithRequired(required bool) CoreAttribute {
308+
a.required = required
309+
return a
310+
}
311+
312+
// WithReturned returns a copy of the attribute with the given returned value.
313+
func (a CoreAttribute) WithReturned(returned AttributeReturned) CoreAttribute {
314+
a.returned = returned.r
315+
return a
316+
}
317+
294318
func (a *CoreAttribute) getRawAttributes() map[string]interface{} {
295319
attributes := map[string]interface{}{
296320
"description": a.description.Value(),

schema/core_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package schema
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/elimity-com/scim/optional"
8+
)
9+
10+
func ExampleCoreAttribute_WithRequired() {
11+
// Customize the pre-built CoreUserSchema to make "emails" required.
12+
userSchema := CoreUserSchema()
13+
for i, attr := range userSchema.Attributes {
14+
if attr.Name() == "emails" {
15+
userSchema.Attributes[i] = attr.WithRequired(true)
16+
}
17+
}
18+
19+
emails, _ := userSchema.Attributes.ContainsAttribute("emails")
20+
fmt.Println(emails.Required())
21+
// Output: true
22+
}
23+
24+
func TestCoreAttribute_WithDescription(t *testing.T) {
25+
attr := SimpleCoreAttribute(SimpleStringParams(StringParams{
26+
Name: "test",
27+
}))
28+
desc := optional.NewString("new description")
29+
got := attr.WithDescription(desc)
30+
if got.Description() != "new description" {
31+
t.Errorf("WithDescription: got %q, want %q", got.Description(), "new description")
32+
}
33+
if attr.Description() != "" {
34+
t.Error("WithDescription modified the original attribute")
35+
}
36+
}
37+
38+
func TestCoreAttribute_WithMutability(t *testing.T) {
39+
attr := SimpleCoreAttribute(SimpleStringParams(StringParams{
40+
Name: "test",
41+
}))
42+
got := attr.WithMutability(AttributeMutabilityImmutable())
43+
if got.Mutability() != "immutable" {
44+
t.Errorf("WithMutability: got %q, want %q", got.Mutability(), "immutable")
45+
}
46+
if attr.Mutability() != "readWrite" {
47+
t.Error("WithMutability modified the original attribute")
48+
}
49+
}
50+
51+
func TestCoreAttribute_WithRequired(t *testing.T) {
52+
attr := SimpleCoreAttribute(SimpleStringParams(StringParams{
53+
Name: "test",
54+
}))
55+
got := attr.WithRequired(true)
56+
if !got.Required() {
57+
t.Error("WithRequired(true): got false, want true")
58+
}
59+
if attr.Required() {
60+
t.Error("WithRequired modified the original attribute")
61+
}
62+
}
63+
64+
func TestCoreAttribute_WithReturned(t *testing.T) {
65+
attr := SimpleCoreAttribute(SimpleStringParams(StringParams{
66+
Name: "test",
67+
}))
68+
got := attr.WithReturned(AttributeReturnedNever())
69+
if got.Returned() != "never" {
70+
t.Errorf("WithReturned: got %q, want %q", got.Returned(), "never")
71+
}
72+
if attr.Returned() != "default" {
73+
t.Error("WithReturned modified the original attribute")
74+
}
75+
}

0 commit comments

Comments
 (0)