-
Notifications
You must be signed in to change notification settings - Fork 43
/
protobuf.go
72 lines (61 loc) · 2.06 KB
/
protobuf.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
package importer
import (
"fmt"
"strings"
"github.com/anz-bank/sysl/internal/bundles"
"github.com/anz-bank/sysl/pkg/arrai"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type ProtobufImporter struct {
appName string
pkg string
importPaths string
logger *logrus.Logger
}
// MakeProtobufImporter is a factory method for creating new Protobuf importer.
func MakeProtobufImporter(logger *logrus.Logger) *ProtobufImporter {
return &ProtobufImporter{logger: logger}
}
func (p *ProtobufImporter) LoadFile(path string) (string, error) {
b := bundles.ProtoImporter
// TODO: Make the appname optional
val, err := arrai.EvaluateBundle(b, `--app-name`, p.appName, `--input`, path, `--import-paths`, p.importPaths)
if err != nil {
return "", errors.Wrap(arrai.ExecutionError{
Context: fmt.Sprintf("AppName: %s, File: %s, ImportPaths: %s", p.appName, path, p.importPaths),
Err: err,
ShortMsg: err.Error(),
}, "Executing arrai transform failed")
}
return strings.TrimSpace(val.String()) + "\n", nil
}
// Load returns a Sysl spec equivalent to protoSpec.
func (p *ProtobufImporter) Load(protoSpec string) (string, error) {
b := bundles.ProtoImporter
// TODO: Make the appname optional
val, err := arrai.EvaluateBundle(b, `--app-name`, p.appName, `--spec`, protoSpec, `--import-paths`, p.importPaths)
if err != nil {
return "", errors.Wrap(arrai.ExecutionError{
Context: fmt.Sprintf("AppName: %s, Content: %s, ImportPaths: %s", p.appName, protoSpec, p.importPaths),
Err: err,
ShortMsg: err.Error(),
}, "Executing arrai transform failed")
}
return strings.TrimSpace(val.String()) + "\n", nil
}
// Set the AppName of the imported app
func (p *ProtobufImporter) WithAppName(appName string) Importer {
p.appName = appName
return p
}
// Set the package attribute of the imported app
func (p *ProtobufImporter) WithPackage(pkg string) Importer {
p.pkg = pkg
return p
}
// Set the importPaths attribute of the imported app
func (p *ProtobufImporter) WithImports(importPaths string) Importer {
p.importPaths = importPaths
return p
}