|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'json' |
| 4 | + |
| 5 | +module OctocatalogDiff |
| 6 | + module API |
| 7 | + module V1 |
| 8 | + # Sets up the override of a fact or ENC parameter during catalog compilation. |
| 9 | + class Override |
| 10 | + # Accessors |
| 11 | + attr_reader :key, :value |
| 12 | + |
| 13 | + # Constructor: Accepts a key and value. |
| 14 | + # @param input [Hash] Must contain :key and :value |
| 15 | + def initialize(input) |
| 16 | + @key = input.fetch(:key) |
| 17 | + @value = parsed_value(input.fetch(:value)) |
| 18 | + end |
| 19 | + |
| 20 | + # Initialize from a parsed command line |
| 21 | + # @param input [String] Command line parameter |
| 22 | + # @return [OctocatalogDiff::API::V1::Override] Initialized object |
| 23 | + def self.create_from_input(input, key = nil) |
| 24 | + # Normally the input will be a string in the format key=(data type)value where the data |
| 25 | + # type is optional and the parentheses are literal. Example: |
| 26 | + # foo=1 (auto-determine data type - in this case it would be a fixnum) |
| 27 | + # foo=(fixnum)1 (will be a fixnum) |
| 28 | + # foo=(string)1 (will be '1' the string) |
| 29 | + # If input is not a string, we can still construct the object if the key is given. |
| 30 | + # That input would come directly from code and not from the command line, since inputs |
| 31 | + # from the command line are always strings. |
| 32 | + if key.nil? && input.is_a?(String) |
| 33 | + unless input.include?('=') |
| 34 | + raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format" |
| 35 | + end |
| 36 | + k, v = input.strip.split('=', 2).map(&:strip) |
| 37 | + new(key: k, value: v) |
| 38 | + elsif key.nil? |
| 39 | + message = "Define a key when the input is not a string (#{input.class} => #{input.inspect})" |
| 40 | + raise ArgumentError, message |
| 41 | + else |
| 42 | + new(key: key, value: input) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + # Guess the datatype from a particular input |
| 49 | + # @param input [String] Input in string format |
| 50 | + # @return [?] Output in appropriate format |
| 51 | + def parsed_value(input) |
| 52 | + # If data type is explicitly given |
| 53 | + if input =~ /^\((\w+)\)(.*)$/m |
| 54 | + datatype = Regexp.last_match(1) |
| 55 | + value = Regexp.last_match(2) |
| 56 | + return convert_to_data_type(datatype.downcase, value) |
| 57 | + end |
| 58 | + |
| 59 | + # Guess data type |
| 60 | + return input.to_i if input =~ /^-?\d+$/ |
| 61 | + return input.to_f if input =~ /^-?\d*\.\d+$/ |
| 62 | + return true if input.casecmp('true').zero? |
| 63 | + return false if input.casecmp('false').zero? |
| 64 | + input |
| 65 | + end |
| 66 | + |
| 67 | + # Handle data type that's explicitly given |
| 68 | + # @param datatype [String] Data type (as a string) |
| 69 | + # @param value [String] Value given |
| 70 | + # @return [?] Value converted to specified data type |
| 71 | + def convert_to_data_type(datatype, value) |
| 72 | + return value if datatype == 'string' |
| 73 | + return parse_json(value) if datatype == 'json' |
| 74 | + return nil if datatype == 'nil' |
| 75 | + if datatype == 'fixnum' |
| 76 | + return Regexp.last_match(1).to_i if value =~ /^(-?\d+)$/ |
| 77 | + raise ArgumentError, "Illegal fixnum '#{value}'" |
| 78 | + end |
| 79 | + if datatype == 'float' |
| 80 | + return Regexp.last_match(1).to_f if value =~ /^(-?\d*\.\d+)$/ |
| 81 | + return Regexp.last_match(1).to_f if value =~ /^(-?\d+)$/ |
| 82 | + raise ArgumentError, "Illegal float '#{value}'" |
| 83 | + end |
| 84 | + if datatype == 'boolean' |
| 85 | + return true if value.casecmp('true').zero? |
| 86 | + return false if value.casecmp('false').zero? |
| 87 | + raise ArgumentError, "Illegal boolean '#{value}'" |
| 88 | + end |
| 89 | + raise ArgumentError, "Unknown data type '#{datatype}'" |
| 90 | + end |
| 91 | + |
| 92 | + # Parse JSON value |
| 93 | + # @param input [String] Input, hopefully in JSON format |
| 94 | + # @return [?] Output data structure |
| 95 | + def parse_json(input) |
| 96 | + JSON.parse(input) |
| 97 | + rescue JSON::ParserError => exc |
| 98 | + raise JSON::ParserError, "Failed to parse JSON: input=#{input} error=#{exc}" |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments