Auto output format (#1599)

* Use file extension to auto detect output format!

* Use file extension to auto detect output format!

* formatting
This commit is contained in:
Mike Farah
2023-03-15 13:22:58 +11:00
committed by GitHub
parent 2c14c98408
commit 08a6cb65fe
12 changed files with 114 additions and 119 deletions
+9 -8
View File
@@ -39,21 +39,22 @@ func InputFormatFromString(format string) (InputFormat, error) {
case "tsv", "t":
return TSVObjectInputFormat, nil
default:
return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml|props]", format)
return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml]", format)
}
}
func InputFormatFromFilename(filename string, defaultFormat string) string {
func FormatFromFilename(filename string) string {
if filename != "" {
GetLogger().Debugf("checking filename '%s' for inputFormat", filename)
GetLogger().Debugf("checking file extension '%s' for auto format detection", filename)
nPos := strings.LastIndex(filename, ".")
if nPos > -1 {
inputFormat := filename[nPos+1:]
GetLogger().Debugf("detected inputFormat '%s'", inputFormat)
return inputFormat
format := filename[nPos+1:]
GetLogger().Debugf("detected format '%s'", format)
return format
}
}
GetLogger().Debugf("using default inputFormat '%s'", defaultFormat)
return defaultFormat
GetLogger().Debugf("using default inputFormat 'yaml'")
return "yaml"
}
-1
View File
@@ -30,4 +30,3 @@ func filterOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
}
return context.ChildContext(results), nil
}
+7 -7
View File
@@ -7,22 +7,22 @@ import (
var filterOperatorScenarios = []expressionScenario{
{
description: "Filter array",
document: `[1,2,3]`,
expression: `filter(. < 3)`,
document: `[1,2,3]`,
expression: `filter(. < 3)`,
expected: []string{
"D0, P[], (!!seq)::[1, 2]\n",
},
},
{
skipDoc: true,
document: `[1,2,3]`,
expression: `filter(. > 1)`,
skipDoc: true,
document: `[1,2,3]`,
expression: `filter(. > 1)`,
expected: []string{
"D0, P[], (!!seq)::[2, 3]\n",
},
},
{
skipDoc: true,
skipDoc: true,
description: "Filter array to empty",
document: `[1,2,3]`,
expression: `filter(. > 4)`,
@@ -31,7 +31,7 @@ var filterOperatorScenarios = []expressionScenario{
},
},
{
skipDoc: true,
skipDoc: true,
description: "Filter empty array",
document: `[]`,
expression: `filter(. > 1)`,
+2 -2
View File
@@ -33,11 +33,11 @@ const (
func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
switch format {
case "yaml", "y":
case "yaml", "y", "yml":
return YamlOutputFormat, nil
case "json", "j":
return JSONOutputFormat, nil
case "props", "p":
case "props", "p", "properties":
return PropsOutputFormat, nil
case "csv", "c":
return CSVOutputFormat, nil