Task creates a Task that will invoke the function. Its parameters may be tweaked before adding it to a queue. Users should not modify the Path or Payload fields of the returned Task.
(args ...interface{})
| 230 | // Its parameters may be tweaked before adding it to a queue. |
| 231 | // Users should not modify the Path or Payload fields of the returned Task. |
| 232 | func (f *Function) Task(args ...interface{}) (*taskqueue.Task, error) { |
| 233 | if f.err != nil { |
| 234 | return nil, fmt.Errorf("delay: func is invalid: %v", f.err) |
| 235 | } |
| 236 | |
| 237 | nArgs := len(args) + 1 // +1 for the context.Context |
| 238 | ft := f.fv.Type() |
| 239 | minArgs := ft.NumIn() |
| 240 | if ft.IsVariadic() { |
| 241 | minArgs-- |
| 242 | } |
| 243 | if nArgs < minArgs { |
| 244 | return nil, fmt.Errorf("delay: too few arguments to func: %d < %d", nArgs, minArgs) |
| 245 | } |
| 246 | if !ft.IsVariadic() && nArgs > minArgs { |
| 247 | return nil, fmt.Errorf("delay: too many arguments to func: %d > %d", nArgs, minArgs) |
| 248 | } |
| 249 | |
| 250 | // Check arg types. |
| 251 | for i := 1; i < nArgs; i++ { |
| 252 | at := reflect.TypeOf(args[i-1]) |
| 253 | var dt reflect.Type |
| 254 | if i < minArgs { |
| 255 | // not a variadic arg |
| 256 | dt = ft.In(i) |
| 257 | } else { |
| 258 | // a variadic arg |
| 259 | dt = ft.In(minArgs).Elem() |
| 260 | } |
| 261 | // nil arguments won't have a type, so they need special handling. |
| 262 | if at == nil { |
| 263 | // nil interface |
| 264 | switch dt.Kind() { |
| 265 | case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| 266 | continue // may be nil |
| 267 | } |
| 268 | return nil, fmt.Errorf("delay: argument %d has wrong type: %v is not nilable", i, dt) |
| 269 | } |
| 270 | switch at.Kind() { |
| 271 | case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| 272 | av := reflect.ValueOf(args[i-1]) |
| 273 | if av.IsNil() { |
| 274 | // nil value in interface; not supported by gob, so we replace it |
| 275 | // with a nil interface value |
| 276 | args[i-1] = nil |
| 277 | } |
| 278 | } |
| 279 | if !at.AssignableTo(dt) { |
| 280 | return nil, fmt.Errorf("delay: argument %d has wrong type: %v is not assignable to %v", i, at, dt) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | inv := invocation{ |
| 285 | Key: f.key, |
| 286 | Args: args, |
| 287 | } |
| 288 | |
| 289 | buf := new(bytes.Buffer) |