-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcodecs.go
72 lines (58 loc) · 1.3 KB
/
codecs.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package flow
import (
"context"
"fmt"
"io"
fdk "github.com/fnproject/fdk-go"
)
const (
appName = "FN_APP_NAME"
fnID = "FN_FN_ID"
format = "FN_FORMAT"
)
type codec interface {
getAppID() string
getFunctionID() string
isContinuation() bool
getFlowID() string
in() io.Reader
out() io.Writer
}
type fdkCodec struct {
ctx context.Context
input io.Reader
output io.Writer
}
func newCodec(ctx context.Context, in io.Reader, out io.Writer) codec {
return &fdkCodec{ctx, in, out}
}
func (c *fdkCodec) getAppID() string {
return fdk.GetContext(c.ctx).AppID()
}
func (c *fdkCodec) getFunctionID() string {
return fdk.GetContext(c.ctx).FnID()
}
func (c *fdkCodec) isContinuation() bool {
_, ok := c.getHeader(StageIDHeader)
return ok
}
func (c *fdkCodec) getFlowID() string {
fid, ok := c.getHeader(FlowIDHeader)
if !ok {
panic("Missing flow ID in continuation")
}
return fid
}
func (c *fdkCodec) getHeader(header string) (string, bool) {
//debug(fmt.Sprintf("headers: %v", fdk.GetContext(c.ctx).Header))
//debug(fmt.Sprintf("env: %v", os.Environ()))
v := fdk.GetContext(c.ctx).Header().Get(header)
debug(fmt.Sprintf("header: %s %v %s", header, v != "", v))
return v, v != ""
}
func (c *fdkCodec) in() io.Reader {
return c.input
}
func (c *fdkCodec) out() io.Writer {
return c.output
}