-
Notifications
You must be signed in to change notification settings - Fork 42
/
avro.go
59 lines (52 loc) · 1.52 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
package importer
import (
"fmt"
"io/ioutil"
"strings"
"github.com/anz-bank/sysl/internal/bundles"
"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) {
if i.appName == "" {
return "", errors.New("application name not provided")
}
b := bundles.Transformer
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
}
// Configure allows the imported Sysl application name, package and import directories to be specified.
func (i *avroImporter) Configure(appName, packageName, _ string) (Importer, error) {
if appName == "" {
return nil, errors.New("application name not provided")
}
i.appName = appName
i.pkg = packageName
return i, nil
}