-
Notifications
You must be signed in to change notification settings - Fork 580
(MODULES-1473) Deprecate type() function for new parser #382
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fixtures: | ||
symlinks: | ||
stdlib: "#{source_dir}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Returns the type when passed a value. | ||
# | ||
# @example how to compare values' types | ||
# # compare the types of two values | ||
# if type_of($first_value) != type_of($second_value) { fail("first_value and second_value are different types") } | ||
# @example how to compare against an abstract type | ||
# unless type_of($first_value) <= Numeric { fail("first_value must be Numeric") } | ||
# unless type_of{$first_value) <= Collection[1] { fail("first_value must be an Array or Hash, and contain at least one element") } | ||
# | ||
# See the documentation for "The Puppet Type System" for more information about types. | ||
# See the `assert_type()` function for flexible ways to assert the type of a value. | ||
# | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also add: Also see the assert_type function for flexible ways to assert the type of a value. |
||
Puppet::Functions.create_function(:type_of) do | ||
def type_of(value) | ||
Puppet::Pops::Types::TypeCalculator.infer_set(value) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# | ||
# type3x.rb | ||
# | ||
|
||
module Puppet::Parser::Functions | ||
newfunction(:type3x, :type => :rvalue, :doc => <<-EOS | ||
DEPRECATED: This function will be removed when puppet 3 support is dropped; please migrate to the new parser's typing system. | ||
|
||
Returns the type when passed a value. Type can be one of: | ||
|
||
* string | ||
* array | ||
* hash | ||
* float | ||
* integer | ||
* boolean | ||
EOS | ||
) do |args| | ||
raise(Puppet::ParseError, "type3x(): Wrong number of arguments " + | ||
"given (#{args.size} for 1)") if args.size < 1 | ||
|
||
value = args[0] | ||
|
||
klass = value.class | ||
|
||
if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) | ||
raise(Puppet::ParseError, 'type3x(): Unknown type') | ||
end | ||
|
||
klass = klass.to_s # Ugly ... | ||
|
||
# We note that Integer is the parent to Bignum and Fixnum ... | ||
result = case klass | ||
when /^(?:Big|Fix)num$/ then 'integer' | ||
when /^(?:True|False)Class$/ then 'boolean' | ||
else klass | ||
end | ||
|
||
if result == "String" then | ||
if value == value.to_i.to_s then | ||
result = "Integer" | ||
elsif value == value.to_f.to_s then | ||
result = "Float" | ||
end | ||
end | ||
|
||
return result.downcase | ||
end | ||
end | ||
|
||
# vim: set ts=2 sw=2 et : |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#! /usr/bin/env ruby -S rspec | ||
require 'spec_helper' | ||
|
||
describe "the type3x function" do | ||
let(:scope) { PuppetlabsSpec::PuppetInternals.scope } | ||
it "should exist" do | ||
expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x") | ||
end | ||
|
||
it "should raise a ParseError if there is less than 1 arguments" do | ||
expect { scope.function_type3x([]) }.to( raise_error(Puppet::ParseError)) | ||
end | ||
|
||
it "should return string when given a string" do | ||
result = scope.function_type3x(["aaabbbbcccc"]) | ||
expect(result).to(eq('string')) | ||
end | ||
|
||
it "should return array when given an array" do | ||
result = scope.function_type3x([["aaabbbbcccc","asdf"]]) | ||
expect(result).to(eq('array')) | ||
end | ||
|
||
it "should return hash when given a hash" do | ||
result = scope.function_type3x([{"a"=>1,"b"=>2}]) | ||
expect(result).to(eq('hash')) | ||
end | ||
|
||
it "should return integer when given an integer" do | ||
result = scope.function_type3x(["1"]) | ||
expect(result).to(eq('integer')) | ||
end | ||
|
||
it "should return float when given a float" do | ||
result = scope.function_type3x(["1.34"]) | ||
expect(result).to(eq('float')) | ||
end | ||
|
||
it "should return boolean when given a boolean" do | ||
result = scope.function_type3x([true]) | ||
expect(result).to(eq('boolean')) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#! /usr/bin/env ruby -S rspec | ||
|
||
require 'spec_helper' | ||
|
||
if ENV["FUTURE_PARSER"] == 'yes' or Puppet.version >= "4" | ||
require 'puppet/pops' | ||
require 'puppet/loaders' | ||
|
||
describe 'the type_of function' do | ||
before(:all) do | ||
loaders = Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, [File.join(fixtures, "modules")])) | ||
Puppet.push_context({:loaders => loaders}, "test-examples") | ||
end | ||
|
||
after(:all) do | ||
Puppet::Pops::Loaders.clear | ||
Puppet::pop_context() | ||
end | ||
|
||
let(:func) do | ||
# Load the function from the environment modulepath's modules (ie, fixtures) | ||
Puppet.lookup(:loaders).private_environment_loader.load(:function, 'type_of') | ||
end | ||
|
||
it 'gives the type of a string' do | ||
expect(func.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType) | ||
end | ||
|
||
it 'gives the type of an integer' do | ||
expect(func.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can add this as well:
@example how to compare against a type
unless type_of($first_value) <= Numeric { fail("first_value must be Numeric") }
unless type_of{$first_value) <= Collection[1] { fail("first_value must be an Array or Hash, and contain at least one element") }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.