44 "sort"
55
66 "github.com/launchdarkly/go-sdk-common/v3/lderrors"
7+
8+ "golang.org/x/exp/slices"
79)
810
911const defaultMultiBuilderCapacity = 3 // arbitrary value based on presumed likely use cases
@@ -17,10 +19,10 @@ const defaultMultiBuilderCapacity = 3 // arbitrary value based on presumed likel
1719// nested Context for each Kind. MultiBuilder setters return a reference the same builder, so they
1820// can be chained together:
1921//
20- // context := ldcontext.NewMultiBuilder().
21- // Add(ldcontext.New("my-user-key")).
22- // Add(ldcontext.NewBuilder("my-org-key").Kind("organization").Name("Org1").Build()).
23- // Build()
22+ // context := ldcontext.NewMultiBuilder().
23+ // Add(ldcontext.New("my-user-key")).
24+ // Add(ldcontext.NewBuilder("my-org-key").Kind("organization").Name("Org1").Build()).
25+ // Build()
2426//
2527// A MultiBuilder should not be accessed by multiple goroutines at once. Once you have called Build(),
2628// the resulting Context is immutable and is safe to use from multiple goroutines. Instances
@@ -128,23 +130,23 @@ func (m *MultiBuilder) Build() Context {
128130// detected by calling the context's Err() method. But, if you prefer to use the two-value
129131// pattern that is common in Go, you can call TryBuild instead:
130132//
131- // c, err := ldcontext.NewMultiBuilder().
132- // Add(context1).Add(context2).
133- // TryBuild()
134- // if err != nil {
135- // // do whatever is appropriate if building the context failed
136- // }
133+ // c, err := ldcontext.NewMultiBuilder().
134+ // Add(context1).Add(context2).
135+ // TryBuild()
136+ // if err != nil {
137+ // // do whatever is appropriate if building the context failed
138+ // }
137139//
138140// The two return values are the same as to 1. the Context that would be returned by Build(),
139141// and 2. the result of calling Err() on that Context. So, the above example is exactly
140142// equivalent to:
141143//
142- // c := ldcontext.NewMultiBuilder().
143- // Add(context1).Add(context2).
144- // Build()
145- // if c.Err() != nil {
146- // // do whatever is appropriate if building the context failed
147- // }
144+ // c := ldcontext.NewMultiBuilder().
145+ // Add(context1).Add(context2).
146+ // Build()
147+ // if c.Err() != nil {
148+ // // do whatever is appropriate if building the context failed
149+ // }
148150//
149151// Note that unlike some Go methods where the first return value is normally an
150152// uninitialized zero value if the error is non-nil, the Context returned by TryBuild in case
@@ -164,17 +166,17 @@ func (m *MultiBuilder) TryBuild() (Context, error) {
164166// individual kinds from it separately. For instance, in the following example, "multi1" and
165167// "multi2" end up being exactly the same:
166168//
167- // c1 := ldcontext.NewWithKind("kind1", "key1")
168- // c2 := ldcontext.NewWithKind("kind2", "key2")
169- // c3 := ldcontext.NewWithKind("kind3", "key3")
169+ // c1 := ldcontext.NewWithKind("kind1", "key1")
170+ // c2 := ldcontext.NewWithKind("kind2", "key2")
171+ // c3 := ldcontext.NewWithKind("kind3", "key3")
170172//
171- // multi1 := ldcontext.NewMultiBuilder().Add(c1).Add(c2).Add(c3).Build()
173+ // multi1 := ldcontext.NewMultiBuilder().Add(c1).Add(c2).Add(c3).Build()
172174//
173- // c1plus2 := ldcontext.NewMultiBuilder().Add(c1).Add(c2).Build()
174- // multi2 := ldcontext.NewMultiBuilder().Add(c1plus2).Add(c3).Build()
175+ // c1plus2 := ldcontext.NewMultiBuilder().Add(c1).Add(c2).Build()
176+ // multi2 := ldcontext.NewMultiBuilder().Add(c1plus2).Add(c3).Build()
175177func (m * MultiBuilder ) Add (context Context ) * MultiBuilder {
176178 if m .contextsCopyOnWrite {
177- m .contexts = append ([] Context ( nil ), m .contexts ... )
179+ m .contexts = slices . Clone ( m .contexts )
178180 m .contextsCopyOnWrite = true
179181 }
180182 if context .Multiple () {
0 commit comments