Class: Yogurt::CodeGenerator::FieldAccessMethod

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
DefinedMethod, Utils, Memoize
Defined in:
lib/yogurt/code_generator/field_access_method.rb

Overview

Method that is used to access a field on an object

Defined Under Namespace

Classes: FragmentBranch

Constant Summary collapse

IMPOSSIBLE_FIELD_SIGNATURE =
T.let("NilClass", String)
IMPOSSIBLE_FIELD_BODY =
T.let(<<~STRING, String)
  # The combination of fragments used to retrieve this field make it impossible
  # for the field to have any value other than `nil`.
  nil
STRING

Instance Method Summary collapse

Methods included from Utils

#camelize, #generate_method_name, #generate_pretty_print, #indent, #possible_types_constant, #typename_method, #underscore

Methods included from DefinedMethod

#name

Methods included from Memoize

#freeze, #memoize_as

Instance Method Details

#bodyObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/yogurt/code_generator/field_access_method.rb', line 124

def body
  reduce!
  memoize_as(:body) do
    break IMPOSSIBLE_FIELD_BODY if field_access_is_impossible?

    if field_access_is_guaranteed? && branches.size == 1
      branches.fetch(0).expression
    elsif field_access_is_guaranteed?
      <<~STRING
        case (type = __typename)
        #{branches.map(&:to_ruby).join("\n")}
        else
          __unexpected_type(field: #{name.inspect}, observed_type: type, expected_types: POSSIBLE_TYPES)
        end
      STRING
    elsif branches.size == 1
      branch = branches.fetch(0)
      condition = branch.sorted_typenames.map {|type| "type == #{type.inspect}"}.join(' || ')
      <<~STRING
        type = __typename
        return unless #{condition}
        #{branch.expression}
      STRING
    else
      <<~STRING
        case (type = __typename)
        #{branches.map(&:to_ruby).join("\n")}
        end
      STRING
    end
  end
end

#branchesObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yogurt/code_generator/field_access_method.rb', line 87

def branches
  reduce!
  memoize_as(:branches) do
    # Construct the branches for each of the possible fragments. When grouped by
    # expression, the typenames possible for each branch should be disjoint. If they're
    # not, the query would have been rejected as invalid by the `FieldsWillMerge`
    # static validation rule.
    result = field_access_paths.group_by(&:expression).map do |expression, group|
      typenames = T.let(Set.new, T::Set[String])
      group.each do |path|
        typenames.merge(path.compatible_object_types)
      end

      FragmentBranch.new(expression: expression, typenames: typenames)
    end

    # Invariant: Make sure that the behavior of the world matches our expectations.
    invalid_branches = result.combination(2).select do |b1, b2|
      next if b1.nil?
      next if b2.nil?

      b1.typenames.intersect?(b2.typenames)
    end

    if invalid_branches.any?
      raise <<~STRING
        Some field access branches have overlapping types, but different field resolution expressions.
        #{invalid_branches.map {|b1, b2| { branch1: T.must(b1).serialize, branch2: T.must(b2).serialize }}.inspect}
      STRING
    end

    result.sort!
    result.freeze
  end
end

#field_access_is_guaranteed?Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
# File 'lib/yogurt/code_generator/field_access_method.rb', line 202

def field_access_is_guaranteed?
  reduce!
  memoize_as(:field_access_is_guaranteed?) do
    # Field access is not guaranteed if, after eliminating all of the unnecessary field
    # access paths, there are any that only return a value for a subset of the possible
    # field types at the root of the fragment.
    field_access_paths.all? do |path|
      root_possible_types.subset?(path.compatible_object_types)
    end
  end
end

#field_access_is_impossible?Boolean

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/yogurt/code_generator/field_access_method.rb', line 193

def field_access_is_impossible?
  reduce!
  field_access_paths.none?
end

#merge?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/yogurt/code_generator/field_access_method.rb', line 67

def merge?(other)
  return false unless other.is_a?(FieldAccessMethod)

  field_access_paths.concat(other.field_access_paths)
  true
end

#signatureObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/yogurt/code_generator/field_access_method.rb', line 158

def signature
  reduce!
  memoize_as(:signature) do
    break IMPOSSIBLE_FIELD_SIGNATURE if field_access_is_impossible?

    signatures = field_access_paths.map do |path|
      signature = path.signature
      next signature if !signature.start_with?("T.nilable")

      # Preserve the original signature if we're guaranteed to always return this field
      next signature if field_access_is_guaranteed?

      # If fragments might cause the field to be omitted, strip off the nilability
      # because we'll wrap the composite signature in a `T.nilable`
      signature.delete_prefix("T.nilable(").delete_suffix(")")
    end

    signatures.uniq!
    signatures.sort!

    composite_signature = if signatures.size == 1
      signatures.fetch(0)
    else
      "T.any(#{signatures.join(', ')})"
    end

    if field_access_is_guaranteed?
      composite_signature
    else
      "T.nilable(#{composite_signature})"
    end
  end
end

#to_rubyObject



75
76
77
78
79
80
81
82
# File 'lib/yogurt/code_generator/field_access_method.rb', line 75

def to_ruby
  <<~STRING
    sig {returns(#{signature})}
    def #{name}
      #{indent(body, 1).strip}
    end
  STRING
end