-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoption.go
52 lines (44 loc) · 1.59 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package owl
import (
"context"
)
// Option is an option for New.
type Option interface {
Apply(context.Context) context.Context
}
// OptionFunc is a function that implements Option.
type OptionFunc func(context.Context) context.Context
func (f OptionFunc) Apply(ctx context.Context) context.Context {
return f(ctx)
}
// WithNamespace binds a namespace to the resolver. The namespace is used to
// lookup directive executors. There's a default namespace, which is used when
// the namespace is not specified. The namespace set in New() will be overridden
// by the namespace set in Resolve() or Scan().
func WithNamespace(ns *Namespace) Option {
return WithValue(ckNamespace, ns)
}
// WithNestedDirectivesEnabled controls whether to resolve nested directives.
// The default value is true. When set to false, the nested directives will not
// be executed. The value set in New() will be overridden by the value set in
// Resolve() or Scan().
func WithNestedDirectivesEnabled(resolve bool) Option {
return WithValue(ckResolveNestedDirectives, resolve)
}
// WithValue binds a value to the context.
//
// When used in New(), the value is bound to Resolver.Context.
//
// When used in Resolve() or Scan(), the value is bound to
// DirectiveRuntime.Context. See DirectiveRuntime.Context for more details.
func WithValue(key, value interface{}) Option {
return OptionFunc(func(ctx context.Context) context.Context {
return context.WithValue(ctx, key, value)
})
}
func buildContextWithOptionsApplied(ctx context.Context, opts ...Option) context.Context {
for _, opt := range opts {
ctx = opt.Apply(ctx)
}
return ctx
}