Skip to content

Commit 447bf28

Browse files
committed
Introduced 'format' to encapsulate encoding and decoding formats together
1 parent 8f6d642 commit 447bf28

11 files changed

+253
-250
lines changed

cmd/evaluate_all_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
7676
}()
7777
}
7878

79-
format, err := yqlib.OutputFormatFromString(outputFormat)
79+
format, err := yqlib.FormatFromString(outputFormat)
8080
if err != nil {
8181
return err
8282
}

cmd/evaluate_sequence_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
9090
}()
9191
}
9292

93-
format, err := yqlib.OutputFormatFromString(outputFormat)
93+
format, err := yqlib.FormatFromString(outputFormat)
9494
if err != nil {
9595
return err
9696
}

cmd/root.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,22 @@ yq -P -oy sample.json
9898
}
9999

100100
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "auto", fmt.Sprintf("[auto|a|%v] output format type.", yqlib.GetAvailableOutputFormatString()))
101+
var outputCompletions = []string{"auto"}
102+
for _, formats := range yqlib.GetAvailableOutputFormats() {
103+
outputCompletions = append(outputCompletions, formats.FormalName)
104+
}
101105

102-
if err = rootCmd.RegisterFlagCompletionFunc("output-format", cobra.FixedCompletions([]string{"auto", "yaml", "json", "props", "xml", "tsv", "csv"}, cobra.ShellCompDirectiveNoFileComp)); err != nil {
106+
if err = rootCmd.RegisterFlagCompletionFunc("output-format", cobra.FixedCompletions(outputCompletions, cobra.ShellCompDirectiveNoFileComp)); err != nil {
103107
panic(err)
104108
}
105-
rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "auto", "[auto|a|yaml|y|props|p|xml|x|tsv|t|csv|c|toml] parse format for input. Note that json is a subset of yaml.")
106-
if err = rootCmd.RegisterFlagCompletionFunc("input-format", cobra.FixedCompletions([]string{"auto", "yaml", "props", "xml", "tsv", "csv", "toml"}, cobra.ShellCompDirectiveNoFileComp)); err != nil {
109+
rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "auto", fmt.Sprintf("[auto|a|%v] parse format for input.", yqlib.GetAvailableInputFormatString()))
110+
111+
var inputCompletions = []string{"auto"}
112+
for _, formats := range yqlib.GetAvailableInputFormats() {
113+
inputCompletions = append(inputCompletions, formats.FormalName)
114+
}
115+
116+
if err = rootCmd.RegisterFlagCompletionFunc("input-format", cobra.FixedCompletions(inputCompletions, cobra.ShellCompDirectiveNoFileComp)); err != nil {
107117
panic(err)
108118
}
109119

cmd/utils.go

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
6464
}
6565
if inputFormat == "" || inputFormat == "auto" || inputFormat == "a" {
6666

67-
inputFormat = yqlib.FormatFromFilename(inputFilename)
67+
inputFormat = yqlib.FormatStringFromFilename(inputFilename)
6868

69-
_, err := yqlib.InputFormatFromString(inputFormat)
69+
_, err := yqlib.FormatFromString(inputFormat)
7070
if err != nil {
7171
// unknown file type, default to yaml
7272
yqlib.GetLogger().Debug("Unknown file format extension '%v', defaulting to yaml", inputFormat)
@@ -86,23 +86,23 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
8686
// before this was introduced, `yq -pcsv things.csv`
8787
// would produce *yaml* output.
8888
//
89-
outputFormat = yqlib.FormatFromFilename(inputFilename)
89+
outputFormat = yqlib.FormatStringFromFilename(inputFilename)
9090
if inputFilename != "-" {
9191
yqlib.GetLogger().Warning("yq default output is now 'auto' (based on the filename extension). Normally yq would output '%v', but for backwards compatibility 'yaml' has been set. Please use -oy to specify yaml, or drop the -p flag.", outputFormat)
9292
}
9393
outputFormat = "yaml"
9494
}
9595

96-
outputFormatType, err := yqlib.OutputFormatFromString(outputFormat)
96+
outputFormatType, err := yqlib.FormatFromString(outputFormat)
9797

9898
if err != nil {
9999
return "", nil, err
100100
}
101101
yqlib.GetLogger().Debug("Using input format %v", inputFormat)
102102
yqlib.GetLogger().Debug("Using output format %v", outputFormat)
103103

104-
if outputFormatType == yqlib.YamlOutputFormat ||
105-
outputFormatType == yqlib.PropsOutputFormat {
104+
if outputFormatType == yqlib.YamlFormat ||
105+
outputFormatType == yqlib.PropertiesFormat {
106106
unwrapScalar = true
107107
}
108108
if unwrapScalarFlag.IsExplicitlySet() {
@@ -113,42 +113,20 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
113113
}
114114

115115
func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) {
116-
yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat)
116+
format, err := yqlib.FormatFromString(inputFormat)
117117
if err != nil {
118118
return nil, err
119119
}
120-
yqlibDecoder, err := createDecoder(yqlibInputFormat, evaluateTogether)
120+
yqlib.ConfiguredYamlPreferences.EvaluateTogether = evaluateTogether
121+
122+
yqlibDecoder := format.DecoderFactory()
121123
if yqlibDecoder == nil {
122124
return nil, fmt.Errorf("no support for %s input format", inputFormat)
123125
}
124-
return yqlibDecoder, err
126+
return yqlibDecoder, nil
125127
}
126128

127-
func createDecoder(format yqlib.InputFormat, evaluateTogether bool) (yqlib.Decoder, error) {
128-
switch format {
129-
case yqlib.LuaInputFormat:
130-
return yqlib.NewLuaDecoder(yqlib.ConfiguredLuaPreferences), nil
131-
case yqlib.XMLInputFormat:
132-
return yqlib.NewXMLDecoder(yqlib.ConfiguredXMLPreferences), nil
133-
case yqlib.PropertiesInputFormat:
134-
return yqlib.NewPropertiesDecoder(), nil
135-
case yqlib.JsonInputFormat:
136-
return yqlib.NewJSONDecoder(), nil
137-
case yqlib.CSVObjectInputFormat:
138-
return yqlib.NewCSVObjectDecoder(yqlib.ConfiguredCsvPreferences), nil
139-
case yqlib.TSVObjectInputFormat:
140-
return yqlib.NewCSVObjectDecoder(yqlib.ConfiguredTsvPreferences), nil
141-
case yqlib.TomlInputFormat:
142-
return yqlib.NewTomlDecoder(), nil
143-
case yqlib.YamlInputFormat:
144-
prefs := yqlib.ConfiguredYamlPreferences
145-
prefs.EvaluateTogether = evaluateTogether
146-
return yqlib.NewYamlDecoder(prefs), nil
147-
}
148-
return nil, fmt.Errorf("invalid decoder: %v", format)
149-
}
150-
151-
func configurePrinterWriter(format *yqlib.PrinterOutputFormat, out io.Writer) (yqlib.PrinterWriter, error) {
129+
func configurePrinterWriter(format *yqlib.Format, out io.Writer) (yqlib.PrinterWriter, error) {
152130

153131
var printerWriter yqlib.PrinterWriter
154132

@@ -166,18 +144,10 @@ func configurePrinterWriter(format *yqlib.PrinterOutputFormat, out io.Writer) (y
166144
}
167145

168146
func configureEncoder() (yqlib.Encoder, error) {
169-
yqlibOutputFormat, err := yqlib.OutputFormatFromString(outputFormat)
147+
yqlibOutputFormat, err := yqlib.FormatFromString(outputFormat)
170148
if err != nil {
171149
return nil, err
172150
}
173-
yqlibEncoder, err := createEncoder(yqlibOutputFormat)
174-
if yqlibEncoder == nil {
175-
return nil, fmt.Errorf("no support for %s output format", outputFormat)
176-
}
177-
return yqlibEncoder, err
178-
}
179-
180-
func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
181151
yqlib.ConfiguredXMLPreferences.Indent = indent
182152
yqlib.ConfiguredYamlPreferences.Indent = indent
183153
yqlib.ConfiguredJSONPreferences.Indent = indent
@@ -191,11 +161,12 @@ func createEncoder(format *yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
191161

192162
yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators
193163

194-
encoder := format.EncoderFactory()
164+
encoder := yqlibOutputFormat.EncoderFactory()
165+
195166
if encoder == nil {
196-
return nil, fmt.Errorf("invalid encoder: %v", format)
167+
return nil, fmt.Errorf("no support for %s output format", outputFormat)
197168
}
198-
return encoder, nil
169+
return encoder, err
199170
}
200171

201172
// this is a hack to enable backwards compatibility with githubactions (which pipe /dev/null into everything)

pkg/yqlib/decoder.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,10 @@
11
package yqlib
22

33
import (
4-
"fmt"
54
"io"
6-
"strings"
7-
)
8-
9-
type InputFormat uint
10-
11-
const (
12-
YamlInputFormat = 1 << iota
13-
XMLInputFormat
14-
PropertiesInputFormat
15-
Base64InputFormat
16-
JsonInputFormat
17-
CSVObjectInputFormat
18-
TSVObjectInputFormat
19-
TomlInputFormat
20-
UriInputFormat
21-
LuaInputFormat
225
)
236

247
type Decoder interface {
258
Init(reader io.Reader) error
269
Decode() (*CandidateNode, error)
2710
}
28-
29-
func InputFormatFromString(format string) (InputFormat, error) {
30-
switch format {
31-
case "yaml", "yml", "y":
32-
return YamlInputFormat, nil
33-
case "xml", "x":
34-
return XMLInputFormat, nil
35-
case "properties", "props", "p":
36-
return PropertiesInputFormat, nil
37-
case "json", "ndjson", "j":
38-
return JsonInputFormat, nil
39-
case "csv", "c":
40-
return CSVObjectInputFormat, nil
41-
case "tsv", "t":
42-
return TSVObjectInputFormat, nil
43-
case "toml":
44-
return TomlInputFormat, nil
45-
case "lua", "l":
46-
return LuaInputFormat, nil
47-
default:
48-
return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml|toml]", format)
49-
}
50-
}
51-
52-
func FormatFromFilename(filename string) string {
53-
54-
if filename != "" {
55-
GetLogger().Debugf("checking file extension '%s' for auto format detection", filename)
56-
nPos := strings.LastIndex(filename, ".")
57-
if nPos > -1 {
58-
format := filename[nPos+1:]
59-
GetLogger().Debugf("detected format '%s'", format)
60-
return format
61-
}
62-
}
63-
64-
GetLogger().Debugf("using default inputFormat 'yaml'")
65-
return "yaml"
66-
}

0 commit comments

Comments
 (0)