class Lacerda::Conversion::DataStructure::Member::Type

Public Class Methods

new(type_definition, is_required, scope) click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 6
def initialize(type_definition, is_required, scope)
  @type_definition = type_definition
  @scope = scope
  @type_name = type_definition['element']
  @is_required = is_required
end

Public Instance Methods

array_type() click to toggle source

bui As there are specied types in the array, `nil` should not be a valid value and therefore required should be true.

# File lib/lacerda/conversion/data_structure/member/type.rb, line 33
def array_type
  return unless array?
  if nested_types.size == 1 && PRIMITIVES.include?(nested_types.first)
    primitive(nested_types.first, true) 
  else
    oneOf(nested_types, true)
  end
end
object?() click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 13
def object?
  @type_name == 'object'
end
to_hash() click to toggle source

A type is transformed to json schema either as a primitive:

{ "type" => ["string"] }
{ "type" => ["string", "null"] }

Or as a oneOf. $ref are always within a oneOf

{"oneOf"=>[{"$ref"=>"#/definitions/tag"}, {"type"=>"null"}]}
# File lib/lacerda/conversion/data_structure/member/type.rb, line 22
def to_hash
  if PRIMITIVES.include?(@type_name)
    primitive(@type_name, required?)
  else
    oneOf([@type_name], required?)
  end
end

Private Instance Methods

array?() click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 44
def array?
  @type_name == 'array'
end
basic_type(type_name, is_required = required?) click to toggle source

A basic type is either a primitive type with exactly 1 primitive type

{'type' => [boolean] }

a reference

{ '$ref' => "#/definitions/name" }

or an object if the type in not there

{ 'type' => object }

Basic types don't care about being required or not.

# File lib/lacerda/conversion/data_structure/member/type.rb, line 55
def basic_type(type_name, is_required = required?)
  if PRIMITIVES.include?(type_name)
    primitive(type_name, is_required)
  else
    reference(type_name)
  end
end
nested_types() click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 90
def nested_types
  error_msg = "This DataStructure::Member is a #{@type_name}, not "\
    'an array, so it cannot have nested types'
  raise error_msg unless array?
  @type_definition['content'].map{|vc| vc['element'] }.uniq
end
oneOf(types, is_required) click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 63
def oneOf(types, is_required)
  types = types.map { |type_name| basic_type(type_name,is_required) }
  types << { 'type' => 'null' } unless is_required
  {
    'oneOf' => types.uniq
  }
end
primitive(type_name, is_required) click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 75
def primitive(type_name, is_required) 
  types = [type_name]
  types << 'null' unless is_required
  if type_name == 'enum'
    enum_values = @type_definition['content'].map { |i| i['content'] }
    { 'type' => types, 'enum' => enum_values }
  else
    { 'type' => types }
  end
end
reference(type_name) click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 71
def reference(type_name)
  {'$ref' => "#/definitions/#{Lacerda::Conversion::DataStructure.scope(@scope, type_name)}" }
end
required?() click to toggle source
# File lib/lacerda/conversion/data_structure/member/type.rb, line 86
def required?
  @is_required
end