Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to override classes #456

Merged
merged 4 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

module Api
# Represents a property type
# rubocop:disable Metrics/ClassLength
class Type < Api::Object::Named
# The list of properties (attr_reader) that can be overridden in
# <provider>.yaml.
Expand All @@ -37,6 +38,9 @@ module Fields
# "ForceSendFields" concepts in the autogenerated API clients.
attr_reader :send_empty_value
attr_reader :min_version

# Can only be overriden - we should never set this ourselves.
attr_reader :new_type
end

include Fields
Expand Down Expand Up @@ -128,6 +132,22 @@ def exclude_if_not_in_version(version)
@exclude ||= version < min_version
end

# Overriding is_a? to enable class overrides.
# Ruby does not let you natively change types, so this is the next best
# thing.
def is_a?(clazz)
return Module.const_get(@new_type).new.is_a?(clazz) if @new_type
super(clazz)
end

# Overriding class to enable class overrides.
# Ruby does not let you natively change types, so this is the next best
# thing.
def class
return Module.const_get(@new_type) if @new_type
super
end

private

# A constant value to be provided as field
Expand Down Expand Up @@ -158,8 +178,8 @@ class Double < Primitive

# Represents a string
class String < Primitive
def initialize(name)
@name = name
def initialize(name = nil)
@name ||= name
end

PROJECT = Api::Type::String.new('project')
Expand Down Expand Up @@ -518,4 +538,5 @@ def property_ns_prefix
]
end
end
# rubocop:enable Metrics/ClassLength
end
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 2 additions & 0 deletions provider/property_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Provider
# Override a resource property (Api::Type) in api.yaml
# TODO(rosbo): Shared common logic with ResourceOverride via a base class.
class PropertyOverride < Api::Object
attr_reader :new_type

include Api::Type::Fields
# To allow overrides for type-specific fields, include those type's
# fields with an 'include' directive here.
Expand Down