Skip to content

Commit

Permalink
Allow format signature to be quoted (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored Nov 20, 2023
1 parent 7284d57 commit 7e73220
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
43 changes: 22 additions & 21 deletions pkg/importer/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,122 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/ghodss/yaml"
)

// Format represents a format that can be imported into Sysl
type Format struct {
Name string // Name of the format
Signature string // This is a string which can be used to uniquely identify the format
FileExt []string // The file extension of the format
Name string // Name of the format
Signature *regexp.Regexp // This is a regex which can be used to uniquely identify the format
FileExt []string // The file extension of the format
}

var SYSL = Format{
Name: "sysl",
Signature: "",
Signature: nil,
FileExt: []string{".sysl"},
}

var SyslPB = Format{
Name: "sysl.pb",
Signature: "",
Signature: nil,
FileExt: []string{".pb", ".textpb"},
}

var XSD = Format{
Name: "xsd",
Signature: ``,
Signature: nil,
FileExt: []string{".xsd", ".xml"},
}

var Grammar = Format{
Name: "grammar",
Signature: "",
Signature: nil,
FileExt: []string{".g"},
}

var Avro = Format{
Name: "avro",
Signature: "",
Signature: nil,
FileExt: []string{".avsc"},
}

var SpannerSQL = Format{
Name: "spannerSQL",
Signature: "",
Signature: nil,
FileExt: []string{".sql"},
}

var SpannerSQLDir = Format{
Name: "spannerSQLdir",
Signature: "",
Signature: nil,
FileExt: []string{".up.sql"},
}

var Postgres = Format{
Name: "postgres",
Signature: "",
Signature: nil,
FileExt: []string{".sql"},
}

var PostgresDir = Format{
Name: "postgresDir",
Signature: "",
Signature: nil,
FileExt: []string{".up.sql"},
}

var Protobuf = Format{
Name: "protobuf",
Signature: "",
Signature: nil,
FileExt: []string{".proto"},
}

var ProtobufDir = Format{
Name: "protobufDir",
Signature: "",
Signature: nil,
FileExt: []string{".up.proto"},
}

var MySQL = Format{
Name: "mysql",
Signature: "",
Signature: nil,
FileExt: []string{".sql"},
}

var MySQLDir = Format{
Name: "mysqlDir",
Signature: "",
Signature: nil,
FileExt: []string{".up.sql"},
}

var BigQuery = Format{
Name: "bigquery",
Signature: "",
Signature: nil,
FileExt: []string{".sql"},
}

// OpenAPI3 is identified by the openapi header. - The value MUST be "3.x.x".
// For more details refer to https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#oasDocument
var OpenAPI3 = Format{
Name: "openapi3",
Signature: `openapi:`,
Signature: regexp.MustCompile(`["']?openapi["']?\s*:`),
FileExt: []string{".yaml", ".json", ".yml"},
}

// OpenAPI2 only has swagger 2.0.0 as the single valid format - The value MUST be "2.0".
// For more details refer to https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swaggerObject
var OpenAPI2 = Format{
Name: "swagger",
Signature: `swagger:`,
Signature: regexp.MustCompile(`["']?swagger["']?\s*:`),
FileExt: []string{".yaml", ".json", ".yml"},
}

var JSONSchema = Format{
Name: "jsonschema",
Signature: "$schema",
Signature: regexp.MustCompile(`\$schema`),
FileExt: []string{".json"},
}

Expand Down Expand Up @@ -165,7 +166,7 @@ func GuessFileType(path string, isDir bool, content []byte, validFormats []Forma
}

for _, format := range matchesExt {
if strings.Contains(string(content), format.Signature) {
if format.Signature == nil || format.Signature.Match(content) {
matchesSignature = append(matchesSignature, format)
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/importer/formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var formatTests = []struct {
{"Parses Sysl PB ext", ".sysl.pb", []byte{}, &SyslPB, nil},
{"Parses openapi3 yaml files", ".yaml", []byte(`openapi: "3.0.0"`), &OpenAPI3, nil},
{"Parses openapi3 yaml files", ".yaml", []byte(`openapi: "3.0.3"`), &OpenAPI3, nil},
{"Parses openapi3 yaml files", ".yaml", []byte(`"openapi": "3.0.0"`), &OpenAPI3, nil},
{"Parses openapi3 yaml files", ".yaml", []byte(`'openapi': "3.0.0"`), &OpenAPI3, nil},
{"Parses openapi3 yaml files", ".yaml", []byte(`openapi : "3.0.0"`), &OpenAPI3, nil},
{"Parses openapi3 yaml files", ".yaml", []byte(`"openapi" : "3.0.0"`), &OpenAPI3, nil},
{"Parses openapi3 json files", ".json", []byte(`{"openapi": "3.0.0"}`), &OpenAPI3, nil},
{"Parses swagger2 yaml files", ".yaml", []byte(`swagger: "2.0"`), &OpenAPI2, nil},
{"Parses swagger2 json files", ".json", []byte(`{"swagger": "2.0"}`), &OpenAPI2, nil},
Expand Down

0 comments on commit 7e73220

Please sign in to comment.