/
importer.go
99 lines (90 loc) · 2.65 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
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
package importer
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
// Importer is an interface implemented by all sysl importers
type Importer interface {
// LoadFile reads in a file from path and returns the generated Sysl.
LoadFile(path string) (string, error)
// Load takes in a string in a format supported by an the importer
// It returns the converted Sysl as a string.
Load(content string) (string, error)
// WithAppName allows the imported Sysl application name to be specified.
WithAppName(appName string) Importer
// WithPackage allows the imported Sysl package attribute to be specified.
WithPackage(packageName string) Importer
}
// Formats lists all supported import formats
// TODO: Add all transform imports dynamically
var Formats = []Format{
Grammar,
OpenAPI3,
OpenAPI2,
XSD,
Avro,
SpannerSQL,
SpannerSQLDir,
Postgres,
PostgresDir,
MySQL,
MySQLDir,
BigQuery,
JSONSchema,
}
// Factory takes in an absolute path and its contents (if path is a file) and returns an importer
// for the detected file type.
func Factory(path string, isDir bool, formatName string, content []byte, logger *logrus.Logger) (Importer, error) {
var format Format
if formatName != "" {
for _, f := range Formats {
if strings.EqualFold(formatName, f.Name) {
format = f
break
}
}
if format.Name == "" {
return nil, fmt.Errorf("an importer does not exist for %s", formatName)
}
} else {
// TODO: Get rid of format autodetection
ft, err := GuessFileType(path, isDir, content, Formats)
if err != nil {
return nil, err
}
format = ft
}
for _, f := range Formats {
if f.Name != format.Name {
continue
}
logger.Debugln("Detected " + f.Name)
break
}
switch format.Name {
case OpenAPI2.Name:
logger.Debugln("Detected OpenAPI2")
return MakeOpenAPI2Importer(logger, "", path), nil
case OpenAPI3.Name:
logger.Debugln("Detected OpenAPI3")
// FIXME: revert back to the arrai importer once regression test is done.
return NewLegacyOpenAPIV3Importer(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", format.Name)
case Avro.Name:
logger.Debugln("Detected Avro")
return NewAvroImporter(logger), nil
case SpannerSQL.Name, SpannerSQLDir.Name, Postgres.Name, PostgresDir.Name, MySQL.Name, MySQLDir.Name, BigQuery.Name:
logger.Debugln("Detected SQL")
return MakeSQLImporter(logger), nil
default:
logger.Debugln("Defaulting to transform-based importing")
return MakeTransformImporter(logger, format.Name), nil
}
}