Class: Kafo::DataTypes::Struct

Inherits:
Kafo::DataType show all
Defined in:
lib/kafo/data_types/struct.rb

Instance Method Summary collapse

Methods inherited from Kafo::DataType

#condition_value, #dump_default, new_from_string, parse_hash, register_type, split_arguments, types, unregister_type

Constructor Details

#initialize(spec) ⇒ Struct

Returns a new instance of Struct.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/kafo/data_types/struct.rb', line 4

def initialize(spec)
  @spec = ::Hash[spec.map do |k,v|
    begin
      k = DataType.new_from_string(k)
    rescue ConfigurationException; end
    begin
      v = DataType.new_from_string(v)
    rescue ConfigurationException; end
    [k, v]
  end]
end

Instance Method Details

#multivalued?Boolean

Returns:



16
17
18
# File 'lib/kafo/data_types/struct.rb', line 16

def multivalued?
  true
end

#to_sObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kafo/data_types/struct.rb', line 20

def to_s
  "struct containing " + @spec.keys.map do |k|
    if k.is_a?(Optional)
      [k.inner_value, %{"#{k.inner_value}" (optional #{@spec[k]})}]
    elsif k.is_a?(NotUndef)
      [k.inner_value, %{"#{k.inner_value}" (required #{@spec[k]})}]
    else
      [k, %{"#{k}" (#{@spec[k]})}]
    end
  end.sort_by(&:first).map(&:last).join(', ')
end

#typecast(value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kafo/data_types/struct.rb', line 32

def typecast(value)
  if value.nil?
    nil
  elsif value.is_a?(::Hash)
    value
  elsif value == ['EMPTY_HASH']
    {}
  else
    ::Hash[[value].flatten.map do |kv|
      k, v = kv.split(':', 2)
      if (value_type = spec_value(k))
        [k, value_type.typecast(v)]
      else
        [k, v]
      end
    end]
  end
end

#valid?(input, errors = []) ⇒ Boolean

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kafo/data_types/struct.rb', line 51

def valid?(input, errors = [])
  unless input.is_a?(::Hash)
    errors << "#{input.inspect} is not a valid struct"
    return false
  end

  required_keys = @spec.keys.select { |k| k.is_a?(NotUndef) }.map { |k| k.inner_value }
  missing_keys = required_keys - input.keys
  errors << "Struct elements are missing: #{missing_keys.join(', ')}" unless missing_keys.empty?

  known_keys = @spec.keys.map { |k| spec_key_name(k) }
  extra_keys = input.keys - known_keys
  errors << "Struct elements are not permitted: #{extra_keys.join(', ')}" unless extra_keys.empty?

  value_errors = []

  # Only check values for optional keys if present
  optional_keys = @spec.keys.select { |k| k.is_a?(Optional) }.map { |k| k.inner_value }
  (optional_keys & input.keys).each { |k| spec_value(k).valid?(input[k], value_errors) }

  # For non-optional and non-required keys, assume nil/undef values if absent
  regular_keys = @spec.keys.select { |k| !k.is_a?(Optional) }.map { |k| spec_key_name(k) }
  regular_keys.each { |k| spec_value(k).valid?(input[k], value_errors) }

  errors << "Struct values are invalid: #{value_errors.join(', ')}" unless value_errors.empty?

  return errors.empty?
end