Sha256: ad5cd743b80f63ab6a4ae18fda30ae14d76b620d3efa7aaea53e6a0ea2a55467

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

module ServiceContract
  module Avro
    class Type < AbstractType
      def name
        array? ? 
          "Array(#{subtype.name})" :
          complex? ?
            definition.name :
            definition.type.to_s
      end

      def fields
        definition.fields.map do |field|
          Parameter.new(field)
        end
      end

      def to_s
        return name unless union?
 
        union_types.map(&:name).join(", ")
      end

      def subtype
        return nil unless definition.respond_to?(:items)
        Type.build(definition.items)
      end

      def array?
        type_string == "array"
      end

      def self.build(definition)
        Type.new(definition)
      end

      def complex?
        type_string == "record"
      end

      def union?
        type_string == "union"
      end

      def valid_ruby_types
        case type_string
        when "array"
          [Array]
        when "int"
          [Fixnum]
        when "string"
          [String]
        when "float"
          [Float]
        when "boolean"
          [TrueClass, FalseClass]
        when "null"
          [NilClass]
        when "union"
          union_types.map(&:valid_ruby_types).flatten
        else # a complex type
          [Hash]
        end
      end

      protected

      def union_types
        definition.schemas.map{|schema| Type.build(schema)}
      end

      def type_string
        type = definition.type
        type = type.type_sym.to_s if type.respond_to?(:type_sym)
        type
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
service_contract-0.3.0 lib/service_contract/avro/type.rb
service_contract-0.2.1 lib/service_contract/avro/type.rb
service_contract-0.2.0 lib/service_contract/avro/type.rb
service_contract-0.1.1 lib/service_contract/avro/type.rb
service_contract-0.1.0 lib/service_contract/avro/type.rb