Class: BareTypes::Struct
- Defined in:
- lib/generative_testing/monkey_patch.rb,
lib/types.rb
Overview
region Agg Types
Instance Attribute Summary collapse
-
#mapping ⇒ Object
Returns the value of attribute mapping.
Class Method Summary collapse
Instance Method Summary collapse
- #==(otherType) ⇒ Object
- #[](key) ⇒ Object
- #create_input ⇒ Object
- #cycle_search(seen) ⇒ Object
- #decode(msg) ⇒ Object
- #encode(msg, buffer) ⇒ Object
- #finalize_references(schema) ⇒ Object
-
#initialize(symbolToType) ⇒ Struct
constructor
A new instance of Struct.
- #to_schema(buffer) ⇒ Object
Constructor Details
#initialize(symbolToType) ⇒ Struct
Returns a new instance of Struct.
623 624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/types.rb', line 623 def initialize(symbolToType) # Mapping from symbols to Bare types (or possibly symbols before finalizing) symbolToType.keys.each do |k| raise BareException.new("Struct keys must be symbols") unless k.is_a?(Symbol) if (!symbolToType[k].class.ancestors.include?(BaseType) && !symbolToType[k].is_a?(Symbol)) raise BareException.new("Struct values must be a BareTypes::TYPE or a symbol with the same name as a user defined type\nInstead got: #{symbolToType[k].inspect}") end raise VoidUsedOutsideTaggedSet.new("Void types may only be used as members of the set of types in a tagged union. Void type used as struct key") if symbolToType.class == BareTypes::Void end raise("Struct must have at least one field") if symbolToType.keys.size == 0 @mapping = symbolToType end |
Instance Attribute Details
#mapping ⇒ Object
Returns the value of attribute mapping.
584 585 586 |
# File 'lib/types.rb', line 584 def mapping @mapping end |
Class Method Details
.make(depth, names) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/generative_testing/monkey_patch.rb', line 213 def self.make(depth, names) hash = {} 0.upto(rand(STRUCT_FIELDS_MAX) + 1) do hash[create_user_type_name.to_sym] = get_type(depth + 1, names) end self.new(hash) end |
Instance Method Details
#==(otherType) ⇒ Object
598 599 600 601 602 603 604 |
# File 'lib/types.rb', line 598 def ==(otherType) return false unless otherType.class == BareTypes::Struct @mapping.each do |k, v| return false unless otherType.mapping[k] == v end true end |
#[](key) ⇒ Object
594 595 596 |
# File 'lib/types.rb', line 594 def [](key) @mapping[key] end |
#create_input ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/generative_testing/monkey_patch.rb', line 221 def create_input input = {} @mapping.keys.each do |name| input[name] = @mapping[name].create_input end input end |
#cycle_search(seen) ⇒ Object
586 587 588 589 590 591 592 |
# File 'lib/types.rb', line 586 def cycle_search(seen) seen.add(self) @mapping.values.each do |val| val.cycle_search(seen) end seen.pop end |
#decode(msg) ⇒ Object
644 645 646 647 648 649 650 651 652 |
# File 'lib/types.rb', line 644 def decode(msg) hash = Hash.new rest = msg @mapping.keys.each do |symbol| value, rest = @mapping[symbol].decode(rest) hash[symbol] = value end return hash, rest end |
#encode(msg, buffer) ⇒ Object
637 638 639 640 641 642 |
# File 'lib/types.rb', line 637 def encode(msg, buffer) @mapping.keys.each do |symbol| raise SchemaMismatch.new("All struct fields must be specified, missing: #{symbol.inspect}") unless msg.keys.include?(symbol) @mapping[symbol].encode(msg[symbol], buffer) end end |
#finalize_references(schema) ⇒ Object
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/types.rb', line 606 def finalize_references(schema) return if @finalized @finalized = true @mapping.each do |key, val| if val.is_a?(Symbol) @mapping[key] = Reference.new(val, schema[val]) begin @mapping[key].ref.finalize_references(schema) rescue NoMethodError => e puts "err" end else val.finalize_references(schema) end end end |
#to_schema(buffer) ⇒ Object
654 655 656 657 658 659 660 661 662 |
# File 'lib/types.rb', line 654 def to_schema(buffer) buffer << "{" @mapping.each do |symbol, type| buffer << " #{symbol}: " type.to_schema(buffer) buffer << "\n" end buffer << "}" end |