4
5
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 4
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::Intersection then type.types.map { |type| "(#{type_string(type, depth)})" }.join(" & ")
when Anchor::Types::Maybe then Anchor.config.maybe_as_union ? type_string(Anchor::Types::Union.new([type.type, Anchor::Types::Null]), depth) : "Maybe<#{type_string(type.type, depth)}>"
when Anchor::Types::Array then Anchor.config.array_bracket_notation ? "(#{type_string(type.type, depth)})[]" : "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::Identity then type_string(type.type)
when Anchor::Types::Unknown.singleton_class then "unknown"
else raise RuntimeError
end
end
|