|
| 1 | +/* |
| 2 | + * Copyright 2024 DefFunction Stream Org. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package gofs |
| 18 | + |
| 19 | +import "context" |
| 20 | + |
| 21 | +type FunctionContext interface { |
| 22 | + context.Context |
| 23 | + GetState(ctx context.Context, key string) ([]byte, error) |
| 24 | + PutState(ctx context.Context, key string, value []byte) error |
| 25 | + Write(ctx context.Context, rawEvent Event[[]byte]) error |
| 26 | + Read(ctx context.Context) (Event[[]byte], error) |
| 27 | + GetConfig(ctx context.Context) (map[string]string, error) |
| 28 | +} |
| 29 | + |
| 30 | +type Event[T any] interface { |
| 31 | + Data() *T |
| 32 | + Ack(ctx context.Context) error |
| 33 | +} |
| 34 | + |
| 35 | +type BaseModule interface { |
| 36 | + Init(ctx FunctionContext) error |
| 37 | +} |
| 38 | + |
| 39 | +type Function[I any, O any] interface { |
| 40 | + BaseModule |
| 41 | + Handle(ctx FunctionContext, event Event[I]) (Event[O], error) |
| 42 | +} |
| 43 | + |
| 44 | +type Source[O any] interface { |
| 45 | + BaseModule |
| 46 | + Handle(ctx FunctionContext, emit func(context.Context, Event[O]) error) error |
| 47 | +} |
| 48 | + |
| 49 | +type Sink[I any] interface { |
| 50 | + BaseModule |
| 51 | + Handle(ctx FunctionContext, event Event[I]) error |
| 52 | +} |
| 53 | + |
| 54 | +type Custom interface { |
| 55 | + BaseModule |
| 56 | + Handle(ctx FunctionContext) error |
| 57 | +} |
| 58 | + |
| 59 | +type eventImpl[T any] struct { |
| 60 | + data *T |
| 61 | + ackFunc func(context.Context) error |
| 62 | +} |
| 63 | + |
| 64 | +func NewEvent[T any](data *T) Event[T] { |
| 65 | + return NewEventWithAck(data, nil) |
| 66 | +} |
| 67 | + |
| 68 | +func NewEventWithAck[T any](data *T, ack func(ctx context.Context) error) Event[T] { |
| 69 | + return &eventImpl[T]{ |
| 70 | + data: data, |
| 71 | + ackFunc: ack, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (e *eventImpl[T]) Data() *T { |
| 76 | + return e.data |
| 77 | +} |
| 78 | + |
| 79 | +func (e *eventImpl[T]) Ack(ctx context.Context) error { |
| 80 | + if e.ackFunc != nil { |
| 81 | + return e.ackFunc(ctx) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +type simpleFunction[I any, O any] struct { |
| 87 | + handle func(ctx FunctionContext, event Event[I]) (Event[O], error) |
| 88 | +} |
| 89 | + |
| 90 | +func NewSimpleFunction[I any, O any](handle func(ctx FunctionContext, event Event[I]) (Event[O], error)) Function[I, O] { |
| 91 | + return &simpleFunction[I, O]{ |
| 92 | + handle: handle, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (f *simpleFunction[I, O]) Init(_ FunctionContext) error { |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func (f *simpleFunction[I, O]) Handle(ctx FunctionContext, event Event[I]) (Event[O], error) { |
| 101 | + return f.handle(ctx, event) |
| 102 | +} |
| 103 | + |
| 104 | +type simpleSource[O any] struct { |
| 105 | + handle func(ctx FunctionContext, emit func(context.Context, Event[O]) error) error |
| 106 | +} |
| 107 | + |
| 108 | +func NewSimpleSource[O any](handle func(ctx FunctionContext, emit func(context.Context, Event[O]) error) error) Source[O] { |
| 109 | + return &simpleSource[O]{ |
| 110 | + handle: handle, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (s *simpleSource[O]) Init(_ FunctionContext) error { |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func (s *simpleSource[O]) Handle(ctx FunctionContext, emit func(context.Context, Event[O]) error) error { |
| 119 | + return s.handle(ctx, emit) |
| 120 | +} |
| 121 | + |
| 122 | +type simpleSink[I any] struct { |
| 123 | + handle func(ctx FunctionContext, event Event[I]) error |
| 124 | +} |
| 125 | + |
| 126 | +func NewSimpleSink[I any](handle func(ctx FunctionContext, event Event[I]) error) Sink[I] { |
| 127 | + return &simpleSink[I]{ |
| 128 | + handle: handle, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func (s *simpleSink[I]) Init(_ FunctionContext) error { |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func (s *simpleSink[I]) Handle(ctx FunctionContext, event Event[I]) error { |
| 137 | + return s.handle(ctx, event) |
| 138 | +} |
| 139 | + |
| 140 | +type simpleCustom struct { |
| 141 | + handle func(ctx FunctionContext) error |
| 142 | +} |
| 143 | + |
| 144 | +func NewSimpleCustom(handle func(ctx FunctionContext) error) Custom { |
| 145 | + return &simpleCustom{ |
| 146 | + handle: handle, |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func (c *simpleCustom) Init(_ FunctionContext) error { |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func (c *simpleCustom) Handle(ctx FunctionContext) error { |
| 155 | + return c.handle(ctx) |
| 156 | +} |
0 commit comments