Skip to content

Commit

Permalink
Add custom JSON generation
Browse files Browse the repository at this point in the history
to_json will print out a more human readable representation of the api
representation. It uses api property names as json key's in order to provide
more useful breadcrumbs and viewing when collapsed.
  • Loading branch information
chrisst committed Jan 16, 2019
1 parent 3b20c2e commit de9a1b0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'api/object'
require 'google/logger'
require 'compile/core'
require 'json'

module Api
# Repesents a product to be managed
Expand All @@ -33,6 +34,23 @@ def api_name
name.delete(' ').downcase
end

def to_json(opts = nil)
json_out = {}

instance_variables.each do |v|
if v == :@objects
json_out['@resources'] = objects.map { |o| [o.name, o] }.to_h
elsif instance_variable_get(v) == false || instance_variable_get(v).nil?
# ignore false or missing because omitting them cleans up result
# and both are the effective defaults of their types
else
json_out[v] = instance_variable_get(v)
end
end

JSON.generate(json_out, opts)
end

# The product full name is the "display name" in string form intended for
# users to read in documentation; "Google Compute Engine", "Cloud Bigtable"
def product_full_name
Expand Down Expand Up @@ -84,6 +102,12 @@ def validate
check :name, type: String, allowed: ORDER, required: true
end

def to_s
str = "#{name}: #{base_url}"
str += ' (default)' if default
str
end

def <=>(other)
ORDER.index(name) <=> ORDER.index(other.name) if other.is_a?(Version)
end
Expand Down
15 changes: 15 additions & 0 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ def validate
end
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]
json_out = {}

instance_variables.each do |v|
json_out[v] = instance_variable_get(v) unless ignored_fields.include? v
end

json_out[:@properties] = properties.map { |p| [p.name, p] }.to_h
json_out[:@parameters] = parameters.map { |p| [p.name, p] }.to_h

JSON.generate(json_out, opts)
end

def identity
props = all_user_properties
if @identity.nil?
Expand Down
23 changes: 23 additions & 0 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ def validate
check_conflicts
end

def to_json(opts = nil)
# ignore fields that will contain references to parent resources and
# those which will be added later
ignored_fields = %i[@resource @__parent @__resource @api_name]
json_out = {}

instance_variables.each do |v|
if v == :@conflicts && instance_variable_get(v).empty?
# ignore empty conflict arrays
elsif instance_variable_get(v) == false
# ignore false booleans as non-existence indicates falsey
elsif !ignored_fields.include? v
json_out[v] = instance_variable_get(v)
end
end

# convert properties to a hash based on name for nested readability
json_out[:@properties] = properties&.map { |p| [p.name, p] }.to_h \
if respond_to? 'properties'

JSON.generate(json_out, opts)
end

def check_default_value_property
return if @default_value.nil?

Expand Down

0 comments on commit de9a1b0

Please sign in to comment.