1

Conditional pretty json output

This commit is contained in:
Alex Yatskov 2016-08-03 09:12:31 -07:00
parent 9a436ce9a0
commit 9708aef745
2 changed files with 37 additions and 13 deletions

View File

@ -81,11 +81,20 @@ func buildDictJson(entries []dictEntry, entities map[string]string) dictJson {
return dict return dict
} }
func outputJson(entries []dictEntry, entities map[string]string, writer io.Writer) error { func outputJson(writer io.Writer, entries []dictEntry, entities map[string]string, pretty bool) error {
dict := buildDictJson(entries, entities) dict := buildDictJson(entries, entities)
bytes, err := json.MarshalIndent(dict, "", " ") var (
// bytes, err := json.Marshal(dict) bytes []byte
err error
)
if pretty {
bytes, err = json.MarshalIndent(dict, "", " ")
} else {
bytes, err = json.Marshal(dict)
}
if err != nil { if err != nil {
return err return err
} }
@ -208,7 +217,7 @@ func convertEdictEntry(edictEntry jmdict.EdictEntry) []dictEntry {
return entries return entries
} }
func processEnamdict(reader io.Reader, writer io.Writer) error { func processEnamdict(writer io.Writer, reader io.Reader, flags int) error {
enamdictEntries, entities, err := jmdict.LoadEnamdict(reader, false) enamdictEntries, entities, err := jmdict.LoadEnamdict(reader, false)
if err != nil { if err != nil {
return err return err
@ -219,10 +228,10 @@ func processEnamdict(reader io.Reader, writer io.Writer) error {
entries = append(entries, convertEnamdictEntry(enamdictEntry)...) entries = append(entries, convertEnamdictEntry(enamdictEntry)...)
} }
return outputJson(entries, entities, writer) return outputJson(writer, entries, entities, flags&flagPrettyJson == flagPrettyJson)
} }
func processEdict(reader io.Reader, writer io.Writer) error { func processEdict(writer io.Writer, reader io.Reader, flags int) error {
edictEntries, entities, err := jmdict.LoadEdict(reader, false) edictEntries, entities, err := jmdict.LoadEdict(reader, false)
if err != nil { if err != nil {
return err return err
@ -233,5 +242,5 @@ func processEdict(reader io.Reader, writer io.Writer) error {
entries = append(entries, convertEdictEntry(edictEntry)...) entries = append(entries, convertEdictEntry(edictEntry)...)
} }
return outputJson(entries, entities, writer) return outputJson(writer, entries, entities, flags&flagPrettyJson == flagPrettyJson)
} }

27
main.go
View File

@ -24,6 +24,7 @@ package main
import ( import (
"errors" "errors"
"flag"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -31,12 +32,18 @@ import (
"path" "path"
) )
const (
flagPrettyJson = 1 << iota
)
func usage() { func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s file_format input_file output_file\n\n", path.Base(os.Args[0])) fmt.Fprintf(os.Stderr, "Usage: %s file_format input_file output_file\n\n", path.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "Parameters:\n")
flag.PrintDefaults()
} }
func process(fileFormat, inputFile, outputFile string) error { func process(fileFormat, inputFile, outputFile string, flags int) error {
handlers := map[string]func(io.Reader, io.Writer) error{ handlers := map[string]func(io.Writer, io.Reader, int) error{
"edict": processEdict, "edict": processEdict,
"enamdict": processEnamdict, "enamdict": processEnamdict,
} }
@ -56,14 +63,22 @@ func process(fileFormat, inputFile, outputFile string) error {
return err return err
} }
return handler(input, output) return handler(output, input, flags)
} }
func main() { func main() {
args := os.Args[1:] prettyJson := flag.Bool("prettyJson", false, "output prettified json")
if len(args) == 3 { flag.Usage = usage
if err := process(args[0], args[1], args[2]); err != nil { flag.Parse()
var flags int
if *prettyJson {
flags |= flagPrettyJson
}
if flag.NArg() == 3 {
if err := process(flag.Arg(0), flag.Arg(1), flag.Arg(2), flags); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {