Skip to content

Commit

Permalink
(maint) Remove optional_object
Browse files Browse the repository at this point in the history
Object is now the top class and so there is no need to use
Optional[Object] everywhere. This removes the type factory method
optional_object and replaces all occurrances with just object. It also
updates all of the uses of Optional[Object] to use just Object.
  • Loading branch information
zaphod42 committed Jun 13, 2014
1 parent f19b010 commit 037e6d5
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/puppet/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def self.min_max_param(method)
end

# Construct a signature consisting of Object type, with min, and max, and given names.
# (there is only one type entry). Note that this signature is Object, not Optional[Object].
# (there is only one type entry).
#
# @api private
def self.object_signature(from, to, names)
Expand Down
12 changes: 6 additions & 6 deletions lib/puppet/functions/assert_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
Puppet::Functions.create_function(:assert_type) do
dispatch :assert_type do
param 'Type', 'type'
param 'Optional[Object]', 'value'
optional_block_param 'Callable[Optional[Object],Optional[Object]]', 'block'
param 'Object', 'value'
optional_block_param 'Callable[Object, Object]', 'block'
end

dispatch :assert_type_s do
param 'String', 'type_string'
param 'Optional[Object]', 'value'
optional_block_param 'Callable[Optional[Object], Optional[Object]]', 'block'
param 'Object', 'value'
optional_block_param 'Callable[Object, Object]', 'block'
end

# @param type [Type] the type the value must be an instance of
# @param value [Optional[Object]] the value to assert
# @param value [Object] the value to assert
#
def assert_type(type, value, block=nil)
unless Puppet::Pops::Types::TypeCalculator.instance?(type,value)
Expand All @@ -47,7 +47,7 @@ def assert_type(type, value, block=nil)
end

# @param type_string [String] the type the value must be an instance of given in String form
# @param value [Optional[Object]] the value to assert
# @param value [Object] the value to assert
#
def assert_type_s(type_string, value)
t = Puppet::Pops::Types::TypeParser.new.parse(type_string)
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/pops/evaluator/closure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def create_callable_type()
type = if param.type_expr
@evaluator.evaluate(param.type_expr, @enclosing_scope)
else
Puppet::Pops::Types::TypeFactory.optional_object()
Puppet::Pops::Types::TypeFactory.object()
end

if param.captures_rest && type.is_a?(Puppet::Pops::Types::PArrayType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def self.legacy_newfunction(name, options, &block)
# Define a dispatch that performs argument type/count checking
#
dispatch :__relay__call__ do
# Use Puppet Type Object (not Optional[Object] since the 3x API passes undef as empty string).
param 'Object', 'args'
# Specify arg count (transformed from 3x function arity specification).
arg_count(min_arg_count, max_arg_count)
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/pops/loader/static_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def create_logging_functions()

# Logs per the specified level, outputs formatted information for arrays, hashes etc.
# Overrides the implementation in Function that uses dispatching. This is not needed here
# since it accepts 0-n Optional[Object]
# since it accepts 0-n Object.
#
define_method(:call) do |scope, *vals|
# NOTE: 3x, does this: vals.join(" ")
Expand Down
7 changes: 1 addition & 6 deletions lib/puppet/pops/types/type_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ def self.optional(optional_type = nil)
t
end

# Convenience method to produce an Optional[Object] type
def self.optional_object()
optional(object())
end

# Produces the Enum type, optionally with specific string values
# @api public
#
Expand Down Expand Up @@ -203,7 +198,7 @@ def self.all_callables()
# A min < params.size means that the difference are optional.
# If max > params.size means that the last type repeats.
# if max is :default, the max value is unbound (infinity).
#
#
# Params are given as a sequence of arguments to {#type_of}.
#
def self.callable(*params)
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/parser/future_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ class { 'foo': x => Bar[joke] }
compile_to_catalog(<<-MANIFEST)
with(1) |$x, String $defaulted = 1| { notify { "${$x + $defaulted}": }}
MANIFEST
end.to raise_error(/expected.*Optional.*String.*actual.*Integer.*Integer/m)
end.to raise_error(/expected.*Object.*String.*actual.*Integer.*Integer/m)
end

it 'raises an error when a default argument value is an incorrect type and there are no arguments passed' do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/functions/assert_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
end.to raise_error(ArgumentError, Regexp.new(Regexp.escape(
"function 'assert_type' called with mis-matched arguments
expected one of:
assert_type(Type type, Optional[Object] value, Callable[Optional[Object], Optional[Object]] block {0,1}) - arg count {2,3}
assert_type(String type_string, Optional[Object] value, Callable[Optional[Object], Optional[Object]] block {0,1}) - arg count {2,3}
assert_type(Type type, Object value, Callable[Object, Object] block {0,1}) - arg count {2,3}
assert_type(String type_string, Object value, Callable[Object, Object] block {0,1}) - arg count {2,3}
actual:
assert_type(Integer, Integer) - arg count {2}")))
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pops/evaluator/evaluating_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ def test(count, block)
env_loader = Puppet.lookup(:loaders).public_environment_loader
fc = Puppet::Functions.create_function(:test) do
dispatch :test do
param 'Optional[Object]', 'lambda_arg'
param 'Object', 'lambda_arg'
required_block_param
end
def test(lambda_arg, block)
Expand Down
10 changes: 4 additions & 6 deletions spec/unit/pops/types/type_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def struct_t(type_hash)
Puppet::Pops::Types::TypeFactory.struct(type_hash)
end

def optional_object_t
Puppet::Pops::Types::TypeFactory.optional_object()
def object_t
Puppet::Pops::Types::TypeFactory.object()
end

def types
Expand Down Expand Up @@ -1148,10 +1148,8 @@ class Bar < Foo
the_block = factory.LAMBDA(params,factory.literal(42))
the_closure = Puppet::Pops::Evaluator::Closure.new(:fake_evaluator, the_block, :fake_scope)
expect(calculator.instance?(all_callables_t, the_closure)).to be_true
# TODO: lambdas are currently unttypes, anything can be given if arg count is correct
expect(calculator.instance?(callable_t(optional_object_t), the_closure)).to be_true
# Arg count is wrong
expect(calculator.instance?(callable_t(optional_object_t, optional_object_t), the_closure)).to be_false
expect(calculator.instance?(callable_t(object_t), the_closure)).to be_true
expect(calculator.instance?(callable_t(object_t, object_t), the_closure)).to be_false
end

it 'a Function instance should be considered a Callable' do
Expand Down

0 comments on commit 037e6d5

Please sign in to comment.