Class: BareTypes::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/generative_testing/monkey_patch.rb,
lib/types.rb

Overview

endregion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ Schema

Returns a new instance of Schema.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/types.rb', line 17

def initialize(types)
  @types = types.map { |k, v| [k.to_sym, v] }.to_h
  @types.each do |k, v|
    unless k.is_a?(Symbol)
      raise("Keys to a schema must be symbols")
    end
    if v.nil?
      raise("Schema values cannot be nil")
    end
  end

  # Resolve references in schema
  # type A u8
  # type B A
  # type C B
  # first  loop would find B and make it a reference to A
  # second loop would find C and make it a reference to B
  progress = true
  remaining = @types.keys.to_a
  while progress
    progress = false
    remaining.each do |key|
      val = @types[key]
      if val.is_a?(Symbol) && !@types[val].is_a?(Symbol)
        @types[key.to_sym] = BareTypes::Reference.new(key, @types[val])
        progress = true
      else
      end
    end
  end

  @types.each do |key, val|
    if val.is_a?(Symbol)
      raise ReferenceException.new("Your types contain an unresolved reference '#{val}'.")
    end
  end

  @types.values.each do |val|
    val.finalize_references(@types)
  end

  @types.each do |key, val|
    val.cycle_search(SeenList.new)
  end
end

Instance Attribute Details

#typesObject

Returns the value of attribute types.



15
16
17
# File 'lib/types.rb', line 15

def types
  @types
end

Class Method Details

.makeObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/generative_testing/monkey_patch.rb', line 241

def self.make
  schema = nil
  loop do
    names = []
    schema = {}
    0.upto(rand(10)+1) do
      names << create_user_type_name.to_sym
    end
    names.each do |name|
      without_this_name = names.select { |n| n != name }
      schema[name] = get_type(0, without_this_name, false)
    end
    begin
      schema = Bare.Schema(schema)
    rescue CircularSchema
      next
    end
    break
  end
  schema
end

Instance Method Details

#==(otherSchema) ⇒ Object



63
64
65
66
# File 'lib/types.rb', line 63

def ==(otherSchema)
  return false unless otherSchema.is_a?(BareTypes::Schema)
  @types == otherSchema.types
end

#[](key) ⇒ Object



84
85
86
# File 'lib/types.rb', line 84

def [](key)
  return @types[key]
end

#create_inputObject



233
234
235
236
237
238
239
# File 'lib/generative_testing/monkey_patch.rb', line 233

def create_input
  input = {}
  @types.each do |key, type|
    input[key] = type.create_input
  end
  input
end

#to_sObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/types.rb', line 68

def to_s
  buffer = ""
  @types.each do |name, type|
    if type.is_a?(BareTypes::Enum)
      buffer << "enum #{name} "
      type.to_schema(buffer)
      buffer << "\n"
    else
      buffer << "type #{name} "
      type.to_schema(buffer)
      buffer << "\n"
    end
  end
  buffer
end