Skip to content

Commit 7c8ae31

Browse files
committed
(MODULES-1473) Deprecate type() function for new parser
The `type()` function will cease to work on the new parser because 'type' is a reserved keyword. The `type3x()` function may be used to continue similar functionality, but will be deprecated in favor of the built-in typing system. The `type_of()` function has been included to introspect types in the new parser.
1 parent 4700f16 commit 7c8ae31

File tree

9 files changed

+163
-40
lines changed

9 files changed

+163
-40
lines changed

.fixtures.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fixtures:
2+
symlinks:
3+
stdlib: "#{source_dir}"

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
sudo: false
23
language: ruby
34
bundler_args: --without system_tests
45
script: "bundle exec rake validate && bundle exec rake lint && bundle exec rake spec SPEC_OPTS='--color --format documentation'"

README.markdown

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ manifests as a valid password attribute. *Type*: rvalue
464464
* `to_bytes`: Converts the argument into bytes, for example 4 kB becomes 4096.
465465
Takes a single string value as an argument. *Type*: rvalue
466466

467-
* `type`: Returns the type when passed a variable. Type can be a string, array, hash, float, integer, or boolean. *Type*: rvalue
467+
* `type3x`: Returns a string description of the type when passed a value. Type can be a string, array, hash, float, integer, or boolean. This function will be removed when puppet 3 support is dropped and the new type system may be used. *Type*: rvalue
468+
469+
* `type_of`: Returns the literal type when passed a value. Requires the new
470+
parser. Useful for comparison of types with `<=` such as in `if
471+
type_of($some_value) <= Array[String] { ... }` (which is equivalent to `if
472+
$some_value =~ Array[String] { ... }`) *Type*: rvalue
468473

469474
* `union`: This function returns a union of two arrays. For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"].
470475

lib/puppet/functions/type_of.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Returns the type when passed a value.
2+
#
3+
# @example how to compare values' types
4+
# # compare the types of two values
5+
# if type_of($first_value) != type_of($second_value) { fail("first_value and second_value are different types") }
6+
# @example how to compare against an abstract type
7+
# unless type_of($first_value) <= Numeric { fail("first_value must be Numeric") }
8+
# unless type_of{$first_value) <= Collection[1] { fail("first_value must be an Array or Hash, and contain at least one element") }
9+
#
10+
# See the documentation for "The Puppet Type System" for more information about types.
11+
# See the `assert_type()` function for flexible ways to assert the type of a value.
12+
#
13+
Puppet::Functions.create_function(:type_of) do
14+
def type_of(value)
15+
Puppet::Pops::Types::TypeCalculator.infer_set(value)
16+
end
17+
end

lib/puppet/parser/functions/type.rb

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,15 @@
44

55
module Puppet::Parser::Functions
66
newfunction(:type, :type => :rvalue, :doc => <<-EOS
7-
Returns the type when passed a variable. Type can be one of:
8-
9-
* string
10-
* array
11-
* hash
12-
* float
13-
* integer
14-
* boolean
7+
DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.
158
EOS
16-
) do |arguments|
17-
18-
raise(Puppet::ParseError, "type(): Wrong number of arguments " +
19-
"given (#{arguments.size} for 1)") if arguments.size < 1
20-
21-
value = arguments[0]
22-
23-
klass = value.class
24-
25-
if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
26-
raise(Puppet::ParseError, 'type(): Unknown type')
27-
end
28-
29-
klass = klass.to_s # Ugly ...
9+
) do |args|
3010

31-
# We note that Integer is the parent to Bignum and Fixnum ...
32-
result = case klass
33-
when /^(?:Big|Fix)num$/ then 'integer'
34-
when /^(?:True|False)Class$/ then 'boolean'
35-
else klass
11+
warning("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
12+
if ! Puppet::Parser::Functions.autoloader.loaded?(:type3x)
13+
Puppet::Parser::Functions.autoloader.load(:type3x)
3614
end
37-
38-
if result == "String" then
39-
if value == value.to_i.to_s then
40-
result = "Integer"
41-
elsif value == value.to_f.to_s then
42-
result = "Float"
43-
end
44-
end
45-
46-
return result.downcase
15+
function_type3x(args + [false])
4716
end
4817
end
4918

lib/puppet/parser/functions/type3x.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# type3x.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:type3x, :type => :rvalue, :doc => <<-EOS
7+
DEPRECATED: This function will be removed when puppet 3 support is dropped; please migrate to the new parser's typing system.
8+
9+
Returns the type when passed a value. Type can be one of:
10+
11+
* string
12+
* array
13+
* hash
14+
* float
15+
* integer
16+
* boolean
17+
EOS
18+
) do |args|
19+
raise(Puppet::ParseError, "type3x(): Wrong number of arguments " +
20+
"given (#{args.size} for 1)") if args.size < 1
21+
22+
value = args[0]
23+
24+
klass = value.class
25+
26+
if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
27+
raise(Puppet::ParseError, 'type3x(): Unknown type')
28+
end
29+
30+
klass = klass.to_s # Ugly ...
31+
32+
# We note that Integer is the parent to Bignum and Fixnum ...
33+
result = case klass
34+
when /^(?:Big|Fix)num$/ then 'integer'
35+
when /^(?:True|False)Class$/ then 'boolean'
36+
else klass
37+
end
38+
39+
if result == "String" then
40+
if value == value.to_i.to_s then
41+
result = "Integer"
42+
elsif value == value.to_f.to_s then
43+
result = "Float"
44+
end
45+
end
46+
47+
return result.downcase
48+
end
49+
end
50+
51+
# vim: set ts=2 sw=2 et :

spec/functions/type3x_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the type3x function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
it "should exist" do
7+
expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x")
8+
end
9+
10+
it "should raise a ParseError if there is less than 1 arguments" do
11+
expect { scope.function_type3x([]) }.to( raise_error(Puppet::ParseError))
12+
end
13+
14+
it "should return string when given a string" do
15+
result = scope.function_type3x(["aaabbbbcccc"])
16+
expect(result).to(eq('string'))
17+
end
18+
19+
it "should return array when given an array" do
20+
result = scope.function_type3x([["aaabbbbcccc","asdf"]])
21+
expect(result).to(eq('array'))
22+
end
23+
24+
it "should return hash when given a hash" do
25+
result = scope.function_type3x([{"a"=>1,"b"=>2}])
26+
expect(result).to(eq('hash'))
27+
end
28+
29+
it "should return integer when given an integer" do
30+
result = scope.function_type3x(["1"])
31+
expect(result).to(eq('integer'))
32+
end
33+
34+
it "should return float when given a float" do
35+
result = scope.function_type3x(["1.34"])
36+
expect(result).to(eq('float'))
37+
end
38+
39+
it "should return boolean when given a boolean" do
40+
result = scope.function_type3x([true])
41+
expect(result).to(eq('boolean'))
42+
end
43+
end

spec/functions/type_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
expect(Puppet::Parser::Functions.function("type")).to eq("function_type")
88
end
99

10-
it "should raise a ParseError if there is less than 1 arguments" do
11-
expect { scope.function_type([]) }.to( raise_error(Puppet::ParseError))
10+
it "should give a deprecation warning when called" do
11+
scope.expects(:warning).with("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
12+
scope.function_type(["aoeu"])
1213
end
1314

1415
it "should return string when given a string" do
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /usr/bin/env ruby -S rspec
2+
3+
require 'spec_helper'
4+
5+
if ENV["FUTURE_PARSER"] == 'yes' or Puppet.version >= "4"
6+
require 'puppet/pops'
7+
require 'puppet/loaders'
8+
9+
describe 'the type_of function' do
10+
before(:all) do
11+
loaders = Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, [File.join(fixtures, "modules")]))
12+
Puppet.push_context({:loaders => loaders}, "test-examples")
13+
end
14+
15+
after(:all) do
16+
Puppet::Pops::Loaders.clear
17+
Puppet::pop_context()
18+
end
19+
20+
let(:func) do
21+
# Load the function from the environment modulepath's modules (ie, fixtures)
22+
Puppet.lookup(:loaders).private_environment_loader.load(:function, 'type_of')
23+
end
24+
25+
it 'gives the type of a string' do
26+
expect(func.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType)
27+
end
28+
29+
it 'gives the type of an integer' do
30+
expect(func.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType)
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)