Skip to content

Commit f155670

Browse files
authored
Use empty IO on null input (#102)
* Use empty IO on `null` input * Bump versions
1 parent a5d00d6 commit f155670

File tree

7 files changed

+39
-20
lines changed

7 files changed

+39
-20
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The built binary will be available as `./bin/oq`. This can be relocated elsewhe
6868

6969
```dockerfile
7070
# Set an arg to store the oq version that should be installed.
71-
ARG OQ_VERSION=1.3.0
71+
ARG OQ_VERSION=1.3.1
7272

7373
# 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.
7474
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

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: oq
33
description: |
44
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
55
6-
version: 1.3.0
6+
version: 1.3.1
77

88
authors:
99
- George Dietrich <[email protected]>

snap/snapcraft.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: oq
2-
version: '1.3.0'
2+
version: '1.3.1'
33
summary: A performant, and portable jq wrapper to support formats other than JSON
44
description: |
55
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.

spec/oq_spec.cr

+16-10
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,29 @@ describe OQ do
135135
output.should eq "0\n"
136136
end
137137
end
138+
end
138139

139-
describe "with a JSON object string" do
140-
it "should return the correct output" do
141-
run_binary(args: ["-cn", %([{"foo":"bar"},{"foo":"baz"}])]) do |output|
142-
output.should eq %([{"foo":"bar"},{"foo":"baz"}]\n)
143-
end
140+
describe "with a JSON object string" do
141+
it "should return the correct output" do
142+
run_binary(args: ["-cn", %([{"foo":"bar"},{"foo":"baz"}])]) do |output|
143+
output.should eq %([{"foo":"bar"},{"foo":"baz"}]\n)
144144
end
145145
end
146+
end
146147

147-
describe "with input from STDIN" do
148-
it "should return the correct output" do
149-
run_binary(input: "foo", args: ["-n", "."]) do |output|
150-
output.should eq "null\n"
151-
end
148+
describe "with input from STDIN" do
149+
it "should return the correct output" do
150+
run_binary(input: "foo", args: ["-n", "."]) do |output|
151+
output.should eq "null\n"
152152
end
153153
end
154154
end
155+
156+
it "should not block waiting for input" do
157+
run_binary(input: Process::Redirect::Inherit, args: ["-n", "."]) do |output|
158+
output.should eq "null\n"
159+
end
160+
end
155161
end
156162

157163
describe "with a custom indent value with JSON" do

spec/spec_helper.cr

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ require "spec"
22
require "../src/oq"
33

44
# Runs the the binary with the given *name* and *args*.
5-
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)
5+
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)
66
buffer_io = IO::Memory.new
77
error_io = IO::Memory.new
88
input_io = IO::Memory.new
9-
input_io << input if input
10-
status = Process.run(name, args, output: buffer_io, input: input_io.rewind, error: error_io)
9+
10+
if input.is_a? Process::Redirect
11+
input_io = input
12+
else
13+
input_io << input if input
14+
input_io = input_io.rewind
15+
end
16+
17+
status = Process.run(name, args, output: buffer_io, input: input_io, error: error_io)
1118

1219
if success
1320
status.success?.should be_true, file: file, line: line

src/oq.cr

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require "./converters/*"
66

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

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

86+
# Do not read any input, using `null` as the singular input value.
87+
property? null : Bool
88+
8689
# If XML namespaces should be parsed as well.
8790
# TODO: Remove this in oq 2.0 as it'll becomethe default.
8891
property? xmlns : Bool
@@ -107,6 +110,7 @@ module OQ
107110
@xml_item : String = "item",
108111
@indent : Int32 = 2,
109112
@tab : Bool = false,
113+
@null : Bool = false,
110114
@xmlns : Bool = false
111115
)
112116
end
@@ -157,9 +161,10 @@ module OQ
157161
# TODO: Remove this in oq 2.x
158162
raise ArgumentError.new "The `--xml-namespace-alias` option must be used with the `--xmlns` option." if !@xmlns && !@xml_namespaces.empty?
159163

160-
# Replace the *input* with a fake `ARGF` `IO` to handle both file and `IO` inputs
161-
# in case `ARGV` is not being used for the input arguments.
162-
input = IO::ARGF.new input_args, input
164+
# 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.
165+
#
166+
# If using `null` input, set the input to an empty memory `IO` to essentially consume nothing.
167+
input = @null ? IO::Memory.new : IO::ARGF.new input_args, input
163168

164169
input_read, input_write = IO.pipe
165170
output_read, output_write = IO.pipe

src/oq_cli.cr

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ OptionParser.parse do |parser|
2828
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}'" }
2929
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 }
3030
parser.on("--tab", "Use a tab for each indentation level instead of two spaces.") { processor.tab = true; processor.add_arg "--tab" }
31+
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" }
3132
parser.on("--no-prolog", "Whether the XML prolog should be emitted if converting to XML.") { processor.xml_prolog = false }
3233
parser.on("--xml-item NAME", "The name for XML array elements without keys.") { |i| processor.xml_item = i }
3334
parser.on("--xmlns", "If XML namespaces should be parsed. NOTE: This will become the default in oq 2.x.") { processor.xmlns = true }

0 commit comments

Comments
 (0)