Class: Gapic::Model::Method::Routing::RoutingParameter

Inherits:
Object
  • Object
show all
Defined in:
lib/gapic/model/method/routing.rb

Overview

Full routing parameter information, including parsing order and matching/extraction patterns and regexes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routing_parameter, order) ⇒ RoutingParameter



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
# File 'lib/gapic/model/method/routing.rb', line 158

def initialize routing_parameter, order
  @order = order
  @field = routing_parameter.field
  @raw_template = routing_parameter.path_template
  @path_template = infer_template @raw_template, @field
  @path_pattern = Gapic::PathPattern.parse @path_template

  resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

  # Only one segment providing an argument and only one argument in the segment
  # (no `{foo}~{bar}` segments)
  valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
          resource_segment.arguments.count == 1

  unless valid
    error_text = create_invalid_error_text @path_pattern, @raw_template
    raise ModelError, error_text
  end

  @field_pattern = @path_pattern.simplified_pattern
  @field_full_regex_str = @path_pattern.to_field_regex_str
  @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

  @key = resource_segment.arguments[0]
  @value_pattern = resource_segment.resource_patterns[0]
end

Instance Attribute Details

#fieldString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#field_full_regex_strString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#field_patternString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#field_regex_strString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#keyString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#orderInteger (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#path_templateString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#raw_templateString? (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

#value_patternString (readonly)



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/gapic/model/method/routing.rb', line 141

class RoutingParameter
  attr_reader :order
  attr_reader :field
  attr_reader :raw_template
  attr_reader :path_template
  attr_reader :key
  attr_reader :value_pattern
  attr_reader :field_pattern
  attr_reader :field_regex_str
  attr_reader :field_full_regex_str

  ##
  # @param routing_parameter [::Google::Api::RoutingParameter]
  #   Routing parameter annotation
  #
  # @param order [Integer]
  #   Order of this annotation among its peers
  def initialize routing_parameter, order
    @order = order
    @field = routing_parameter.field
    @raw_template = routing_parameter.path_template
    @path_template = infer_template @raw_template, @field
    @path_pattern = Gapic::PathPattern.parse @path_template

    resource_segment = @path_pattern.segments.find(&:resource_id_segment?)

    # Only one segment providing an argument and only one argument in the segment
    # (no `{foo}~{bar}` segments)
    valid = @path_pattern.segments.count(&:resource_id_segment?) == 1 &&
            resource_segment.arguments.count == 1

    unless valid
      error_text = create_invalid_error_text @path_pattern, @raw_template
      raise ModelError, error_text
    end

    @field_pattern = @path_pattern.simplified_pattern
    @field_full_regex_str = @path_pattern.to_field_regex_str
    @field_regex_str = Gapic::PathPattern.parse(@field_pattern).to_field_regex_str

    @key = resource_segment.arguments[0]
    @value_pattern = resource_segment.resource_patterns[0]
  end

  ##
  # Whether pattern matching is not needed
  # since the patterns allow all strings
  # @return [Boolean]
  def pattern_matching_not_needed?
    field_pattern == "**" && value_pattern == "**"
  end

  ##
  # Whether the value to be added to the routing header
  # is the value of the whole field
  # @return [Boolean]
  def value_is_full_field?
    @path_pattern.segments.count == 1
  end

  private

  # Converts path template simplified forms into canonical
  # ResourceId representations by adding a field as a Resource Id
  # @param template [String]
  # @param field [String]
  # @return [String]
  def infer_template template, field
    if template.nil? || template.empty?
      return "{#{field}=**}"
    end

    if template.strip == "**"
      return "{#{field}=**}"
    end

    if template.strip == "*"
      return "{#{field}=*}"
    end

    template
  end

  def create_invalid_error_text path_pattern, raw_template
    reason = if path_pattern.segments.count(&:resource_id_segment?).zero?
               "it contains no ResourceId (e.g. `{foo=*}`) segments"
             elsif path_pattern.segments.count(&:resource_id_segment?) > 1
               "it contains more than one ResourceId (e.g. `{foo=*}`) segments "
             else
               "it contains a multivariate ResourceId segment (e.g. `{foo}~{bar}`)"
             end

    "A routing header parameter with the path_template #{raw_template}\n is invalid: #{reason}"
  end
end

Instance Method Details

#pattern_matching_not_needed?Boolean

Whether pattern matching is not needed since the patterns allow all strings



189
190
191
# File 'lib/gapic/model/method/routing.rb', line 189

def pattern_matching_not_needed?
  field_pattern == "**" && value_pattern == "**"
end

#value_is_full_field?Boolean

Whether the value to be added to the routing header is the value of the whole field



197
198
199
# File 'lib/gapic/model/method/routing.rb', line 197

def value_is_full_field?
  @path_pattern.segments.count == 1
end