Skip to content

Commit

Permalink
Use empty IO on null input (#102)
Browse files Browse the repository at this point in the history
* Use empty IO on `null` input
* Bump versions
  • Loading branch information
Blacksmoke16 authored Dec 19, 2021
1 parent a5d00d6 commit f155670
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The built binary will be available as `./bin/oq`. This can be relocated elsewhe

```dockerfile
# Set an arg to store the oq version that should be installed.
ARG OQ_VERSION=1.3.0
ARG OQ_VERSION=1.3.1

# Grab the binary from the latest Github release and make it executable; placing it within /usr/local/bin. Can also put it elsewhere if you so desire.
RUN wget https://github.com/Blacksmoke16/oq/releases/download/v${OQ_VERSION}/oq-v${OQ_VERSION}-linux-x86_64 -O /usr/local/bin/oq && chmod +x /usr/local/bin/oq
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: oq
description: |
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
version: 1.3.0
version: 1.3.1

authors:
- George Dietrich <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: oq
version: '1.3.0'
version: '1.3.1'
summary: A performant, and portable jq wrapper to support formats other than JSON
description: |
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
Expand Down
26 changes: 16 additions & 10 deletions spec/oq_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,29 @@ describe OQ do
output.should eq "0\n"
end
end
end

describe "with a JSON object string" do
it "should return the correct output" do
run_binary(args: ["-cn", %([{"foo":"bar"},{"foo":"baz"}])]) do |output|
output.should eq %([{"foo":"bar"},{"foo":"baz"}]\n)
end
describe "with a JSON object string" do
it "should return the correct output" do
run_binary(args: ["-cn", %([{"foo":"bar"},{"foo":"baz"}])]) do |output|
output.should eq %([{"foo":"bar"},{"foo":"baz"}]\n)
end
end
end

describe "with input from STDIN" do
it "should return the correct output" do
run_binary(input: "foo", args: ["-n", "."]) do |output|
output.should eq "null\n"
end
describe "with input from STDIN" do
it "should return the correct output" do
run_binary(input: "foo", args: ["-n", "."]) do |output|
output.should eq "null\n"
end
end
end

it "should not block waiting for input" do
run_binary(input: Process::Redirect::Inherit, args: ["-n", "."]) do |output|
output.should eq "null\n"
end
end
end

describe "with a custom indent value with JSON" do
Expand Down
13 changes: 10 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ require "spec"
require "../src/oq"

# Runs the the binary with the given *name* and *args*.
def run_binary(input : String? = nil, name : String = "bin/oq", args : Array(String) = [] of String, *, success : Bool = true, file = __FILE__, line = __LINE__, & : String, Process::Status, String -> Nil)
def run_binary(input : String | Process::Redirect | Nil = nil, name : String = "bin/oq", args : Array(String) = [] of String, *, success : Bool = true, file = __FILE__, line = __LINE__, & : String, Process::Status, String -> Nil)
buffer_io = IO::Memory.new
error_io = IO::Memory.new
input_io = IO::Memory.new
input_io << input if input
status = Process.run(name, args, output: buffer_io, input: input_io.rewind, error: error_io)

if input.is_a? Process::Redirect
input_io = input
else
input_io << input if input
input_io = input_io.rewind
end

status = Process.run(name, args, output: buffer_io, input: input_io, error: error_io)

if success
status.success?.should be_true, file: file, line: line
Expand Down
13 changes: 9 additions & 4 deletions src/oq.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "./converters/*"

# A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
module OQ
VERSION = "1.3.0"
VERSION = "1.3.1"

# The support formats that can be converted to/from.
enum Format
Expand Down Expand Up @@ -83,6 +83,9 @@ module OQ
# If a tab for each indentation level instead of spaces.
property? tab : Bool

# Do not read any input, using `null` as the singular input value.
property? null : Bool

# If XML namespaces should be parsed as well.
# TODO: Remove this in oq 2.0 as it'll becomethe default.
property? xmlns : Bool
Expand All @@ -107,6 +110,7 @@ module OQ
@xml_item : String = "item",
@indent : Int32 = 2,
@tab : Bool = false,
@null : Bool = false,
@xmlns : Bool = false
)
end
Expand Down Expand Up @@ -157,9 +161,10 @@ module OQ
# TODO: Remove this in oq 2.x
raise ArgumentError.new "The `--xml-namespace-alias` option must be used with the `--xmlns` option." if !@xmlns && !@xml_namespaces.empty?

# Replace the *input* with a fake `ARGF` `IO` to handle both file and `IO` inputs
# in case `ARGV` is not being used for the input arguments.
input = IO::ARGF.new input_args, input
# Replace the *input* with a fake `ARGF` `IO` to handle both file and `IO` inputs in case `ARGV` is not being used for the input arguments.
#
# If using `null` input, set the input to an empty memory `IO` to essentially consume nothing.
input = @null ? IO::Memory.new : IO::ARGF.new input_args, input

input_read, input_write = IO.pipe
output_read, output_write = IO.pipe
Expand Down
1 change: 1 addition & 0 deletions src/oq_cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OptionParser.parse do |parser|
parser.on("-o FORMAT", "--output FORMAT", "Format of the output data. Supported formats: #{OQ::Format}") { |format| (f = OQ::Format.parse?(format)) ? processor.output_format = f : abort "Invalid output format: '#{format}'" }
parser.on("--indent NUMBER", "Use the given number of spaces for indentation (JSON/XML only).") { |n| processor.indent = n.to_i; processor.add_arg "--indent"; processor.add_arg n }
parser.on("--tab", "Use a tab for each indentation level instead of two spaces.") { processor.tab = true; processor.add_arg "--tab" }
parser.on("-n", "--null-input", "Don't read any input at all, running the filter once using `null` as the input.") { processor.null = true; processor.add_arg "--null-input" }
parser.on("--no-prolog", "Whether the XML prolog should be emitted if converting to XML.") { processor.xml_prolog = false }
parser.on("--xml-item NAME", "The name for XML array elements without keys.") { |i| processor.xml_item = i }
parser.on("--xmlns", "If XML namespaces should be parsed. NOTE: This will become the default in oq 2.x.") { processor.xmlns = true }
Expand Down

0 comments on commit f155670

Please sign in to comment.