-
Notifications
You must be signed in to change notification settings - Fork 42
/
importer.go
51 lines (45 loc) · 1.45 KB
/
importer.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
package importer
import (
"fmt"
"github.com/spf13/afero"
"github.com/sirupsen/logrus"
)
// Importer is an interface implemented by all sysl importers
type Importer interface {
// Load takes in a string in a format supported by an the importer
// It returns the converted Sysl as a string
Load(file string) (string, error)
// WithAppName allows the exported Sysl application name to be specified
WithAppName(appName string) Importer
// WithPackage allows the exported Sysl package attribute to be specified
WithPackage(packageName string) Importer
}
var Formats = []Format{
Grammar,
OpenAPI3,
Swagger,
XSD,
}
// Factory takes in an absolute filePath and a file and returns an importer from the detected file type
func Factory(filePath string, file []byte, logger *logrus.Logger) (Importer, error) {
fileType, err := GuessFileType(filePath, file, Formats)
if err != nil {
return nil, err
}
switch fileType.Name {
case Swagger.Name:
logger.Debugln("Detected OpenAPI2")
return MakeOpenAPI2Importer(logger, "", filePath), nil
case OpenAPI3.Name:
logger.Debugln("Detected OpenAPI3")
return NewOpenAPIV3Importer(logger, afero.NewOsFs()), nil
case XSD.Name:
logger.Debugln("Detected XSD")
return MakeXSDImporter(logger), nil
case Grammar.Name:
logger.Debugln("Detected Grammar file")
return nil, fmt.Errorf("importer disabled for: %s", fileType.Name)
default:
return nil, fmt.Errorf("an importer does not exist for: %s", fileType.Name)
}
}