MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / Invoke

Method Invoke

db/functions/function.go:146–197  ·  view source on GitHub ↗

Creates an Invocation of a UserFunction.

(database *db.Database, args map[string]any, mutationAllowed bool, ctx context.Context)

Source from the content-addressed store, hash-verified

144
145// Creates an Invocation of a UserFunction.
146func (fn *functionImpl) Invoke(database *db.Database, args map[string]any, mutationAllowed bool, ctx context.Context) (db.UserFunctionInvocation, error) {
147 if ctx == nil {
148 return nil, fmt.Errorf("missing context to UserFunction.Invoke")
149 }
150
151 if err := db.CheckTimeout(ctx); err != nil {
152 return nil, err
153 }
154
155 if args == nil {
156 args = map[string]any{}
157 }
158 if fn.checkArgs {
159 if err := fn.checkArguments(args); err != nil {
160 return nil, err
161 }
162 }
163
164 // Check that the user is authorized:
165 if fn.Allow != nil || !fn.allowByDefault {
166 if err := fn.authorize(database.User(), args); err != nil {
167 return nil, err
168 }
169 }
170
171 if (!fn.Mutating || !mutationAllowed) && ctx.Value(readOnlyKey) == nil {
172 // Add a value to the Context to indicate that mutations aren't allowed:
173 var why string
174 if !fn.Mutating {
175 why = fmt.Sprintf("%s %q", fn.typeName, fn.name)
176 } else {
177 why = "a read-only API call"
178 }
179 ctx = context.WithValue(ctx, readOnlyKey, why)
180 }
181
182 if fn.isN1QL() {
183 return &n1qlInvocation{
184 functionImpl: fn,
185 db: database,
186 args: args,
187 ctx: ctx,
188 }, nil
189 } else {
190 return &jsInvocation{
191 functionImpl: fn,
192 db: database,
193 ctx: ctx,
194 args: args,
195 }, nil
196 }
197}
198
199//////// FUNCTION PARAMETERS/ARGUMENTS:
200

Callers

nothing calls this directly

Calls 7

checkArgumentsMethod · 0.95
authorizeMethod · 0.95
isN1QLMethod · 0.95
CheckTimeoutFunction · 0.92
ErrorfMethod · 0.80
UserMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected