puppet 4.4.2 → 4.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puppet might be problematic. Click here for more details.
- data/CONTRIBUTING.md +5 -5
- data/Gemfile +2 -2
- data/LICENSE +2 -2
- data/README.md +5 -0
- data/ext/project_data.yaml +2 -0
- data/lib/hiera_puppet.rb +6 -14
- data/lib/puppet/application/agent.rb +2 -3
- data/lib/puppet/data_providers/hiera_config.rb +2 -4
- data/lib/puppet/data_providers/hiera_interpolate.rb +12 -154
- data/lib/puppet/data_providers/json_data_provider_factory.rb +0 -7
- data/lib/puppet/data_providers/yaml_data_provider_factory.rb +2 -8
- data/lib/puppet/defaults.rb +70 -7
- data/lib/puppet/functions.rb +69 -0
- data/lib/puppet/functions/dig.rb +39 -0
- data/lib/puppet/functions/lest.rb +53 -0
- data/lib/puppet/functions/lookup.rb +40 -27
- data/lib/puppet/functions/new.rb +502 -0
- data/lib/puppet/functions/regsubst.rb +11 -10
- data/lib/puppet/functions/then.rb +74 -0
- data/lib/puppet/functions/type.rb +4 -4
- data/lib/puppet/functions/with.rb +1 -1
- data/lib/puppet/indirector/catalog/compiler.rb +2 -0
- data/lib/puppet/indirector/resource_type/parser.rb +5 -0
- data/lib/puppet/indirector/rest.rb +5 -1
- data/lib/puppet/loaders.rb +2 -0
- data/lib/puppet/metatype/manager.rb +19 -2
- data/lib/puppet/module_tool/applications/application.rb +1 -1
- data/lib/puppet/module_tool/skeleton/templates/generator/Gemfile +6 -2
- data/lib/puppet/module_tool/skeleton/templates/generator/Rakefile +19 -4
- data/lib/puppet/module_tool/skeleton/templates/generator/{tests → examples}/init.pp.erb +1 -1
- data/lib/puppet/module_tool/skeleton/templates/generator/spec/classes/init_spec.rb.erb +0 -1
- data/lib/puppet/network/http/api/master/v3/environment.rb +6 -2
- data/lib/puppet/parser/ast/pops_bridge.rb +20 -3
- data/lib/puppet/parser/compiler/catalog_validator/relationship_validator.rb +24 -2
- data/lib/puppet/parser/e4_parser_adapter.rb +13 -12
- data/lib/puppet/parser/environment_compiler.rb +2 -2
- data/lib/puppet/parser/resource.rb +14 -5
- data/lib/puppet/parser/scope.rb +18 -15
- data/lib/puppet/plugins/data_providers/data_provider.rb +19 -8
- data/lib/puppet/pops.rb +6 -0
- data/lib/puppet/pops/adapters.rb +5 -1
- data/lib/puppet/pops/evaluator/access_operator.rb +52 -14
- data/lib/puppet/pops/evaluator/compare_operator.rb +34 -4
- data/lib/puppet/pops/evaluator/evaluator_impl.rb +75 -22
- data/lib/puppet/pops/evaluator/literal_evaluator.rb +7 -6
- data/lib/puppet/pops/evaluator/runtime3_converter.rb +13 -1
- data/lib/puppet/pops/evaluator/runtime3_support.rb +14 -4
- data/lib/puppet/pops/functions/dispatcher.rb +1 -1
- data/lib/puppet/pops/issues.rb +18 -2
- data/lib/puppet/pops/loader/base_loader.rb +48 -7
- data/lib/puppet/pops/loader/dependency_loader.rb +27 -2
- data/lib/puppet/pops/loader/loader.rb +12 -0
- data/lib/puppet/pops/loader/predefined_loader.rb +29 -0
- data/lib/puppet/pops/loader/runtime3_type_loader.rb +57 -0
- data/lib/puppet/pops/loader/static_loader.rb +92 -5
- data/lib/puppet/pops/loader/type_definition_instantiator.rb +25 -3
- data/lib/puppet/pops/loaders.rb +84 -14
- data/lib/puppet/pops/lookup/explainer.rb +38 -1
- data/lib/puppet/pops/lookup/interpolation.rb +115 -0
- data/lib/puppet/pops/lookup/sub_lookup.rb +86 -0
- data/lib/puppet/pops/model/ast_transformer.rb +8 -1
- data/lib/puppet/pops/model/factory.rb +31 -8
- data/lib/puppet/pops/model/model.rb +8 -0
- data/lib/puppet/pops/model/model_label_provider.rb +1 -0
- data/lib/puppet/pops/model/model_meta.rb +7 -1
- data/lib/puppet/pops/model/model_tree_dumper.rb +4 -0
- data/lib/puppet/pops/parser/egrammar.ra +24 -7
- data/lib/puppet/pops/parser/eparser.rb +863 -798
- data/lib/puppet/pops/parser/evaluating_parser.rb +4 -0
- data/lib/puppet/pops/parser/locator.rb +8 -4
- data/lib/puppet/pops/pcore.rb +30 -0
- data/lib/puppet/pops/types/class_loader.rb +2 -4
- data/lib/puppet/pops/types/implementation_registry.rb +146 -0
- data/lib/puppet/pops/types/iterable.rb +4 -4
- data/lib/puppet/pops/types/p_object_type.rb +846 -0
- data/lib/puppet/pops/types/p_runtime_type.rb +102 -0
- data/lib/puppet/pops/types/p_sem_ver_range_type.rb +164 -0
- data/lib/puppet/pops/types/p_sem_ver_type.rb +113 -0
- data/lib/puppet/pops/types/puppet_object.rb +21 -0
- data/lib/puppet/pops/types/ruby_generator.rb +258 -0
- data/lib/puppet/pops/types/string_converter.rb +922 -0
- data/lib/puppet/pops/types/type_calculator.rb +29 -5
- data/lib/puppet/pops/types/type_conversion_error.rb +15 -0
- data/lib/puppet/pops/types/type_factory.rb +49 -16
- data/lib/puppet/pops/types/type_formatter.rb +335 -112
- data/lib/puppet/pops/types/type_mismatch_describer.rb +110 -29
- data/lib/puppet/pops/types/type_parser.rb +205 -197
- data/lib/puppet/pops/types/types.rb +481 -103
- data/lib/puppet/pops/validation.rb +1 -1
- data/lib/puppet/pops/validation/checker4_0.rb +66 -4
- data/lib/puppet/pops/validation/validator_factory_4_0.rb +1 -0
- data/lib/puppet/pops/visitor.rb +3 -1
- data/lib/puppet/property.rb +1 -1
- data/lib/puppet/provider/augeas/augeas.rb +1 -1
- data/lib/puppet/provider/package/pip.rb +64 -20
- data/lib/puppet/provider/package/rpm.rb +112 -0
- data/lib/puppet/provider/package/yum.rb +7 -68
- data/lib/puppet/provider/service/daemontools.rb +3 -3
- data/lib/puppet/provider/service/init.rb +4 -2
- data/lib/puppet/provider/service/runit.rb +3 -3
- data/lib/puppet/provider/service/smf.rb +6 -3
- data/lib/puppet/provider/service/systemd.rb +59 -73
- data/lib/puppet/reference/providers.rb +1 -2
- data/lib/puppet/resource.rb +54 -37
- data/lib/puppet/resource/catalog.rb +31 -29
- data/lib/puppet/resource/type_collection.rb +23 -8
- data/lib/puppet/settings.rb +4 -2
- data/lib/puppet/settings/base_setting.rb +9 -3
- data/lib/puppet/settings/symbolic_enum_setting.rb +17 -0
- data/lib/puppet/test/test_helper.rb +0 -1
- data/lib/puppet/type.rb +9 -3
- data/lib/puppet/type/exec.rb +17 -17
- data/lib/puppet/type/file.rb +12 -0
- data/lib/puppet/type/file/content.rb +6 -6
- data/lib/puppet/type/file/ensure.rb +4 -4
- data/lib/puppet/type/file/source.rb +4 -4
- data/lib/puppet/type/file/target.rb +2 -2
- data/lib/puppet/type/mount.rb +18 -1
- data/lib/puppet/type/package.rb +3 -3
- data/lib/puppet/type/schedule.rb +4 -4
- data/lib/puppet/type/service.rb +15 -0
- data/lib/puppet/type/sshkey.rb +5 -3
- data/lib/puppet/type/tidy.rb +3 -3
- data/lib/puppet/type/zone.rb +5 -5
- data/lib/puppet/util/feature.rb +1 -1
- data/lib/puppet/util/monkey_patches.rb +8 -0
- data/lib/puppet/util/network_device/cisco/device.rb +16 -6
- data/lib/puppet/util/network_device/cisco/interface.rb +5 -6
- data/lib/puppet/util/plist.rb +3 -3
- data/lib/puppet/version.rb +1 -1
- data/spec/fixtures/unit/application/environments/production/data/common.yaml +13 -0
- data/spec/fixtures/unit/data_providers/environments/production/modules/abc/lib/puppet/functions/abc/data.rb +2 -1
- data/spec/fixtures/unit/data_providers/environments/production/modules/abc/manifests/init.pp +2 -1
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_json/data/empty_key.json +1 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_json/hiera.yaml +5 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_json/manifests/init.pp +2 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_json/metadata.json +9 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_yaml/data/empty_key.yaml +1 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_yaml/hiera.yaml +5 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_yaml/manifests/init.pp +2 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_key_yaml/metadata.json +9 -0
- data/spec/fixtures/unit/functions/lookup/environments/production/modules/empty_yaml/data/empty.yaml +1 -0
- data/spec/fixtures/unit/pops/loaders/loaders/dependent_modules_with_metadata/modules/usee/lib/puppet/type/usee_type.rb +5 -0
- data/spec/fixtures/unit/pops/loaders/loaders/dependent_modules_with_metadata/modules/user/manifests/init.pp +6 -0
- data/spec/fixtures/unit/provider/service/smf/svcs.out +4 -3
- data/spec/integration/module_tool/tar/mini_spec.rb +27 -27
- data/spec/integration/parser/catalog_spec.rb +14 -2
- data/spec/integration/parser/compiler_spec.rb +94 -3
- data/spec/integration/parser/resource_expressions_spec.rb +1 -1
- data/spec/integration/resource/type_collection_spec.rb +8 -0
- data/spec/lib/puppet_spec/compiler.rb +11 -4
- data/spec/shared_contexts/types_setup.rb +4 -0
- data/spec/unit/application/lookup_spec.rb +91 -9
- data/spec/unit/appmgmt_spec.rb +44 -35
- data/spec/unit/capability_spec.rb +33 -53
- data/spec/unit/data_providers/function_data_provider_spec.rb +19 -1
- data/spec/unit/data_providers/hiera_data_provider_spec.rb +1 -1
- data/spec/unit/defaults_spec.rb +18 -0
- data/spec/unit/functions/assert_type_spec.rb +1 -1
- data/spec/unit/functions/dig_spec.rb +58 -0
- data/spec/unit/functions/lest_spec.rb +34 -0
- data/spec/unit/functions/lookup_spec.rb +108 -2
- data/spec/unit/functions/new_spec.rb +543 -0
- data/spec/unit/functions/regsubst_spec.rb +8 -0
- data/spec/unit/functions/then_spec.rb +40 -0
- data/spec/unit/functions4_spec.rb +78 -10
- data/spec/unit/hiera_puppet_spec.rb +49 -8
- data/spec/unit/indirector/resource_type/parser_spec.rb +5 -0
- data/spec/unit/indirector/rest_spec.rb +12 -0
- data/spec/unit/network/http/api/master/v3/environment_spec.rb +60 -0
- data/spec/unit/node/environment_spec.rb +10 -0
- data/spec/unit/parser/compiler_spec.rb +20 -1
- data/spec/unit/parser/functions/create_resources_spec.rb +2 -2
- data/spec/unit/parser/functions/shared.rb +1 -1
- data/spec/unit/parser/resource_spec.rb +8 -1
- data/spec/unit/parser/scope_spec.rb +45 -0
- data/spec/unit/pops/evaluator/access_ops_spec.rb +14 -0
- data/spec/unit/pops/evaluator/evaluating_parser_spec.rb +13 -5
- data/spec/unit/pops/loaders/static_loader_spec.rb +92 -1
- data/spec/unit/{data_providers/hiera_interpolation_spec.rb → pops/lookup/interpolation_spec.rb} +7 -5
- data/spec/unit/pops/parser/lexer2_spec.rb +2 -9
- data/spec/unit/pops/parser/parse_application_spec.rb +3 -8
- data/spec/unit/pops/parser/parse_basic_expressions_spec.rb +19 -0
- data/spec/unit/pops/parser/parse_capabilities_spec.rb +3 -10
- data/spec/unit/pops/parser/parse_site_spec.rb +19 -10
- data/spec/unit/pops/parser/parser_rspec_helper.rb +0 -4
- data/spec/unit/pops/types/enumeration_spec.rb +13 -12
- data/spec/unit/pops/types/iterable_spec.rb +2 -2
- data/spec/unit/pops/types/p_object_type_spec.rb +1060 -0
- data/spec/unit/pops/types/p_sem_ver_type_spec.rb +285 -0
- data/spec/unit/pops/types/recursion_guard_spec.rb +19 -17
- data/spec/unit/pops/types/ruby_generator_spec.rb +261 -0
- data/spec/unit/pops/types/string_converter_spec.rb +904 -0
- data/spec/unit/pops/types/type_calculator_spec.rb +430 -406
- data/spec/unit/pops/types/type_factory_spec.rb +119 -104
- data/spec/unit/pops/types/type_formatter_spec.rb +73 -6
- data/spec/unit/pops/types/type_mismatch_describer_spec.rb +2 -2
- data/spec/unit/pops/types/type_parser_spec.rb +54 -15
- data/spec/unit/pops/types/types_spec.rb +113 -8
- data/spec/unit/pops/validator/validator_spec.rb +84 -10
- data/spec/unit/provider/package/pip3_spec.rb +9 -270
- data/spec/unit/provider/package/pip_spec.rb +85 -30
- data/spec/unit/provider/package/rpm_spec.rb +160 -3
- data/spec/unit/provider/package/yum_spec.rb +23 -134
- data/spec/unit/provider/service/smf_spec.rb +14 -2
- data/spec/unit/provider/service/systemd_spec.rb +33 -41
- data/spec/unit/resource/capability_finder_spec.rb +10 -2
- data/spec/unit/settings/file_setting_spec.rb +6 -0
- data/spec/unit/transaction/additional_resource_generator_spec.rb +80 -65
- data/spec/unit/type/mount_spec.rb +51 -10
- data/spec/unit/type/service_spec.rb +16 -0
- data/spec/unit/type_spec.rb +14 -0
- data/spec/unit/util/feature_spec.rb +1 -1
- data/spec/unit/util/monkey_patches_spec.rb +60 -0
- data/spec/unit/util/network_device/cisco/device_spec.rb +1 -1
- metadata +63 -11
- data/lib/puppet/pops/types/types_meta.rb +0 -0
- data/spec/integration/provider/package_spec.rb +0 -35
data/lib/puppet/functions.rb
CHANGED
@@ -290,6 +290,45 @@ module Puppet::Functions
|
|
290
290
|
dispatch(meth_name, &block)
|
291
291
|
end
|
292
292
|
end
|
293
|
+
|
294
|
+
# Allows types local to the function to be defined to ease the use of complex types
|
295
|
+
# in a 4.x function. Within the given block, calls to `type` can be made with a string
|
296
|
+
# 'AliasType = ExistingType` can be made to define aliases. The defined aliases are
|
297
|
+
# available for further aliases, and in all dispatchers.
|
298
|
+
#
|
299
|
+
# @since 4.5.0
|
300
|
+
# @api public
|
301
|
+
#
|
302
|
+
def self.local_types(&block)
|
303
|
+
if loader.nil?
|
304
|
+
raise ArgumentError, "No loader present. Call create_loaded_function(:myname, loader,...), instead of 'create_function' if running tests"
|
305
|
+
end
|
306
|
+
aliases = LocalTypeAliasesBuilder.new(loader, name)
|
307
|
+
aliases.instance_eval(&block)
|
308
|
+
# Add the loaded types to the builder
|
309
|
+
aliases.local_types.each do |type_alias_expr|
|
310
|
+
# Bind the type alias to the local_loader using the alias
|
311
|
+
t = Puppet::Pops::Loader::TypeDefinitionInstantiator.create_from_model(type_alias_expr, aliases.loader)
|
312
|
+
|
313
|
+
# Also define a method for convenient access to the defined type alias.
|
314
|
+
# Since initial capital letter in Ruby means a Constant these names use a prefix of
|
315
|
+
# `type`. As an example, the type 'MyType' is accessed by calling `type_MyType`.
|
316
|
+
define_method("type_#{t.name}") { t }
|
317
|
+
end
|
318
|
+
# Store the loader in the class
|
319
|
+
@loader = aliases.loader
|
320
|
+
end
|
321
|
+
|
322
|
+
# Creates a new function instance in the given closure scope (visibility to variables), and a loader
|
323
|
+
# (visibility to other definitions). The created function will either use the `given_loader` or
|
324
|
+
# (if it has local type aliases) a loader that was constructed from the loader used when loading
|
325
|
+
# the function's class.
|
326
|
+
#
|
327
|
+
# TODO: It would be of value to get rid of the second parameter here, but that would break API.
|
328
|
+
#
|
329
|
+
def self.new(closure_scope, given_loader)
|
330
|
+
super(closure_scope, @loader || given_loader)
|
331
|
+
end
|
293
332
|
end
|
294
333
|
|
295
334
|
# Base class for all functions implemented in the puppet language
|
@@ -497,6 +536,36 @@ module Puppet::Functions
|
|
497
536
|
end
|
498
537
|
end
|
499
538
|
|
539
|
+
# The LocalTypeAliasBuilder is used by the 'local_types' method to collect the individual
|
540
|
+
# type aliases given by the function's author.
|
541
|
+
#
|
542
|
+
class LocalTypeAliasesBuilder
|
543
|
+
attr_reader :local_types, :parser, :loader
|
544
|
+
|
545
|
+
def initialize(loader, name)
|
546
|
+
@loader = Puppet::Pops::Loader::PredefinedLoader.new(loader, :"local_function_#{name}")
|
547
|
+
@local_types = []
|
548
|
+
# get the shared parser used by puppet's compiler
|
549
|
+
@parser = Puppet::Pops::Parser::EvaluatingParser.singleton()
|
550
|
+
end
|
551
|
+
|
552
|
+
# Defines a local type alias, the given string should be a Puppet Language type alias expression
|
553
|
+
# in string form without the leading 'type' keyword.
|
554
|
+
# Calls to local_type must be made before the first parameter definition or an error will
|
555
|
+
# be raised.
|
556
|
+
#
|
557
|
+
# @param assignment_string [String] a string on the form 'AliasType = ExistingType'
|
558
|
+
# @api public
|
559
|
+
#
|
560
|
+
def type(assignment_string)
|
561
|
+
result = parser.parse_string("type #{assignment_string}", nil) # no file source :-(
|
562
|
+
unless result.body.kind_of?(Puppet::Pops::Model::TypeAlias)
|
563
|
+
raise ArgumentError, "Expected a type alias assignment on the form 'AliasType = T', got '#{assignment_string}'"
|
564
|
+
end
|
565
|
+
@local_types << result.body
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
500
569
|
private
|
501
570
|
|
502
571
|
# @note WARNING: This style of creating functions is not public. It is a system
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Returns a value for a sequence of given keys/indexes into a structure.
|
2
|
+
# This function is used to "dig into" a complex data structure by
|
3
|
+
# using a sequence of keys / indexes to access a value from which
|
4
|
+
# the next key/index is accessed recursively.
|
5
|
+
#
|
6
|
+
# The first encountered `undef` value or key stops the "dig" and `undef` is returned.
|
7
|
+
#
|
8
|
+
# An error is raised if an attempt is made to "dig" into
|
9
|
+
# something other than an `undef` (which immediately returns `undef`), an Array or a Hash.
|
10
|
+
#
|
11
|
+
# @example Using `dig`
|
12
|
+
#
|
13
|
+
# ~~~ puppet
|
14
|
+
# $data = {a => { b => [{x => 10, y => 20}, {x => 100, y => 200}]}}
|
15
|
+
# notice $data.dig(a, b, 1, x)
|
16
|
+
# ~~~
|
17
|
+
#
|
18
|
+
# Would notice the value 100.
|
19
|
+
#
|
20
|
+
# @since 4.5.0
|
21
|
+
#
|
22
|
+
Puppet::Functions.create_function(:dig) do
|
23
|
+
dispatch :dig do
|
24
|
+
param 'Optional[Collection]', :data
|
25
|
+
repeated_param 'Any', :arg
|
26
|
+
end
|
27
|
+
|
28
|
+
def dig(data, *args)
|
29
|
+
walked_path = []
|
30
|
+
args.reduce(data) do | d, k |
|
31
|
+
return nil if d.nil? || k.nil?
|
32
|
+
if !(d.is_a?(Array) || d.is_a?(Hash))
|
33
|
+
raise ArgumentError, "The given data does not contain a Collection at #{walked_path}, got '#{d.class}'"
|
34
|
+
end
|
35
|
+
walked_path << k
|
36
|
+
d[k]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Call a [lambda](https://docs.puppet.com/puppet/latest/reference/lang_lambdas.html)
|
2
|
+
# (which should accept no arguments) if the argument given to the function is `undef`.
|
3
|
+
# Returns the result of calling the lambda if the argument is `undef`, otherwwise the
|
4
|
+
# given argument.
|
5
|
+
#
|
6
|
+
# The `lest` function is useful in a chain of `then` calls, or in general
|
7
|
+
# as a guard against `undef` values. The function can be used to call `fail`, or to
|
8
|
+
# return a default value.
|
9
|
+
#
|
10
|
+
# These two expressions are equivalent:
|
11
|
+
# ~~~puppet
|
12
|
+
# if $x == undef { do_things() }
|
13
|
+
# lest($x) || { do_things() }
|
14
|
+
# ~~
|
15
|
+
#
|
16
|
+
# @example Using `lest`
|
17
|
+
#
|
18
|
+
# ~~~ puppet
|
19
|
+
# $data = {a => [ b, c ] }
|
20
|
+
# notice $data.dig(a, b, c)
|
21
|
+
# .then |$x| { $x * 2 }
|
22
|
+
# .lest || { fail("no value for $data[a][b][c]" }
|
23
|
+
# ~~~
|
24
|
+
#
|
25
|
+
# Would fail the operation because $data[a][b][c] results in `undef`
|
26
|
+
#
|
27
|
+
# In contrast - this example:
|
28
|
+
#
|
29
|
+
# ~~~ puppet
|
30
|
+
# $data = {a => { b => { c => 10 } } }
|
31
|
+
# notice $data.dig(a, b, c)
|
32
|
+
# .then |$x| { $x * 2 }
|
33
|
+
# .lest || { fail("no value for $data[a][b][c]" }
|
34
|
+
# ~~~
|
35
|
+
#
|
36
|
+
# Would notice the value 20
|
37
|
+
#
|
38
|
+
# @since 4.5.0
|
39
|
+
#
|
40
|
+
Puppet::Functions.create_function(:lest) do
|
41
|
+
dispatch :lest do
|
42
|
+
param 'Any', :arg
|
43
|
+
block_param 'Callable[0,0]', :block
|
44
|
+
end
|
45
|
+
|
46
|
+
def lest(arg)
|
47
|
+
if arg.nil?
|
48
|
+
yield()
|
49
|
+
else
|
50
|
+
arg
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -122,54 +122,67 @@
|
|
122
122
|
#
|
123
123
|
# @since 4.0.0
|
124
124
|
Puppet::Functions.create_function(:lookup, Puppet::Functions::InternalFunction) do
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
125
|
+
|
126
|
+
local_types do
|
127
|
+
type 'NameType = Variant[String, Array[String]]'
|
128
|
+
type 'ValueType = Type'
|
129
|
+
type 'DefaultValueType = Any'
|
130
|
+
type 'MergeType = Variant[String[1], Hash[String, Scalar]]'
|
131
|
+
type 'BlockType = Callable[NameType]'
|
132
|
+
type "OptionsWithName = Struct[{\
|
133
|
+
name => NameType,\
|
134
|
+
value_type => Optional[ValueType],\
|
135
|
+
default_value => Optional[DefaultValueType],\
|
136
|
+
override => Optional[Hash[String,Any]],\
|
137
|
+
default_values_hash => Optional[Hash[String,Any]],\
|
138
|
+
merge => Optional[MergeType]\
|
139
|
+
}]"
|
140
|
+
type "OptionsWithoutName = Struct[{\
|
141
|
+
value_type => Optional[ValueType],\
|
142
|
+
default_value => Optional[DefaultValueType],\
|
143
|
+
override => Optional[Hash[String,Any]],\
|
144
|
+
default_values_hash => Optional[Hash[String,Any]],\
|
145
|
+
merge => Optional[MergeType]\
|
146
|
+
}]"
|
147
|
+
|
148
|
+
end
|
136
149
|
|
137
150
|
dispatch :lookup_1 do
|
138
151
|
scope_param
|
139
|
-
param
|
140
|
-
optional_param
|
141
|
-
optional_param
|
152
|
+
param 'NameType', :name
|
153
|
+
optional_param 'ValueType', :value_type
|
154
|
+
optional_param 'MergeType', :merge
|
142
155
|
end
|
143
156
|
|
144
157
|
dispatch :lookup_2 do
|
145
158
|
scope_param
|
146
|
-
param
|
147
|
-
param
|
148
|
-
param
|
149
|
-
param
|
159
|
+
param 'NameType', :name
|
160
|
+
param 'Optional[ValueType]', :value_type
|
161
|
+
param 'Optional[MergeType]', :merge
|
162
|
+
param 'DefaultValueType', :default_value
|
150
163
|
end
|
151
164
|
|
152
165
|
dispatch :lookup_3 do
|
153
166
|
scope_param
|
154
|
-
param
|
155
|
-
optional_param
|
156
|
-
optional_param
|
157
|
-
required_block_param
|
167
|
+
param 'NameType', :name
|
168
|
+
optional_param 'ValueType', :value_type
|
169
|
+
optional_param 'MergeType', :merge
|
170
|
+
required_block_param 'BlockType', :block
|
158
171
|
end
|
159
172
|
|
160
173
|
# Lookup without name. Name then becomes a required entry in the options hash
|
161
174
|
dispatch :lookup_4 do
|
162
175
|
scope_param
|
163
|
-
param
|
164
|
-
optional_block_param
|
176
|
+
param 'OptionsWithName', :options_hash
|
177
|
+
optional_block_param 'BlockType', :block
|
165
178
|
end
|
166
179
|
|
167
180
|
# Lookup using name and options hash.
|
168
181
|
dispatch :lookup_5 do
|
169
182
|
scope_param
|
170
|
-
param
|
171
|
-
param
|
172
|
-
optional_block_param
|
183
|
+
param 'Variant[String,Array[String]]', :name
|
184
|
+
param 'OptionsWithoutName', :options_hash
|
185
|
+
optional_block_param 'BlockType', :block
|
173
186
|
end
|
174
187
|
|
175
188
|
def lookup_1(scope, name, value_type=nil, merge=nil)
|
@@ -0,0 +1,502 @@
|
|
1
|
+
# Creates a new instance/object of a given type.
|
2
|
+
#
|
3
|
+
# This functions makes it possible to create new instances of
|
4
|
+
# concrete types. If a block is given it is called with the
|
5
|
+
# just created instance as an argument.
|
6
|
+
#
|
7
|
+
# Calling this function is equivalent to directly
|
8
|
+
# calling the type:
|
9
|
+
#
|
10
|
+
# @ example new and calling type directly are equivalent
|
11
|
+
#
|
12
|
+
# ~~~ puppet
|
13
|
+
# $a = Integer.new("42")
|
14
|
+
# $b = Integer("42")
|
15
|
+
# ~~~
|
16
|
+
#
|
17
|
+
# This would convert the string "42" to the decimal value 42.
|
18
|
+
#
|
19
|
+
# @ example arguments by position or by name
|
20
|
+
#
|
21
|
+
# ~~~ puppet
|
22
|
+
# $a = Integer.new("42", 8)
|
23
|
+
# $b = Integer({from => "42", radix => 8})
|
24
|
+
# ~~~
|
25
|
+
#
|
26
|
+
# This would convert the octal (radix 8) number "42" in string form
|
27
|
+
# to the decimal value 34.
|
28
|
+
#
|
29
|
+
# The new function supports two ways of giving the arguments:
|
30
|
+
#
|
31
|
+
# * by name (using a hash with property to value mapping)
|
32
|
+
# * by position (as regular arguments)
|
33
|
+
#
|
34
|
+
# Note that it is not possible to create new instances of
|
35
|
+
# some abstract classes (e.g. `Variant`). The type Optional[T] is an
|
36
|
+
# exception as it will create an instance of T or `undef` if the
|
37
|
+
# value to convert is `undef`.
|
38
|
+
#
|
39
|
+
# The arguments that can be given to each type is
|
40
|
+
# determined by the type.
|
41
|
+
#
|
42
|
+
# An assertion is always made that
|
43
|
+
# the produced value complies with the given type constraints.
|
44
|
+
#
|
45
|
+
# @example type constraints are checked:
|
46
|
+
#
|
47
|
+
# ~~~ puppet
|
48
|
+
# Integer[0].new("-100")
|
49
|
+
# ~~~
|
50
|
+
#
|
51
|
+
# Would fail with an assertion error.
|
52
|
+
#
|
53
|
+
# The following sections show the arguments and conversion rules
|
54
|
+
# per type built into the Puppet Type System.
|
55
|
+
#
|
56
|
+
# Conversion to Optional[T] and NotUndef[T]
|
57
|
+
# -----------------------------------------
|
58
|
+
# Conversion to these types is the same as a conversion to the type argument `T`. In the case of `Optional[T]`
|
59
|
+
# it accepted that the result may be `undef` (i.e. not possible to convert).
|
60
|
+
#
|
61
|
+
# Conversion to Integer
|
62
|
+
# ---------------------
|
63
|
+
#
|
64
|
+
# A new `Integer` can be created from `Integer`, `Float`, `Boolean`, and `String` values.
|
65
|
+
# For conversion `from` String it is possible to specify the radix.
|
66
|
+
#
|
67
|
+
# ~~~ puppet
|
68
|
+
# type Radix = Variant[Default, Integer[2,2], Integer[8,8], Integer[10,10], Integer[16,16]]
|
69
|
+
# Callable[String, Radix, 1, 2]
|
70
|
+
# Callable[Variant[Numeric, Boolean]]
|
71
|
+
# ~~~
|
72
|
+
#
|
73
|
+
# * When converting from String the default radix is 10
|
74
|
+
# * If radix is not specified an attempt is made to detect the radix from the start of the string:
|
75
|
+
# * `0b` or `0B` is taken as radix 2,
|
76
|
+
# * `0x` or `0X` is taken as radix 16,
|
77
|
+
# * `0` as radix 8,
|
78
|
+
# * all other are decimal
|
79
|
+
# * Conversion from String accepts an optional sign in the string.
|
80
|
+
# * For hexadecimal (radix 16) conversion an optional leading "0x", or "0X" is accepted
|
81
|
+
# * for octal (radix 8) an optional leading "0" is accepted
|
82
|
+
# * for binary (radix 2) an optional leading "0b" or "0B" is accepted
|
83
|
+
# * When `radix` is set to `default`, the conversion is based on the leading
|
84
|
+
# characters in the string. A leading "0" for radix 8, a leading "0x", or "0X" for
|
85
|
+
# radix 16, and leading "0b" or "0B" for binary.
|
86
|
+
# * Conversion from Boolean results in 0 for `false` and 1 for `true`.
|
87
|
+
# * Conversion from Integer, Float, and Boolean ignores the radix.
|
88
|
+
# * Float value fractions are truncated (no rounding)
|
89
|
+
#
|
90
|
+
# @example Converting to Integer
|
91
|
+
#
|
92
|
+
# ~~~ puppet
|
93
|
+
# $a_number = Integer("0xFF", 16) # results in 255
|
94
|
+
# $a_number = Numeric("010") # results in 8
|
95
|
+
# $a_number = Numeric("010", 10) # results in 10
|
96
|
+
# $a_number = Integer("true") # results in 1
|
97
|
+
# ~~~
|
98
|
+
#
|
99
|
+
# Conversion to Float
|
100
|
+
# -------------------
|
101
|
+
#
|
102
|
+
# A new `Float` can be created from `Integer`, `Float`, `Boolean`, and `String` values.
|
103
|
+
# For conversion `from` String both float and integer formats are supported.
|
104
|
+
#
|
105
|
+
# * For an integer, the floating point fraction of .0 is added to the value.
|
106
|
+
# * A Boolean true is converted to 1.0, and a false to 0.0
|
107
|
+
# * In String format, integer prefixes for hex and binary are understood (but not octal since
|
108
|
+
# floating point in string format may start with a '0').
|
109
|
+
#
|
110
|
+
# Conversion to Numeric
|
111
|
+
# ---------------------
|
112
|
+
#
|
113
|
+
# A new Integer or Float can be created from Integer, Float, Boolean and
|
114
|
+
# String values.
|
115
|
+
#
|
116
|
+
# ~~~ puppet
|
117
|
+
# Callable[Variant[Numeric, Boolean, String]]
|
118
|
+
# ~~~
|
119
|
+
#
|
120
|
+
# * If the value has a decimal period, or if given in scientific notation
|
121
|
+
# (e/E), the result is a `Float`, otherwise the value is an `Integer`. The
|
122
|
+
# conversion from `String` always uses a radix based on the prefix of the string.
|
123
|
+
# * Conversion from Boolean results in 0 for `false` and 1 for `true`.
|
124
|
+
#
|
125
|
+
# @example Converting to Numeric
|
126
|
+
#
|
127
|
+
# ~~~ puppet
|
128
|
+
# $a_number = Numeric("true") # results in 1
|
129
|
+
# $a_number = Numeric("0xFF") # results in 255
|
130
|
+
# $a_number = Numeric("010") # results in 8
|
131
|
+
# $a_number = Numeric("3.14") # results in 3.14 (a float)
|
132
|
+
# ~~~
|
133
|
+
#
|
134
|
+
# Conversion to String
|
135
|
+
# --------------------
|
136
|
+
#
|
137
|
+
# Conversion to String is the most comprehensive conversion as there are many
|
138
|
+
# use cases where a String representation is wanted. The defaults for the many options
|
139
|
+
# have been chosen with care to be the most basic "value in textual form" representation.
|
140
|
+
#
|
141
|
+
# A new String can be created from all other data types. The process is performed in
|
142
|
+
# several steps - first the type of the given value is inferred, then the resulting type
|
143
|
+
# is used to find the most significant format specified for that type. And finally,
|
144
|
+
# the found format is used to convert the given value.
|
145
|
+
#
|
146
|
+
# The mapping from type to format is referred to as the format map. This map
|
147
|
+
# allows different formatting depending on type.
|
148
|
+
#
|
149
|
+
# @example Positive Integers in Hexadecimal prefixed with '0x', negative in Decimal
|
150
|
+
#
|
151
|
+
# ~~~ puppet
|
152
|
+
# $format_map = { Integer[default, 0] => "%d",
|
153
|
+
# Integer[1, default] => "%#x"
|
154
|
+
# }
|
155
|
+
# String("-1", $format_map) # produces '-1'
|
156
|
+
# String("10", $format_map) # produces '0xa'
|
157
|
+
# ~~~
|
158
|
+
#
|
159
|
+
# A format is specified on the form:
|
160
|
+
#
|
161
|
+
# ~~~
|
162
|
+
# %[Flags][Width][.Precision]Format
|
163
|
+
# ~~~
|
164
|
+
#
|
165
|
+
# `Width` is the number of characters into which the value should be fitted. This allocated space is
|
166
|
+
# padded if value is shorter. By default it is space padded, and the flag 0 will cause padding with 0
|
167
|
+
# for numerical formats.
|
168
|
+
#
|
169
|
+
# `Precision` is the number of fractional digits to show for floating point, and the maximum characters
|
170
|
+
# included in a string format.
|
171
|
+
#
|
172
|
+
# Note that all data type supports the formats `s` and `p` with the meaning "default to string" and
|
173
|
+
# "default programmatic string representation" (which for example means that a String is quoted in 'p' format).
|
174
|
+
#
|
175
|
+
# ### Signatures of String conversion
|
176
|
+
#
|
177
|
+
# ~~~ puppet
|
178
|
+
# type Format = Pattern[/^%([\s\+\-#0\[\{<\(\|]*)([1-9][0-9]*)?(?:\.([0-9]+))?([a-zA-Z])/]
|
179
|
+
# type ContainerFormat = Struct[{
|
180
|
+
# format => Optional[String],
|
181
|
+
# separator => Optional[String],
|
182
|
+
# separator2 => Optional[String],
|
183
|
+
# string_formats => Hash[Type, Format]
|
184
|
+
# }]
|
185
|
+
# type TypeMap = Hash[Type, Variant[Format, ContainerFormat]]
|
186
|
+
# type Formats = Variant[Default, String[1], TypeMap]
|
187
|
+
#
|
188
|
+
# Callable[Any, Formats]
|
189
|
+
# ~~~
|
190
|
+
#
|
191
|
+
# Where:
|
192
|
+
# * `separator` is the string used to separate entries in an array, or hash (extra space should not be included at
|
193
|
+
# then end), defaults to `","`
|
194
|
+
# * `separator2` is the separator between key and value in a hash entry (space padding should be included as
|
195
|
+
# wanted), defaults to `" => ".
|
196
|
+
# * `string_formats` is a type to format map for values contained in arrays and hashes - defaults to `{Any => "%p"}`. Note that
|
197
|
+
# these nested formats are not applicable to containers which are always formatted as per the top level format specification.
|
198
|
+
#
|
199
|
+
# @example Simple Conversion to String (using defaults)
|
200
|
+
#
|
201
|
+
# ~~~ puppet
|
202
|
+
# $str = String(10) # produces '10'
|
203
|
+
# $str = String([10]) # produces '["10"]'
|
204
|
+
# ~~~
|
205
|
+
#
|
206
|
+
# @example Simple Conversion to String specifying the format for the given value directly
|
207
|
+
#
|
208
|
+
# ~~~ puppet
|
209
|
+
# $str = String(10, "%#x") # produces '0x10'
|
210
|
+
# $str = String([10], "%(a") # produces '("10")'
|
211
|
+
# ~~~
|
212
|
+
#
|
213
|
+
# @example Specifying type for values contained in an array
|
214
|
+
#
|
215
|
+
# ~~~ puppet
|
216
|
+
# $formats = { Array => {format => '%(a', string_formats => { Integer => '%#x' } }
|
217
|
+
# $str = String([1,2,3], $formats) # produces '(0x1, 0x2, 0x3)'
|
218
|
+
# ~~~
|
219
|
+
#
|
220
|
+
# Given formats are merged with the default formats, and matching of values to convert against format is based on
|
221
|
+
# the specificity of the mapped type; for example, different formats can be used for short and long arrays.
|
222
|
+
#
|
223
|
+
# ### Integer to String
|
224
|
+
#
|
225
|
+
# | Format | Integer Formats
|
226
|
+
# | ------ | ---------------
|
227
|
+
# | d | Decimal, negative values produces leading '-'
|
228
|
+
# | x X | Hexadecimal in lower or upper case. Uses ..f/..F for negative values unless # is also used
|
229
|
+
# | o | Octal. Uses ..0 for negative values unless # is also used
|
230
|
+
# | b B | Binary with prefix 'b' or 'B'. Uses ..1/..1 for negative values unless # is also used
|
231
|
+
# | c | numeric value representing a Unicode value, result is a one unicode character string, quoted if alternative flag # is used
|
232
|
+
# | s | same as d, or d in quotes if alternative flag # is used
|
233
|
+
# | p | same as d
|
234
|
+
# | eEfgGaA | converts integer to float and formats using the floating point rules
|
235
|
+
#
|
236
|
+
# Defaults to `d`
|
237
|
+
#
|
238
|
+
# ### Float to String
|
239
|
+
#
|
240
|
+
# | Format | Float formats
|
241
|
+
# | ------ | -------------
|
242
|
+
# | f | floating point in non exponential notation
|
243
|
+
# | e E | exponential notation with 'e' or 'E'
|
244
|
+
# | g G | conditional exponential with 'e' or 'E' if exponent < -4 or >= the precision
|
245
|
+
# | a A | hexadecimal exponential form, using 'x'/'X' as prefix and 'p'/'P' before exponent
|
246
|
+
# | s | converted to string using format p, then applying string formatting rule, alternate form # quotes result
|
247
|
+
# | p | f format with minimum significant number of fractional digits, prec has no effect
|
248
|
+
# | dxXobBc | converts float to integer and formats using the integer rules
|
249
|
+
#
|
250
|
+
# Defaults to `p`
|
251
|
+
#
|
252
|
+
# ### String to String
|
253
|
+
#
|
254
|
+
# | Format | String
|
255
|
+
# | ------ | ------
|
256
|
+
# | s | unquoted string, verbatim output of control chars
|
257
|
+
# | p | programmatic representation - strings are quoted, interior quotes and control chars are escaped
|
258
|
+
# | C | each :: name segment capitalized, quoted if alternative flag # is used
|
259
|
+
# | c | capitalized string, quoted if alternative flag # is used
|
260
|
+
# | d | downcased string, quoted if alternative flag # is used
|
261
|
+
# | u | upcased string, quoted if alternative flag # is used
|
262
|
+
# | t | trims leading and trailing whitespace from the string, quoted if alternative flag # is used
|
263
|
+
#
|
264
|
+
# Defaults to `s` at top level and `p` inside array or hash.
|
265
|
+
#
|
266
|
+
# ### Boolean to String
|
267
|
+
#
|
268
|
+
# | Format | Boolean Formats
|
269
|
+
# | ---- | -------------------
|
270
|
+
# | t T | 'true'/'false' or 'True'/'False' , first char if alternate form is used (i.e. 't'/'f' or 'T'/'F').
|
271
|
+
# | y Y | 'yes'/'no', 'Yes'/'No', 'y'/'n' or 'Y'/'N' if alternative flag # is used
|
272
|
+
# | dxXobB | numeric value 0/1 in accordance with the given format which must be valid integer format
|
273
|
+
# | eEfgGaA | numeric value 0.0/1.0 in accordance with the given float format and flags
|
274
|
+
# | s | 'true' / 'false'
|
275
|
+
# | p | 'true' / 'false'
|
276
|
+
#
|
277
|
+
# ### Regexp to String
|
278
|
+
#
|
279
|
+
# | Format | Regexp Formats (%/)
|
280
|
+
# | ---- | ------------------
|
281
|
+
# | s | / / delimiters, alternate flag replaces / delimiters with quotes
|
282
|
+
# | p | / / delimiters
|
283
|
+
#
|
284
|
+
# ### Undef to String
|
285
|
+
#
|
286
|
+
# | Format | Undef formats
|
287
|
+
# | ------ | -------------
|
288
|
+
# | s | empty string, or quoted empty string if alternative flag # is used
|
289
|
+
# | p | 'undef', or quoted '"undef"' if alternative flag # is used
|
290
|
+
# | n | 'nil', or 'null' if alternative flag # is used
|
291
|
+
# | dxXobB | 'NaN'
|
292
|
+
# | eEfgGaA | 'NaN'
|
293
|
+
# | v | 'n/a'
|
294
|
+
# | V | 'N/A'
|
295
|
+
# | u | 'undef', or 'undefined' if alternative # flag is used
|
296
|
+
#
|
297
|
+
# ### Default value to String
|
298
|
+
#
|
299
|
+
# | Format | Default formats
|
300
|
+
# | ------ | ---------------
|
301
|
+
# | d D | 'default' or 'Default', alternative form # causes value to be quoted
|
302
|
+
# | s | same as d
|
303
|
+
# | p | same as d
|
304
|
+
#
|
305
|
+
# ### Array & Tuple to String
|
306
|
+
#
|
307
|
+
# | Format | Array/Tuple Formats
|
308
|
+
# | ------ | -------------
|
309
|
+
# | a | formats with `[ ]` delimiters and `,`, alternate form `#` indents nested arrays/hashes
|
310
|
+
# | s | same as a
|
311
|
+
# | p | same as a
|
312
|
+
#
|
313
|
+
# See "Flags" `<[({\|` for formatting of delimiters, and "Additional parameters for containers; Array and Hash" for
|
314
|
+
# more information about options.
|
315
|
+
#
|
316
|
+
# The alternate form flag `#` will cause indentation of nested array or hash containers. If width is also set
|
317
|
+
# it is taken as the maximum allowed length of a sequence of elements (not including delimiters). If this max length
|
318
|
+
# is exceeded, each element will be indented.
|
319
|
+
#
|
320
|
+
# ### Hash & Struct to String
|
321
|
+
#
|
322
|
+
# | Format | Hash/Struct Formats
|
323
|
+
# | ------ | -------------
|
324
|
+
# | h | formats with `{ }` delimiters, `,` element separator and ` => ` inner element separator unless overridden by flags
|
325
|
+
# | s | same as h
|
326
|
+
# | p | same as h
|
327
|
+
# | a | converts the hash to an array of [k,v] tuples and formats it using array rule(s)
|
328
|
+
#
|
329
|
+
# See "Flags" `<[({\|` for formatting of delimiters, and "Additional parameters for containers; Array and Hash" for
|
330
|
+
# more information about options.
|
331
|
+
#
|
332
|
+
# The alternate form flag `#` will format each hash key/value entry indented on a separate line.
|
333
|
+
#
|
334
|
+
# ### Type to String
|
335
|
+
#
|
336
|
+
# | Format | Array/Tuple Formats
|
337
|
+
# | ------ | -------------
|
338
|
+
# | s | The same as p, quoted if alternative flag # is used
|
339
|
+
# | p | Outputs the type in string form as specified by the Puppet Language
|
340
|
+
#
|
341
|
+
# ### Flags
|
342
|
+
#
|
343
|
+
# | Flag | Effect
|
344
|
+
# | ------ | ------
|
345
|
+
# | (space) | space instead of + for numeric output (- is shown), for containers skips delimiters
|
346
|
+
# | # | alternate format; prefix 0x/0x, 0 (octal) and 0b/0B for binary, Floats force decimal '.'. For g/G keep trailing 0.
|
347
|
+
# | + | show sign +/- depending on value's sign, changes x,X, o,b, B format to not use 2's complement form
|
348
|
+
# | - | left justify the value in the given width
|
349
|
+
# | 0 | pad with 0 instead of space for widths larger than value
|
350
|
+
# | <[({\| | defines an enclosing pair <> [] () {} or \| \| when used with a container type
|
351
|
+
#
|
352
|
+
# Conversion to Boolean
|
353
|
+
# ---
|
354
|
+
#
|
355
|
+
# Accepts a single value as argument:
|
356
|
+
# * Float 0.0 is `false`, all other float values are `true`
|
357
|
+
# * Integer 0 is `false`, all other integer values are `true`
|
358
|
+
# * Strings
|
359
|
+
# * `true` if 'true', 'yes', 'y' (case independent compare)
|
360
|
+
# * `false` if 'false', 'no', 'n' (case independent compare)
|
361
|
+
# * Boolean is already boolean and is simply returned
|
362
|
+
#
|
363
|
+
# Conversion to Array and Tuple
|
364
|
+
# ---
|
365
|
+
#
|
366
|
+
# When given a single value as argument:
|
367
|
+
#
|
368
|
+
# * A non empty `Hash` is converted to an array matching `Array[Tuple[Any,Any], 1]`
|
369
|
+
# * An empty `Hash` becomes an empty array
|
370
|
+
# * An `Array` is simply returned
|
371
|
+
# * An `Iterable[T]` is turned into an array of `T` instances
|
372
|
+
#
|
373
|
+
# When given a second Boolean argument
|
374
|
+
# * if `true`, a value that is not already an array is returned as a one element array
|
375
|
+
# * if `false`, (the default), converts the first argument as shown above.
|
376
|
+
#
|
377
|
+
# @example ensuring value is array
|
378
|
+
#
|
379
|
+
# ~~~ puppet
|
380
|
+
# $arr = Array($value, true)
|
381
|
+
# ~~~
|
382
|
+
#
|
383
|
+
# Conversion to a `Tuple` works exactly as conversion to an `Array`, only that the constructed array is
|
384
|
+
# asserted against the given tuple type.
|
385
|
+
#
|
386
|
+
# Conversion to Hash and Struct
|
387
|
+
# ---
|
388
|
+
# Accepts a single value as argument:
|
389
|
+
#
|
390
|
+
# * An empty `Array` becomes an empty hash
|
391
|
+
# * An `Array` matching `Array[Tuple[Any,Any], 1]` is converted to a hash where each tuple describes a key/value entry
|
392
|
+
# * An `Array` with an even number of entries is interpreted as `[key1, val1, key2, val2, ...]`
|
393
|
+
# * An `Iterable` is turned into an `Array` and then converted to hash as per the array rules
|
394
|
+
# * A `Hash` is simply returned
|
395
|
+
#
|
396
|
+
# Conversion to a `Struct` works exactly as conversion to a `Hash`, only that the constructed hash is
|
397
|
+
# asserted against the given struct type.
|
398
|
+
#
|
399
|
+
# Creating a SemVer
|
400
|
+
# ---
|
401
|
+
# A SemVer object represents a single [Semantic Version](http://semver.org/).
|
402
|
+
# It can be created from a String, individual values for its parts, or a hash specifying the value per part.
|
403
|
+
# See the specification at [semver.org](http://semver.org/) for the meaning of the SemVer's parts.
|
404
|
+
#
|
405
|
+
# The signatures are:
|
406
|
+
#
|
407
|
+
# ~~~ puppet
|
408
|
+
#
|
409
|
+
# type PositiveInteger = Integer[0,default]
|
410
|
+
# type SemVerQualifier = Pattern[/\A(?<part>[0-9A-Za-z-]+)(?:\.\g<part>)*\Z/]
|
411
|
+
# type SemVerString = String[1]
|
412
|
+
# type SemVerHash =Struct[{
|
413
|
+
# major => PositiveInteger,
|
414
|
+
# minor => PositiveInteger,
|
415
|
+
# patch => PositiveInteger,
|
416
|
+
# Optional[prerelease] => SemVerQualifier,
|
417
|
+
# Optional[build] => SemVerQualifier
|
418
|
+
# }]
|
419
|
+
#
|
420
|
+
# function SemVer.new(SemVerString $str)
|
421
|
+
# function SemVer.new(
|
422
|
+
# PositiveInteger $major
|
423
|
+
# PositiveInteger $minor
|
424
|
+
# PositiveInteger $patch
|
425
|
+
# Optional[SemVerQualifier] $prerelease = undef
|
426
|
+
# Optional[SemVerQualifier] $build = undef
|
427
|
+
# )
|
428
|
+
# function SemVer.new(SemVerHash $hash_args)
|
429
|
+
#
|
430
|
+
# ~~~
|
431
|
+
#
|
432
|
+
# Examples of SemVer and SemVerRange usage:
|
433
|
+
#
|
434
|
+
# ~~~ puppet
|
435
|
+
#
|
436
|
+
# # As a type, SemVer can describe disjunct ranges which versions can be matched
|
437
|
+
# # against - here the type is constructed with two SemVerRange objects.
|
438
|
+
# $t = SemVer[SemVerRange('>=1.0.0 <2.0.0'), SemVerRange('>=3.0.0 <4.0.0')]
|
439
|
+
# notice(SemVer('1.2.3') =~ $t) # true
|
440
|
+
# notice(SemVer('2.3.4') =~ $t) # false
|
441
|
+
# notice(SemVer('3.4.5') =~ $t) # true
|
442
|
+
#
|
443
|
+
# ~~~
|
444
|
+
#
|
445
|
+
# Creating a SemVerRange
|
446
|
+
# ---
|
447
|
+
# A SemVerRange object represents a range of SemVer. It can be created from
|
448
|
+
# a String, from two SemVer instances, where either end can be given as
|
449
|
+
# a literal `default` to indicate infinity. The string format of a SemVerRange is specified by
|
450
|
+
# the [Semantic Version Range Grammar](https://github.com/npm/node-semver#ranges).
|
451
|
+
# Use of the comparator sets described in the grammar (joining with `||`) is not supported.
|
452
|
+
#
|
453
|
+
# The signatures are:
|
454
|
+
#
|
455
|
+
# ~~~ puppet
|
456
|
+
# type SemVerRangeString = String[1]
|
457
|
+
# type SemVerRangeHash = Struct[{
|
458
|
+
# min => Variant[default, SemVer],
|
459
|
+
# Optional[max] => Variant[default, SemVer],
|
460
|
+
# Optional[exclude_max] => Boolean
|
461
|
+
# }]
|
462
|
+
#
|
463
|
+
# function SemVerRange.new( SemVerRangeString $semver_range_string)
|
464
|
+
#
|
465
|
+
# function SemVerRange.new(
|
466
|
+
# Variant[default,SemVer] $min
|
467
|
+
# Variant[default,SemVer] $max
|
468
|
+
# Optional[Boolean] $exclude_max = undef
|
469
|
+
# }
|
470
|
+
# function SemVerRange.new(SemVerRangeHash $semver_range_hash)
|
471
|
+
# ~~~
|
472
|
+
#
|
473
|
+
# For examples, see `SemVer.new`.
|
474
|
+
#
|
475
|
+
# @since 4.5.0
|
476
|
+
#
|
477
|
+
Puppet::Functions.create_function(:new, Puppet::Functions::InternalFunction) do
|
478
|
+
|
479
|
+
dispatch :new_instance do
|
480
|
+
scope_param
|
481
|
+
param 'Type', :type
|
482
|
+
repeated_param 'Any', :args
|
483
|
+
optional_block_param
|
484
|
+
end
|
485
|
+
|
486
|
+
def new_instance(scope, t, *args)
|
487
|
+
result = catch :undefined_value do
|
488
|
+
new_function_for_type(t, scope).call(scope, *args)
|
489
|
+
end
|
490
|
+
assert_type(t, result)
|
491
|
+
return block_given? ? yield(result) : result
|
492
|
+
end
|
493
|
+
|
494
|
+
def new_function_for_type(t, scope)
|
495
|
+
@new_function_cache ||= Hash.new() {|hsh, key| hsh[key] = key.new_function(loader).new(scope, loader) }
|
496
|
+
@new_function_cache[t]
|
497
|
+
end
|
498
|
+
|
499
|
+
def assert_type(type, value)
|
500
|
+
Puppet::Pops::Types::TypeAsserter.assert_instance_of('new():', type, value)
|
501
|
+
end
|
502
|
+
end
|