Skip to content

Commit

Permalink
Better debugging output for bad api.yaml values (GoogleCloudPlatform#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisst authored and modular-magician committed May 9, 2019
1 parent 0fcf578 commit 06e68e9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions api/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def to_s
JSON.pretty_generate(self)
end

# Prints a dot notation path to where the field is nested within the parent
# object when called on a property. eg: parent.meta.label.foo
# Redefined on Product to terminate the calls up the parent chain.
def lineage
name
end

def to_json(opts = nil)
json_out = {}

Expand Down
7 changes: 7 additions & 0 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ def to_s
JSON.pretty_generate(self)
end

# Prints a dot notation path to where the field is nested within the parent
# object when called on a property. eg: parent.meta.label.foo
# Redefined on Resource to terminate the calls up the parent chain.
def lineage
name
end

def to_json(opts = nil)
# ignore fields that will contain references to parent resources
ignored_fields = %i[@__product @__parent @__resource @api_name @collection_url_response]
Expand Down
12 changes: 12 additions & 0 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ def to_s
JSON.pretty_generate(self)
end

# Prints a dot notation path to where the field is nested within the parent
# object. eg: parent.meta.label.foo
# The only intended purpose is to allow better error messages. Some objects
# and at some points in the build this doesn't ouput a valid output.
def lineage
return name if __parent.nil?

__parent.lineage + '.' + name
end

def to_json(opts = nil)
# ignore fields that will contain references to parent resources and
# those which will be added later
Expand Down Expand Up @@ -454,6 +464,8 @@ def all_properties
end

def properties
raise "Field '#{lineage}' properties are nil!" if @properties.nil?

@properties.reject(&:exclude)
end

Expand Down
7 changes: 4 additions & 3 deletions google/yaml_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ def check(variable, **opts)
value = instance_variable_get("@#{variable}")
end

# Check if value is required.
raise "Missing '#{variable}'" if value.nil? && opts[:required]
# Check if value is required. Print nested path if available.
lineage_path = respond_to?('lineage') ? lineage : ''
raise "#{lineage_path} > Missing '#{variable}'" if value.nil? && opts[:required]
return if value.nil?

# Check type
check_property_value(variable, value, opts[:type]) if opts[:type]

# Check item_type
if value.is_a?(Array)
raise "#{variable} must have item_type on arrays" unless opts[:item_type]
raise "#{lineage_path} > #{variable} must have item_type on arrays" unless opts[:item_type]

value.each_with_index do |o, index|
check_property_value("#{variable}[#{index}]", o, opts[:item_type])
Expand Down
10 changes: 9 additions & 1 deletion overrides/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,20 @@ def verify_properties(properties, overrides, res_name = '')

# Returns a property (or throws an error if it does not exist)
def find_property(properties, path, res_name = '')
# Add context for debugging property name typos
available = []
prefix = ''

prop = nil
path.each do |part|
available = properties.map { |p| prefix + p.name } unless properties.empty?
prefix = prefix + part + '.'

prop = properties.select { |o| o.name == part }.first
properties = prop&.nested_properties || []
end
raise "#{path.join('.')} does not exist on #{res_name}" unless prop
raise "#{path.join('.')} does not exist on #{res_name}.\n Possible values #{available}" \
unless prop

prop
end
Expand Down
4 changes: 2 additions & 2 deletions spec/override_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def self.attributes
it {
runner = Overrides::Validator.new(api, overrides)
expect { runner.run }.to raise_error(RuntimeError,
'@blahbad does not exist on AnotherResource')
/@blahbad does not exist on AnotherResource/)
}
end
describe 'should be able identify a bad property' do
Expand All @@ -54,7 +54,7 @@ def self.attributes
it {
runner = Overrides::Validator.new(api, overrides)
expect { runner.run }.to raise_error(RuntimeError,
'blahbad does not exist on AnotherResource')
/blahbad does not exist on AnotherResource/)
}
end

Expand Down

0 comments on commit 06e68e9

Please sign in to comment.