knative.dev/func-go@v0.21.3/cloudevents/mock/function.go (about)

     1  package mock
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/cloudevents/sdk-go/v2/event"
     7  )
     8  
     9  type Function struct {
    10  	OnStart  func(context.Context, map[string]string) error
    11  	OnStop   func(context.Context) error
    12  	OnHandle func(context.Context, event.Event) (*event.Event, error)
    13  }
    14  
    15  func (f *Function) Start(ctx context.Context, cfg map[string]string) error {
    16  	if f.OnStart != nil {
    17  		return f.OnStart(ctx, cfg)
    18  	}
    19  	return nil
    20  }
    21  
    22  func (f *Function) Stop(ctx context.Context) error {
    23  	if f.OnStop != nil {
    24  		return f.OnStop(ctx)
    25  	}
    26  	return nil
    27  }
    28  
    29  func (f *Function) Handle(ctx context.Context, event event.Event) (*event.Event, error) {
    30  	if f.OnHandle != nil {
    31  		return f.OnHandle(ctx, event)
    32  	}
    33  	return nil, nil
    34  }