2022-02-10 01:02:53 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-03-08 23:17:19 +00:00
|
|
|
"strings"
|
2022-02-10 01:02:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InputFormat uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
YamlInputFormat = 1 << iota
|
|
|
|
XMLInputFormat
|
|
|
|
PropertiesInputFormat
|
2022-02-22 22:26:35 +00:00
|
|
|
Base64InputFormat
|
2022-07-27 02:26:22 +00:00
|
|
|
JsonInputFormat
|
2022-08-01 00:28:34 +00:00
|
|
|
CSVObjectInputFormat
|
|
|
|
TSVObjectInputFormat
|
2023-01-23 00:37:18 +00:00
|
|
|
UriInputFormat
|
2022-02-10 01:02:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Decoder interface {
|
2022-10-28 03:16:46 +00:00
|
|
|
Init(reader io.Reader) error
|
|
|
|
Decode() (*CandidateNode, error)
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func InputFormatFromString(format string) (InputFormat, error) {
|
|
|
|
switch format {
|
2023-03-08 23:17:19 +00:00
|
|
|
case "yaml", "yml", "y":
|
2022-02-10 01:02:53 +00:00
|
|
|
return YamlInputFormat, nil
|
|
|
|
case "xml", "x":
|
|
|
|
return XMLInputFormat, nil
|
2023-03-08 23:17:19 +00:00
|
|
|
case "properties", "props", "p":
|
2022-02-10 01:02:53 +00:00
|
|
|
return PropertiesInputFormat, nil
|
2022-07-27 02:26:22 +00:00
|
|
|
case "json", "ndjson", "j":
|
|
|
|
return JsonInputFormat, nil
|
2022-08-01 00:28:34 +00:00
|
|
|
case "csv", "c":
|
|
|
|
return CSVObjectInputFormat, nil
|
|
|
|
case "tsv", "t":
|
|
|
|
return TSVObjectInputFormat, nil
|
2022-02-10 01:02:53 +00:00
|
|
|
default:
|
2023-03-09 01:04:56 +00:00
|
|
|
return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml]", format)
|
2022-02-10 01:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-08 23:17:19 +00:00
|
|
|
|
2023-03-09 00:58:52 +00:00
|
|
|
func FormatFromFilename(filename string) string {
|
|
|
|
|
2023-03-08 23:17:19 +00:00
|
|
|
if filename != "" {
|
2023-03-09 00:58:52 +00:00
|
|
|
GetLogger().Debugf("checking file extension '%s' for auto format detection", filename)
|
2023-03-08 23:17:19 +00:00
|
|
|
nPos := strings.LastIndex(filename, ".")
|
|
|
|
if nPos > -1 {
|
2023-03-09 00:58:52 +00:00
|
|
|
format := filename[nPos+1:]
|
|
|
|
GetLogger().Debugf("detected format '%s'", format)
|
|
|
|
return format
|
2023-03-08 23:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 00:58:52 +00:00
|
|
|
GetLogger().Debugf("using default inputFormat 'yaml'")
|
|
|
|
return "yaml"
|
2023-03-08 23:17:19 +00:00
|
|
|
}
|