class Dry::Swagger::StructParser

Constants

PREDICATE_TYPES

Attributes

keys[R]

Public Class Methods

new() click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 16
def initialize
  @keys = {}
  @config = Dry::Swagger::Config::StructConfiguration
end

Public Instance Methods

call(dto, &block) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 25
def call(dto, &block)
  @keys = {}
  visit(dto.schema.to_ast)
  instance_eval(&block) if block_given?
  self
end
to_h() click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 21
def to_h
  { keys: keys }
end
to_swagger() click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 139
def to_swagger
  DocumentationGenerator.new(@config).generate_documentation(keys)
end
visit(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 32
def visit(node, opts = {})
  meth, rest = node
  public_send(:"visit_#{meth}", rest, opts)
end
visit_and(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 107
def visit_and(node, opts = {})
  left, right = node

  visit(left, opts)
  visit(right, opts)
end
visit_array(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 134
def visit_array(node, opts = {})
  opts[:array] = true
  visit(node[0], opts)
end
visit_constrained(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 78
def visit_constrained(node, opts = {})
  node.each {|it| visit(it, opts) }
end
visit_constructor(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 37
def visit_constructor(node, opts = {})
  visit(node[0], opts)
end
visit_enum(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 114
def visit_enum(node, opts = {})
  visit(node[0], opts)
end
visit_key(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 71
def visit_key(node, opts = {})
  name, required, rest = node
  opts[:key] = name
  opts[:required] = required
  visit(rest, opts)
end
visit_nominal(_node, _opts) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 82
def visit_nominal(_node, _opts); end
visit_predicate(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 84
def visit_predicate(node, opts = {})
  name, rest = node
  type = rest[0][1]

  if name.equal?(:type?)
    type = type.to_s.to_sym
    return unless PREDICATE_TYPES[type]

    type_definition = {
        type: PREDICATE_TYPES[type],
        required: opts.fetch(:required),
        @config.nullable_type => opts.fetch(:nullable, false)
    }

    type_definition[:array] = opts[:array] if opts[:array]

    keys[opts[:key]] = type_definition
  elsif name.equal?(:included_in?)
    type += [nil] if opts.fetch(:nullable, false)
    keys[opts[:key]][:enum] = type
  end
end
visit_schema(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 41
def visit_schema(node, opts = {})
  target = (key = opts[:key]) ? self.class.new : self

  required = opts.fetch(:required, true)
  nullable = opts.fetch(:nullable, false)

  node[0].each do |child|
    target.visit(child)
  end

  return unless key

  target_info =  target.to_h if opts[:member]

  type = opts[:array]? 'array' : 'hash'

  definition = {
      type: type,
      required: required,
      @config.nullable_type => nullable,
      **target_info
  }

  if opts[:oneOf]
    keys[key] = keys[key] ? keys[key] << definition : [definition]
  else
    keys[key] = definition
  end
end
visit_struct(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 128
def visit_struct(node, opts = {})
  opts[:member] = true

  visit(node[1], opts)
end
visit_sum(node, opts = {}) click to toggle source
# File lib/dry/swagger/struct_parser.rb, line 118
def visit_sum(node, opts = {})
  if node[0][0].equal?(:constrained)
    opts[:nullable] = true
    visit(node[1], opts) # ignore NilClass constrained
  elsif node[0][0].equal?(:struct) || node[0][0].equal?(:sum)
    opts[:oneOf] = true
    node.each { |child| visit(child, opts) unless child.is_a?(Hash) }
  end
end