Skip to content

Commit 3e00f4e

Browse files
author
Kevin Paulisse
committed
Wrap fact override calls in API object
1 parent 240e5ea commit 3e00f4e

3 files changed

Lines changed: 4 additions & 112 deletions

File tree

lib/octocatalog-diff/cli/fact_override.rb

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,80 +13,7 @@ class FactOverride
1313
# @param input [String] Input in the format: key=(data type)value
1414
# @return [OctocatalogDiff::API::V1::FactOverride] Constructed override object
1515
def self.fact_override(input, key = nil)
16-
# Normally the input will be a string in the format key=(data type)value where the data
17-
# type is optional and the parentheses are literal. Example:
18-
# foo=1 (auto-determine data type - in this case it would be a fixnum)
19-
# foo=(fixnum)1 (will be a fixnum)
20-
# foo=(string)1 (will be '1' the string)
21-
# If input is not a string, we can still construct the object if the key is given.
22-
# That input would come directly from code and not from the command line, since inputs
23-
# from the command line are always strings.
24-
if key.nil? && input.is_a?(String)
25-
unless input.include?('=')
26-
raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format"
27-
end
28-
key, raw_value = input.strip.split('=', 2)
29-
value = parsed_value(raw_value)
30-
OctocatalogDiff::API::V1::FactOverride.new(fact_name: key, value: value)
31-
elsif key.nil?
32-
message = "Define a key when the input is not a string (#{input.class} => #{input.inspect})"
33-
raise ArgumentError, message
34-
else
35-
OctocatalogDiff::API::V1::FactOverride.new(fact_name: key, value: input)
36-
end
37-
end
38-
39-
# Guess the datatype from a particular input
40-
# @param input [String] Input in string format
41-
# @return [?] Output in appropriate format
42-
def self.parsed_value(input)
43-
# If data type is explicitly given
44-
if input =~ /^\((\w+)\)(.*)$/m
45-
datatype = Regexp.last_match(1)
46-
value = Regexp.last_match(2)
47-
return convert_to_data_type(datatype.downcase, value)
48-
end
49-
50-
# Guess data type
51-
return input.to_i if input =~ /^-?\d+$/
52-
return input.to_f if input =~ /^-?\d*\.\d+$/
53-
return true if input.casecmp('true').zero?
54-
return false if input.casecmp('false').zero?
55-
input
56-
end
57-
58-
# Handle data type that's explicitly given
59-
# @param datatype [String] Data type (as a string)
60-
# @param value [String] Value given
61-
# @return [?] Value converted to specified data type
62-
def self.convert_to_data_type(datatype, value)
63-
return value if datatype == 'string'
64-
return parse_json(value) if datatype == 'json'
65-
return nil if datatype == 'nil'
66-
if datatype == 'fixnum'
67-
return Regexp.last_match(1).to_i if value =~ /^(-?\d+)$/
68-
raise ArgumentError, "Illegal fixnum '#{value}'"
69-
end
70-
if datatype == 'float'
71-
return Regexp.last_match(1).to_f if value =~ /^(-?\d*\.\d+)$/
72-
return Regexp.last_match(1).to_f if value =~ /^(-?\d+)$/
73-
raise ArgumentError, "Illegal float '#{value}'"
74-
end
75-
if datatype == 'boolean'
76-
return true if value.casecmp('true').zero?
77-
return false if value.casecmp('false').zero?
78-
raise ArgumentError, "Illegal boolean '#{value}'"
79-
end
80-
raise ArgumentError, "Unknown data type '#{datatype}'"
81-
end
82-
83-
# Parse JSON value
84-
# @param input [String] Input, hopefully in JSON format
85-
# @return [?] Output data structure
86-
def self.parse_json(input)
87-
JSON.parse(input)
88-
rescue JSON::ParserError => exc
89-
raise JSON::ParserError, "Failed to parse JSON: input=#{input} error=#{exc}"
16+
OctocatalogDiff::API::V1::Override.create_from_input(input, key)
9017
end
9118
end
9219
end

spec/octocatalog-diff/tests/cli/fact_override_spec.rb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -259,39 +259,4 @@
259259
end
260260
end
261261
end
262-
263-
context 'with non-string input' do
264-
describe '#new' do
265-
it 'should pass through a fixnum without manipulation' do
266-
testobj = OctocatalogDiff::Cli::FactOverride.fact_override(42, 'foo')
267-
expect(testobj.key).to eq('foo')
268-
expect(testobj.value).to eq(42)
269-
end
270-
271-
it 'should pass through a float without manipulation' do
272-
testobj = OctocatalogDiff::Cli::FactOverride.fact_override(42.15, 'foo')
273-
expect(testobj.key).to eq('foo')
274-
expect(testobj.value).to eq(42.15)
275-
end
276-
277-
it 'should pass through a boolean without manipulation' do
278-
testobj = OctocatalogDiff::Cli::FactOverride.fact_override(false, 'foo')
279-
expect(testobj.key).to eq('foo')
280-
expect(testobj.value).to eq(false)
281-
end
282-
283-
it 'should pass through a nil without manipulation' do
284-
testobj = OctocatalogDiff::Cli::FactOverride.fact_override(nil, 'foo')
285-
expect(testobj.key).to eq('foo')
286-
expect(testobj.value).to eq(nil)
287-
end
288-
289-
it 'should pass through a hash without manipulation' do
290-
arg = { 'foo' => 'bar', 'baz' => [1, 2, 3, 4, 5] }
291-
testobj = OctocatalogDiff::Cli::FactOverride.fact_override(arg, 'foo')
292-
expect(testobj.key).to eq('foo')
293-
expect(testobj.value).to eq(arg)
294-
end
295-
end
296-
end
297262
end

spec/octocatalog-diff/tests/cli_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
expect(options[:from_fact_override]).to be_a_kind_of(Array)
184184
expect(options[:from_fact_override].size).to eq(1)
185185
ffo = options[:from_fact_override].first
186-
expect(ffo).to be_a_kind_of(OctocatalogDiff::API::V1::FactOverride)
186+
expect(ffo).to be_a_kind_of(OctocatalogDiff::API::V1::Override)
187187
expect(ffo.key).to eq('foo')
188188
expect(ffo.value).to eq('bar')
189189
end
@@ -195,10 +195,10 @@
195195
expect(options[:to_fact_override].size).to eq(2)
196196

197197
tfo = options[:to_fact_override]
198-
expect(tfo[0]).to be_a_kind_of(OctocatalogDiff::API::V1::FactOverride)
198+
expect(tfo[0]).to be_a_kind_of(OctocatalogDiff::API::V1::Override)
199199
expect(tfo[0].key).to eq('foo')
200200
expect(tfo[0].value).to eq('bar')
201-
expect(tfo[1]).to be_a_kind_of(OctocatalogDiff::API::V1::FactOverride)
201+
expect(tfo[1]).to be_a_kind_of(OctocatalogDiff::API::V1::Override)
202202
expect(tfo[1].key).to eq('baz')
203203
expect(tfo[1].value).to eq('buzz')
204204
end

0 commit comments

Comments
 (0)