diff --git a/pkg/marshal/json_converter.go b/pkg/marshal/json_converter.go deleted file mode 100644 index 5ca9961d..00000000 --- a/pkg/marshal/json_converter.go +++ /dev/null @@ -1,54 +0,0 @@ -package marshal - -import ( - "encoding/json" - "fmt" - "strconv" - - yaml "github.com/mikefarah/yaml/v2" -) - -type JsonConverter interface { - JsonToString(context interface{}) (string, error) -} - -type jsonConverter struct{} - -func NewJsonConverter() JsonConverter { - return &jsonConverter{} -} - -func (j *jsonConverter) JsonToString(context interface{}) (string, error) { - out, err := json.Marshal(j.toJSON(context)) - if err != nil { - return "", fmt.Errorf("error printing yaml as json: %v", err) - } - return string(out), nil -} - -func (j *jsonConverter) toJSON(context interface{}) interface{} { - switch context := context.(type) { - case []interface{}: - oldArray := context - newArray := make([]interface{}, len(oldArray)) - for index, value := range oldArray { - newArray[index] = j.toJSON(value) - } - return newArray - case yaml.MapSlice: - oldMap := context - newMap := make(map[string]interface{}) - for _, entry := range oldMap { - if str, ok := entry.Key.(string); ok { - newMap[str] = j.toJSON(entry.Value) - } else if i, ok := entry.Key.(int); ok { - newMap[strconv.Itoa(i)] = j.toJSON(entry.Value) - } else if b, ok := entry.Key.(bool); ok { - newMap[strconv.FormatBool(b)] = j.toJSON(entry.Value) - } - } - return newMap - default: - return context - } -} diff --git a/pkg/marshal/json_converter_test.go b/pkg/marshal/json_converter_test.go deleted file mode 100644 index e49fbd28..00000000 --- a/pkg/marshal/json_converter_test.go +++ /dev/null @@ -1,48 +0,0 @@ -package marshal - -import ( - "testing" - - "github.com/mikefarah/yq/v2/test" -) - -func TestJsonToString(t *testing.T) { - var data = test.ParseData(` ---- -b: - c: 2 -`) - got, _ := NewJsonConverter().JsonToString(data) - test.AssertResult(t, "{\"b\":{\"c\":2}}", got) -} - -func TestJsonToString_withIntKey(t *testing.T) { - var data = test.ParseData(` ---- -b: - 2: c -`) - got, _ := NewJsonConverter().JsonToString(data) - test.AssertResult(t, `{"b":{"2":"c"}}`, got) -} - -func TestJsonToString_withBoolKey(t *testing.T) { - var data = test.ParseData(` ---- -b: - false: c -`) - got, _ := NewJsonConverter().JsonToString(data) - test.AssertResult(t, `{"b":{"false":"c"}}`, got) -} - -func TestJsonToString_withArray(t *testing.T) { - var data = test.ParseData(` ---- -b: - - item: one - - item: two -`) - got, _ := NewJsonConverter().JsonToString(data) - test.AssertResult(t, "{\"b\":[{\"item\":\"one\"},{\"item\":\"two\"}]}", got) -} diff --git a/pkg/marshal/yaml_converter.go b/pkg/marshal/yaml_converter.go deleted file mode 100644 index b5826da8..00000000 --- a/pkg/marshal/yaml_converter.go +++ /dev/null @@ -1,43 +0,0 @@ -package marshal - -import ( - "strings" - - yaml "github.com/mikefarah/yaml/v2" - errors "github.com/pkg/errors" -) - -type YamlConverter interface { - YamlToString(context interface{}, trimOutput bool) (string, error) -} - -type yamlConverter struct{} - -func NewYamlConverter() YamlConverter { - return &yamlConverter{} -} - -func (y *yamlConverter) YamlToString(context interface{}, trimOutput bool) (string, error) { - switch context := context.(type) { - case string: - return context, nil - default: - return y.marshalContext(context, trimOutput) - } -} - -func (y *yamlConverter) marshalContext(context interface{}, trimOutput bool) (string, error) { - out, err := yaml.Marshal(context) - - if err != nil { - return "", errors.Wrap(err, "error printing yaml") - } - - outStr := string(out) - // trim the trailing new line as it's easier for a script to add - // it in if required than to remove it - if trimOutput { - return strings.Trim(outStr, "\n "), nil - } - return outStr, nil -} diff --git a/pkg/marshal/yaml_converter_test.go b/pkg/marshal/yaml_converter_test.go deleted file mode 100644 index 35e9a4d1..00000000 --- a/pkg/marshal/yaml_converter_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package marshal - -import ( - "testing" - - "github.com/mikefarah/yq/v2/test" -) - -func TestYamlToString(t *testing.T) { - var raw = `b: - c: 2 -` - var data = test.ParseData(raw) - got, _ := NewYamlConverter().YamlToString(data, false) - test.AssertResult(t, raw, got) -} - -func TestYamlToString_withTrim(t *testing.T) { - var raw = `b: - c: 2` - var data = test.ParseData(raw) - got, _ := NewYamlConverter().YamlToString(data, true) - test.AssertResult(t, raw, got) -} - -func TestYamlToString_withIntKey(t *testing.T) { - var raw = `b: - 2: c -` - var data = test.ParseData(raw) - got, _ := NewYamlConverter().YamlToString(data, false) - test.AssertResult(t, raw, got) -} - -func TestYamlToString_withBoolKey(t *testing.T) { - var raw = `b: - false: c -` - var data = test.ParseData(raw) - got, _ := NewYamlConverter().YamlToString(data, false) - test.AssertResult(t, raw, got) -} - -func TestYamlToString_withArray(t *testing.T) { - var raw = `b: -- item: one -- item: two -` - var data = test.ParseData(raw) - got, _ := NewYamlConverter().YamlToString(data, false) - test.AssertResult(t, raw, got) -} diff --git a/yq.go b/yq.go index 5a21169c..c0d576c4 100644 --- a/yq.go +++ b/yq.go @@ -8,7 +8,6 @@ import ( "os" "strconv" - "github.com/mikefarah/yq/v3/pkg/marshal" "github.com/mikefarah/yq/v3/pkg/yqlib" errors "github.com/pkg/errors" @@ -31,8 +30,6 @@ var version = false var docIndex = "0" var log = logging.MustGetLogger("yq") var lib = yqlib.NewYqLib(log) -var jsonConverter = marshal.NewJsonConverter() -var yamlConverter = marshal.NewYamlConverter() var valueParser = yqlib.NewValueParser() func main() { @@ -595,13 +592,6 @@ func parseValue(argument string) yaml.Node { return yaml.Node{Value: argument, Tag: tag, Kind: yaml.ScalarNode} } -func toString(context interface{}) (string, error) { - if outputToJSON { - return jsonConverter.JsonToString(context) - } - return yamlConverter.YamlToString(context, true) -} - func safelyRenameFile(from string, to string) { if renameError := os.Rename(from, to); renameError != nil { log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)