Class: Protod::Proto::Field

Inherits:
Part
  • Object
show all
Defined in:
lib/protod/proto/field.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Part

#ancestor_as, #freeze, #has?, #ident=, #push, #root

Class Method Details

.build_from(const_or_name, **attributes) ⇒ Object

Raises:

  • (NotImplementedError)


14
15
16
17
18
19
20
# File 'lib/protod/proto/field.rb', line 14

def build_from(const_or_name, **attributes)
  i = Protod::Interpreter.find_by(const_or_name, with_register_from_ancestor: true)

  raise NotImplementedError, "Not found the interpreter for #{const_or_name}. You can define a interpreter using Protod::Interpreter.register_for" unless i

  new(interpreter: i, **attributes)
end

.build_from_rbs(type, on:, **attributes) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/protod/proto/field.rb', line 22

def build_from_rbs(type, on:, **attributes)
  case type
  when RBS::Types::Optional
    build_from_rbs(type.type, on: on, **attributes.merge(optional: attributes[:repeated] ? false : true))
  when RBS::Types::Union
    real_type = type.types.find { _1.name.kind == :class && Protod.rbs_environment.class_decls.key?(_1.name) }

    raise ArgumentError, "Not found declared class in union type on #{on}" unless real_type

    build_from_rbs(real_type, on: on, **attributes)
  when RBS::Types::Alias
    alias_decl = Protod.rbs_environment.type_alias_decls[type.name]

    raise ArgumentError, "Not found alias declaration of #{type.name.name} on #{on}" unless alias_decl

    build_from_rbs(alias_decl.decl.type, on: on, **attributes)
  when RBS::Types::ClassInstance
    case
    when should_repeated_with(type.name.to_s.safe_constantize)
      build_from_rbs(type.args.first, on: on, **attributes.merge(optional: false, repeated: true))
    when type.args.size > 0
      raise NotImplementedError, "Unsupported rbs type : Record or Tuple on #{on}"
    else
      build_from(type.name.to_s, **attributes)
    end
  when RBS::Types::Bases::Base
    build_from(type.class.name, **attributes)
  else
    binding.pry
    raise NotImplementedError, "Unsupported rbs type : #{type.class.name} on #{on}"
  end
end

.should_repeated_with(const) ⇒ Object



55
56
57
# File 'lib/protod/proto/field.rb', line 55

def should_repeated_with(const)
  const&.ancestors&.include?(::Array)
end

Instance Method Details

#to_protoObject

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/protod/proto/field.rb', line 64

def to_proto
  raise ArgumentError, "Not set interpreter" unless interpreter

  type_part = if interpreter.package && interpreter.package == ancestor_as(Protod::Proto::Package)
                interpreter.proto_full_ident.delete_prefix("#{interpreter.package.full_ident}.")
              else
                interpreter.proto_full_ident
              end

  [
    format_proto(''),
    [
      # optional ? 'optional' : nil,
      repeated ? 'repeated' : nil,
      type_part,
      ident,
      '=',
      index
    ].compact.join(' '),
    ';'
  ].join
end

#void?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/protod/proto/field.rb', line 60

def void?
  interpreter ? interpreter.proto_ident.blank? : false
end