@@ -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
0 commit comments