Class: Anchor::TypeScript::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/anchor/type_script/serializer.rb

Class Method Summary collapse

Class Method Details

.type_string(type, depth = 1) ⇒ Object

rubocop:disable Layout/LineLength

[View source]

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/anchor/type_script/serializer.rb', line 6

def type_string(type, depth = 1)
  case type
  when Anchor::Types::String.singleton_class then "string"
  when Anchor::Types::BigDecimal.singleton_class then "string"
  when Anchor::Types::Float.singleton_class then "number"
  when Anchor::Types::Integer.singleton_class then "number"
  when Anchor::Types::Boolean.singleton_class then "boolean"
  when Anchor::Types::Null.singleton_class then "null"
  when Anchor::Types::Record, Anchor::Types::Record.singleton_class then "Record<string, #{type_string(type.try(:value_type) || Anchor::Types::Unknown)}>"
  when Anchor::Types::Union then type.types.map { |type| type_string(type, depth) }.join(" | ")
  when Anchor::Types::Maybe then "Maybe<#{type_string(type.type, depth)}>"
  when Anchor::Types::Array then "Array<#{type_string(type.type, depth)}>"
  when Anchor::Types::Literal then serialize_literal(type.value)
  when Anchor::Types::Reference then type.name
  when Anchor::Types::Object, Anchor::Types::Object.singleton_class then serialize_object(type, depth)
  when Anchor::Types::Enum.singleton_class then type.anchor_schema_name
  when Anchor::Types::Unknown.singleton_class then "unknown"
  else raise RuntimeError
  end
end