-
Notifications
You must be signed in to change notification settings - Fork 23
/
pattern.go
52 lines (46 loc) · 1.19 KB
/
pattern.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
package microgateway
import (
"encoding/json"
"fmt"
"github.com/project-flogo/microgateway/api"
"github.com/project-flogo/microgateway/internal/pattern"
)
var patternMap = make(map[string][]byte)
func init() {
patterns := []string{"DefaultChannelPattern", "DefaultHttpPattern"}
for i := range patterns {
patternName := patterns[i] + ".json"
JSON, err := pattern.Asset(patternName)
if err != nil {
fmt.Println("Error from Asset function")
}
patternMap[patternName] = JSON
}
}
// Load loads a pattern
func Load(pattern string) (*api.Microgateway, error) {
patternJSON := []byte{}
patternJSON = getPattern(pattern)
pDef := &api.Microgateway{}
err := json.Unmarshal(patternJSON, pDef)
if err != nil {
return nil, err
}
return pDef, nil
}
//Registers a pattern
func Register(patternName string, pattern string) error {
patternFileName := patternName + ".json"
if _, ok := patternMap[patternFileName]; !ok {
patternMap[patternFileName] = []byte(pattern)
}
return nil
}
//Returns a registered pattern
func getPattern(pattern string) []byte {
patternFileName := pattern + ".json"
if _, ok := patternMap[patternFileName]; ok {
return patternMap[patternFileName]
}
return nil
}