-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
159 lines (137 loc) · 3.49 KB
/
output.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package azfunc
import (
"encoding/json"
"github.com/KarlGW/azfunc/bindings"
"github.com/KarlGW/azfunc/data"
)
// bindable is the interface that wraps around methods Data, Name and Write.
type bindable interface {
// Data returns the data of the binding.
Data() data.Raw
// Name returns the name of the binding.
Name() string
// Write to the binding.
Write([]byte) (int, error)
}
// Output represents an outgoing response to the Functuon Host.
type Output struct {
Outputs map[string]bindable
ReturnValue any
http *bindings.HTTP
Logs []string
}
// MarshalJSON implements custom marshaling to produce
// the required JSON structure as expected by the
// function host.
func (o Output) MarshalJSON() ([]byte, error) {
type Alias Output
temp := struct {
Outputs map[string]any `json:"Outputs"`
Alias
}{
Outputs: make(map[string]any),
Alias: Alias(o),
}
for key, binding := range o.Outputs {
if b, ok := binding.(*bindings.HTTP); ok {
temp.Outputs[key] = b
} else {
temp.Outputs[key] = binding.Data()
}
}
return json.Marshal(temp)
}
// JSON returns the JSON encoding of Output.
func (o Output) JSON() []byte {
if o.http != nil {
o.Outputs[o.http.Name()] = o.http
}
b, _ := json.Marshal(o)
return b
}
// AddBindings one or more bindings to Output.
func (o *Output) AddBindings(bindables ...bindable) {
if o.Outputs == nil {
o.Outputs = make(map[string]bindable, len(bindables))
}
for _, binding := range bindables {
if b, ok := binding.(*bindings.HTTP); ok {
o.http = b
} else {
o.Outputs[binding.Name()] = binding
}
}
}
// Log adds a message to the Logs of Output.
func (o *Output) Log(msg string) {
if o.Logs == nil {
o.Logs = make([]string, 0)
}
o.Logs = append(o.Logs, msg)
}
// SetReturnValue sets ReturnValue of Output.
func (o *Output) SetReturnValue(v any) {
o.ReturnValue = v
}
// Binding returns the binding with the provided name, if no binding
// with that name exists, return a new generic binding with the
// provided name.
func (o Output) Binding(name string) bindable {
binding, ok := o.Outputs[name]
if !ok {
o.Outputs[name] = bindings.NewGeneric(name)
return o.Outputs[name]
}
return binding
}
// HTTP returns the HTTP binding of output if any is set.
// If not set it will create, set and return it.
func (o *Output) HTTP() *bindings.HTTP {
if o.http == nil {
o.http = bindings.NewHTTP()
return o.http
}
return o.http
}
// OutputOptions contains options for creating a new
// Output.
type OutputOptions struct {
ReturnValue any
http *bindings.HTTP
Bindings []bindable
Logs []string
}
// Output option is a function that sets OutputOptions.
type OutputOption func(o *OutputOptions)
// NewOutput creates a new Output containing binding to be used for creating
// the response back to the Function host.
func NewOutput(options ...OutputOption) Output {
opts := OutputOptions{}
for _, option := range options {
option(&opts)
}
var logs []string
if len(opts.Logs) > 0 {
logs = make([]string, len(opts.Logs))
copy(logs, opts.Logs)
}
output := Output{
Logs: logs,
ReturnValue: opts.ReturnValue,
http: opts.http,
}
output.AddBindings(opts.Bindings...)
return output
}
// WithBindings add one or more bindings to OutputOptions
func WithBindings(bindables ...bindable) OutputOption {
return func(o *OutputOptions) {
for _, binding := range bindables {
if b, ok := binding.(*bindings.HTTP); ok {
o.http = b
} else {
o.Bindings = append(o.Bindings, binding)
}
}
}
}