Skip to content

Commit

Permalink
Add JSON output mode (-j flag)
Browse files Browse the repository at this point in the history
- Implement JSON conversion for XML/HTML input
- Add tests for JSON output functionality
- Update README with JSON output examples
- Refactor main command logic to support JSON mode
  • Loading branch information
tmc committed Nov 8, 2024
1 parent 25614c4 commit 92cc6f9
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 24 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,16 @@ You can play with the `xq` utility using the Dockerized environment:
docker-compose run --rm xq
xq /opt/examples/xml/unformatted.xml
```

Output the result as JSON:

```
cat test/data/xml/unformatted.xml | xq -j
```

This will output the result in JSON format, preserving the XML structure. The JSON output will be an object where:
- XML elements become object keys
- Attributes are prefixed with "@"
- Text content is stored under "#text" if the element has attributes or child elements
- Repeated elements are automatically converted to arrays
- Elements with only text content are represented as strings
108 changes: 86 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/sibprogrammer/xq/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"io"
"os"
"path"
"strings"

"github.com/antchfx/xmlquery"
"github.com/sibprogrammer/xq/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// Version information
Expand Down Expand Up @@ -41,6 +44,7 @@ func NewRootCmd() *cobra.Command {

reader = os.Stdin
} else {
var err error
if reader, err = os.Open(args[len(args)-1]); err != nil {
return err
}
Expand All @@ -61,41 +65,47 @@ func NewRootCmd() *cobra.Command {
if cssAttr != "" && cssQuery == "" {
return errors.New("query option (-q) is missed for attribute selection")
}
jsonOutputMode, _ := cmd.Flags().GetBool("json")

pr, pw := io.Pipe()
errChan := make(chan error, 1)

go func() {
defer func() {
_ = pw.Close()
}()
defer close(errChan)
defer pw.Close()

var err error
if xPathQuery != "" {
err = utils.XPathQuery(reader, pw, xPathQuery, singleNode, options)
} else if cssQuery != "" {
err = utils.CSSQuery(reader, pw, cssQuery, cssAttr, options)
} else {
var contentType utils.ContentType
contentType, reader = detectFormat(cmd.Flags(), reader)

switch contentType {
case utils.ContentHtml:
err = utils.FormatHtml(reader, pw, indent, colors)
case utils.ContentXml:
err = utils.FormatXml(reader, pw, indent, colors)
case utils.ContentJson:
err = utils.FormatJson(reader, pw, indent, colors)
default:
err = fmt.Errorf("unknown content type: %v", contentType)
if jsonOutputMode {
err = processAsJSON(cmd.Flags(), reader, pw, contentType)
} else {
switch contentType {
case utils.ContentHtml:
err = utils.FormatHtml(reader, pw, indent, colors)
case utils.ContentXml:
err = utils.FormatXml(reader, pw, indent, colors)
case utils.ContentJson:
err = utils.FormatJson(reader, pw, indent, colors)
default:
err = fmt.Errorf("unknown content type: %v", contentType)
}
}
}

if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
errChan <- err
}()

return utils.PagerPrint(pr, cmd.OutOrStdout())
if err := utils.PagerPrint(pr, cmd.OutOrStdout()); err != nil {
return err
}

return <-errChan
},
}
}
Expand Down Expand Up @@ -127,6 +137,9 @@ func InitFlags(cmd *cobra.Command) {
"Extract an attribute value instead of node content for provided CSS query")
cmd.PersistentFlags().BoolP("node", "n", utils.GetConfig().Node,
"Return the node content instead of text")
cmd.PersistentFlags().BoolP("json", "j", false, "Output the result as JSON")
cmd.PersistentFlags().Bool("compact", false, "Compact JSON output (no indentation)")
cmd.PersistentFlags().IntP("depth", "d", -1, "Maximum nesting depth for JSON output (-1 for unlimited)")
}

func Execute() {
Expand Down Expand Up @@ -193,7 +206,7 @@ func detectFormat(flags *pflag.FlagSet, origReader io.Reader) (utils.ContentType
return utils.ContentHtml, origReader
}

buf := make([]byte, 10)
buf := make([]byte, 20)
length, err := origReader.Read(buf)
if err != nil {
return utils.ContentText, origReader
Expand All @@ -211,3 +224,54 @@ func detectFormat(flags *pflag.FlagSet, origReader io.Reader) (utils.ContentType

return utils.ContentXml, reader
}

func processAsJSON(flags *pflag.FlagSet, reader io.Reader, w io.Writer, contentType utils.ContentType) error {
var (
jsonCompact bool
jsonDepth int
result interface{}
)
jsonCompact, _ = flags.GetBool("compact")
if flags.Changed("depth") {
jsonDepth, _ = flags.GetInt("depth")
} else {
jsonDepth = -1
}

switch contentType {
case utils.ContentXml, utils.ContentHtml:
doc, err := xmlquery.Parse(reader)
if err != nil {
return fmt.Errorf("error while parsing XML: %w", err)
}
result = utils.NodeToJSON(doc, jsonDepth)
case utils.ContentJson:
decoder := json.NewDecoder(reader)
if err := decoder.Decode(&result); err != nil {
return fmt.Errorf("error while parsing JSON: %w", err)
}
default:
// Treat as plain text
content, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("error while reading content: %w", err)
}
result = map[string]interface{}{
"text": string(content),
}
}

var encoder *json.Encoder
if jsonCompact {
encoder = json.NewEncoder(w)
} else {
encoder = json.NewEncoder(w)
encoder.SetIndent("", " ")
}

if err := encoder.Encode(result); err != nil {
return fmt.Errorf("error while encoding JSON: %v", err)
}

return nil
}
107 changes: 105 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package cmd

import (
"bytes"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"encoding/json"
"fmt"
"path"
"strings"
"testing"

"github.com/sibprogrammer/xq/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

func execute(cmd *cobra.Command, args ...string) (string, error) {
Expand Down Expand Up @@ -87,3 +92,101 @@ func TestRootCmd(t *testing.T) {
_, err = execute(command, "--indent", "incorrect", xmlFilePath)
assert.ErrorContains(t, err, "invalid argument")
}

func TestProcessAsJSON(t *testing.T) {
tests := []struct {
name string
input string
contentType utils.ContentType
flags map[string]interface{}
expected map[string]interface{}
wantErr bool
}{
{
name: "Simple XML",
input: "<root><child>value</child></root>",
contentType: utils.ContentXml,
expected: map[string]interface{}{
"root": map[string]interface{}{
"child": "value",
},
},
},
{name: "Simple JSON",
input: `{"root": {"child": "value"}}`,
contentType: utils.ContentJson,
expected: map[string]interface{}{
"root": map[string]interface{}{
"child": "value",
},
},
},
{
name: "Simple HTML",
input: "<html><body><p>text</p></body></html>",
contentType: utils.ContentHtml,
expected: map[string]interface{}{
"html": map[string]interface{}{
"body": map[string]interface{}{
"p": "text",
},
},
},
},
{
name: "Plain text",
input: "text",
contentType: utils.ContentText,
expected: map[string]interface{}{
"text": "text",
},
},
{
name: "invalid input",
input: "thinking>\nI'll analyze each command and its output:\n</thinking>",
wantErr: true,
},
{
name: "combined",
expected: map[string]interface{}{
"#text": "Thank you\nBye.",
"thinking": "1. woop",
},
input: `Thank you
<thinking>
1. woop
</thinking>
Bye.`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up flags
flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
flags.Bool("compact", false, "")
flags.Int("depth", -1, "")
for name, v := range tt.flags {
_ = flags.Set(name, fmt.Sprint(v))
}

reader := strings.NewReader(tt.input)
var output bytes.Buffer

err := processAsJSON(flags, reader, &output, tt.contentType)

if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)

var resultMap map[string]interface{}
err = json.Unmarshal(output.Bytes(), &resultMap)
assert.NoError(t, err)

assert.Equal(t, tt.expected, resultMap)
}
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/antchfx/xmlquery v1.4.2
github.com/antchfx/xpath v1.3.2
github.com/fatih/color v1.18.0
github.com/google/go-cmp v0.6.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
26 changes: 26 additions & 0 deletions internal/utils/contenttype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/utils/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=ContentType
package utils
Loading

0 comments on commit 92cc6f9

Please sign in to comment.