-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_workato_connector.go
107 lines (94 loc) · 2.31 KB
/
generate_workato_connector.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
package genworkato
import (
"bytes"
"embed"
"encoding/json"
"fmt"
"os"
"strings"
tmpl "text/template"
"github.com/Masterminds/sprig/v3"
"github.com/SafetyCulture/protoc-gen-workato/config"
"github.com/SafetyCulture/protoc-gen-workato/template"
gendoc "github.com/pseudomuto/protoc-gen-doc"
)
func formatStringSlice(slc []string) (string, error) {
if slc == nil {
return "[]", nil
}
b, err := json.Marshal(slc)
if err != nil {
return "", err
}
return string(b), nil
}
//go:embed templates/*.tmpl.rb
var tmpls embed.FS
// GenerateWorkatoConnector generates a Workato SDK Connector from protobufs
func GenerateWorkatoConnector(gendoctemplate *gendoc.Template, cfg *config.Config) ([]byte, error) {
workatoTemplate, err := template.FromGenDoc(gendoctemplate, cfg)
if err != nil {
return nil, err
}
tp := tmpl.New("Connector Template").
Funcs(sprig.TxtFuncMap())
tp.Funcs(tmpl.FuncMap{
"format_string_slice": formatStringSlice,
"field_key": func(name string, data interface{}) (string, error) {
value := ""
switch d := data.(type) {
case string:
if len(d) != 0 {
value = fmt.Sprintf("\"%s\"", strings.ReplaceAll(d, `"`, `\"`))
}
case *bool:
if d != nil {
value = "false"
if *d {
value = "true"
}
}
case bool:
value = "false"
if d {
value = "true"
}
}
if len(value) == 0 {
return "", nil
}
return fmt.Sprintf("%s: %s,\n ", name, value), nil
},
"include": func(name string, data interface{}) (string, error) {
buf := bytes.NewBuffer(nil)
if err := tp.ExecuteTemplate(buf, name, data); err != nil {
return "", err
}
return buf.String(), nil
},
})
if tp, err = tp.ParseFS(tmpls, "templates/*.tmpl.rb"); err != nil {
return nil, err
}
templateName := "connector.tmpl.rb"
if cfg.TemplateFile != "" {
templateName = "custom_template"
customTp := tp.New(templateName)
b, err := os.ReadFile(cfg.TemplateFile)
if err != nil {
return nil, err
}
_, err = customTp.Parse(string(b))
if err != nil {
return nil, err
}
}
// We should sort all the lists in this template before rendering
// This way the result is deterministic and diffing is easy.
var buf bytes.Buffer
err = tp.ExecuteTemplate(&buf, templateName, workatoTemplate)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}