-
Notifications
You must be signed in to change notification settings - Fork 42
/
avro.go
60 lines (52 loc) · 1.42 KB
/
avro.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
package importer
import (
"fmt"
"io/ioutil"
"strings"
arrai2 "github.com/anz-bank/sysl/internal/arrai"
"github.com/anz-bank/sysl/pkg/arrai"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// NewAvroImporter returns a new avroImporter.
func NewAvroImporter(logger *logrus.Logger) Importer {
return &avroImporter{logger: logger}
}
// avroImporter represents Avro specification importer.
type avroImporter struct {
appName string
pkg string
logger *logrus.Logger
}
func (i *avroImporter) LoadFile(path string) (string, error) {
bs, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return i.Load(string(bs))
}
// Load returns a Sysl spec equivalent to avroSpec.
func (i *avroImporter) Load(avroSpec string) (string, error) {
b, err := arrai2.Asset("pkg/importer/avro/transformer_cli.arraiz")
if err != nil {
return "", err
}
val, err := arrai.EvaluateBundle(b, avroSpec, i.appName, i.pkg)
if err != nil {
return "", errors.Wrap(arrai.ExecutionError{
Context: fmt.Sprintf("import(`%s`, `%s`, `%s`)", avroSpec, i.appName, i.pkg),
Err: err,
ShortMsg: err.Error(),
}, "Executing arrai transform failed")
}
return strings.TrimSpace(val.String()) + "\n", nil
}
func (i *avroImporter) WithAppName(appName string) Importer {
i.appName = appName
return i
}
// Sets the package attribute of the imported app.
func (i *avroImporter) WithPackage(pkg string) Importer {
i.pkg = pkg
return i
}