This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathfunction.go
More file actions
96 lines (76 loc) · 2.66 KB
/
Copy pathfunction.go
File metadata and controls
96 lines (76 loc) · 2.66 KB
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
package function
import (
"encoding/json"
"errors"
"github.com/serverless/event-gateway/metadata"
"go.uber.org/zap/zapcore"
)
// ID uniquely identifies a function.
type ID string
// Function represents a function deployed on one of the supported providers.
type Function struct {
Space string `json:"space" validate:"required,min=3,space"`
ID ID `json:"functionId" validate:"required,functionid"`
ProviderType ProviderType `json:"type"`
ProviderConfig *json.RawMessage `json:"provider"`
Provider Provider `json:"-" validate:"-"`
Metadata metadata.Metadata `json:"metadata,omitempty"`
}
// Functions is an array of functions.
type Functions []*Function
// Call tries to send a payload to a target function
func (f *Function) Call(payload []byte) ([]byte, error) {
return f.Provider.Call(payload)
}
// MarshalLogObject is a part of zapcore.ObjectMarshaler interface
func (f Function) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("space", string(f.Space))
enc.AddString("functionId", string(f.ID))
enc.AddString("type", string(f.ProviderType))
enc.AddObject("provider", f.Provider)
return nil
}
// MarshalJSON marshals provides as config and returns JSON representation of the function.
func (f *Function) MarshalJSON() ([]byte, error) {
// This line is needed to avoid stack overflow because of recursive MarshalJSON call
type functionJSON Function
config, err := json.Marshal(f.Provider)
if err != nil {
return nil, err
}
rawConfig := json.RawMessage(config)
fn := functionJSON{
Space: f.Space,
ID: f.ID,
ProviderType: f.ProviderType,
ProviderConfig: &rawConfig,
Metadata: f.Metadata,
}
return json.Marshal(fn)
}
// UnmarshalJSON unmarshals function JSON, detects provider type and loads the provider.
func (f *Function) UnmarshalJSON(data []byte) error {
// This line is needed to avoid stack overflow because of recursive UnmarshalJSON call
type functionJSON Function
rawFunction := functionJSON{}
if err := json.Unmarshal(data, &rawFunction); err != nil {
return err
}
if rawFunction.ProviderType == "" {
return errors.New("provider configuration not set")
}
f.ID = rawFunction.ID
f.Space = rawFunction.Space
f.Metadata = rawFunction.Metadata
f.ProviderType = rawFunction.ProviderType
if loader, ok := providers[rawFunction.ProviderType]; ok {
// err includes validation errors happening on provider side
provider, err := loader.Load(*rawFunction.ProviderConfig)
if err != nil {
return err
}
f.Provider = provider
return nil
}
return errors.New("provider " + string(rawFunction.ProviderType) + " not supported")
}