class TezosClient::Tools::AnnotsToType

Constants

TYPES_MAPPING

Public Instance Methods

execute() click to toggle source
# File lib/tezos_client/tools/annots_to_type.rb, line 25
def execute
  return { "prim" => typed_annots.values.first } if typed_annots.size == 1

  { "prim" => "pair", "args" => generate_type_args(ordered_annots) }
end

Private Instance Methods

generate_type_args(annots) click to toggle source
# File lib/tezos_client/tools/annots_to_type.rb, line 47
def generate_type_args(annots)
  annot = annots.pop
  annot_type = typed_annots[annot]

  unless annots.size == 1
    return [
      micheline_type(annot_type, annot),
      {
        "prim" => "pair",
        "args" => generate_type_args(annots)
      }
    ]
  end

  generated_args = [micheline_type(annot_type, annot)]
  annot = annots.pop
  annot_type = typed_annots[annot]
  generated_args.append(micheline_type(annot_type, annot))

  generated_args
end
micheline_type(annot_type, annot) click to toggle source
# File lib/tezos_client/tools/annots_to_type.rb, line 32
def micheline_type(annot_type, annot)
  if annot_type.to_s.start_with?("optional_")
    {
      "prim" => "option",
      "args" => [{ "prim" => annot_type.to_s.delete_prefix("optional_") }],
      "annots" => ["%#{annot}"]
    }
  else
    {
      "prim" => annot_type,
      "annots" => ["%#{annot}"]
    }
  end
end
ordered_annots() click to toggle source
# File lib/tezos_client/tools/annots_to_type.rb, line 69
def ordered_annots
  @ordered_annots ||= typed_annots.keys.sort.reverse
end
validate_types() click to toggle source
# File lib/tezos_client/tools/annots_to_type.rb, line 73
def validate_types
  allowed_types = TYPES_MAPPING.keys
  return if typed_annots.values.map{|type| type.to_s.delete_prefix("optional_").to_sym}.all? { |type| allowed_types.include? type }

  errors.add(:base, "The allowed types are: #{allowed_types.join(', ')}")
end