Skip to content

Commit

Permalink
Add custom JSON generation (#1252)
Browse files Browse the repository at this point in the history
Merged PR #1252.
  • Loading branch information
chrisst authored and modular-magician committed Feb 1, 2019
1 parent 5f3e646 commit 7dd4d8c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .ci/magic-modules/ensure_downstreams_merged.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_unmerged_prs(g, dependencies):
for pull in pulls:
# check whether the PR is merged - if it is, add it to the list.
pr = repo.get_pull(int(pull[1]))
if not pr.is_merged():
if not pr.is_merged() and not pr.state == "closed":
unmerged_dependencies.append(pull)
return unmerged_dependencies

Expand Down
29 changes: 29 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,28 @@ def api_name
name.downcase
end

def to_s
# relies on the custom to_json definitions
JSON.pretty_generate(self)
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 +107,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
19 changes: 19 additions & 0 deletions api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ def validate
end
end

def to_s
JSON.pretty_generate(self)
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
27 changes: 27 additions & 0 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ def validate
check_conflicts
end

def to_s
JSON.pretty_generate(self)
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 || instance_variable_get(v).nil?
# 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 7dd4d8c

Please sign in to comment.