Data type syntax

Each value in the Puppet language has a data type, like “string.” There is also a set of values whose data type is “data type.” These values represent the other data types. For example, the value String represents the data type of strings. The value that represents the data type of these values is Type.

You can use these special values to examine a piece of data or enforce rules. For example, you can test whether something is a string with the expression $possible_string =~ String, or specify that a class parameter requires string values with class myclass (String $string_parameter = "default value") { ... }.

Syntax

Data types are written as unquoted upper-case words, like String.

Data types sometimes take parameters, which make them more specific. For example, String[8] is the data type of strings with a minimum of eight characters.

Each known data type defines how many parameters it accepts, what values those parameters take, and the order in which they must be given. Some of the abstract types require parameters, and most types have some optional parameters available.

The general form of a data type is:
  • An upper-case word matching one of the known data types.

  • Sometimes, a set of parameters, which consists of:
    • An opening square bracket [ after the type’s name. There can’t be any space between the name and the bracket.

    • A comma-separated list of values or expressions. Arbitrary whitespace is allowed, but you can’t have a trailing comma after the final value.

    • A closing square bracket ].

The following example uses an abstract data type Variant, which takes any number of data types as parameters. One of the parameters provided in the example is another abstract data type Enum, which takes any number of strings as parameters:
Variant[Boolean, Enum['true', 'false', 'running', 'stopped']]
Note: When parameters are required, you must specify them. The only situation when you can leave out required parameters is if you’re referring to the type itself. For example, Type[Variant] is legal, even though Variant has required parameters.

Usage

Data types are useful in parameter lists, match (=~) expressions, case statements, and selector expressions. There are also a few less common uses for them.

Specify data types in your Puppet code whenever you can, aligning them in columns. Type your class parameters wherever possible, and be specific when using a type. For example, use an Enum for input validation, instead of using a String and checking the contents of the string in the code. You have the option to specify String[1] instead of String, because you might want to enforce non-empty strings. Specify data types as deeply as possible, without affecting readability. If readability becomes a problem, consider creating a custom data type alias.

Parameter lists

Classes, defined types, and lambdas all let you specify parameters, which let your code request data from a user or some other source. Generally, your code expects each parameter to be a specific kind of data. You can enforce that expectation by putting a data type before that parameter’s name in the parameter list. At evaluation time, Puppet raises an error if a parameter receives an illegal value.

For example, consider the following class. If you tried to set $autoupdate to a string like "true", Puppet would raise an error, because it expects a Boolean value:
class ntp (
  Boolean $service_manage = true,
  Boolean $autoupdate     = false,
  String  $package_ensure = 'present',
  # ...
) {
  # ...
}
Abstract data types let you write more sophisticated and flexible restrictions. For example, this $puppetdb_service_statusparameter accepts values of true, false, "true", "false", "running", and "stopped", and raises an error for any other value:
class puppetdb::server (
  Variant[Boolean, Enum['true', 'false', 'running', 'stopped']]
    $puppetdb_service_status = $puppetdb::params::puppetdb_service_status,
) inherits puppetdb::params {
  # ...
}

Cases

Case statements and selector expressions allow data types as their cases. Puppet chooses a data type case if the control expression resolves to a value of that data type. For example:
$enable_real = $enable ? {
  Boolean => $enable,
  String  => str2bool($enable),
  Numeric => num2bool($enable),
  default => fail('Illegal value for $enable parameter'),
}

Match expressions

The match operators =~ and !~ accept a data type on the right operand, and test whether the left operand is a value of that data type.

For example, 5 =~ Integer and 5 =~ Integer[1,10] both resolve to true.

Less common uses

The built-in function assert_type takes a value and a data type, and raises errors if your code encounters an illegal value. Think of it as shorthand for an if statement with a non-match (!~) expression and a fail() function call.

You can also provide data types as both operands for the comparison operators ==, !=, <, >, <=, and >=, to test whether two data types are equal, whether one is a subset of another, and so on.

Obtaining data types

The built-in function type returns the type of any value. For example, type(3) returns Integer[3,3].

List of Puppet data types

The following data types are available in the Puppet language.

For details on each data type, see the linked documentation or the specification document.

Data type Purpose Type category
Any The parent type of all types. Abstract
Array The data type of arrays. Data
Binary A type representing a sequence of bytes. Data
Boolean The data type of Boolean values. Data, Scalar
Callable Something that can be called (such as a function or lambda). Platform
CatalogEntry The parent type of all types that are included in a Puppet catalog. Abstract
Class A special data type used to declare classes. Catalog
Collection A parent type of Array and Hash. Abstract
Data A parent type of all data directly representable as JSON. Abstract
Default The "default value" type. Platform
Deferred A type describing a call to be resolved in the future. Platform
Enum An enumeration of strings. Abstract
Error A type used to communicate when a function has produced an error.
Float The data type of floating point numbers. Data, Scalar
Hash The data type of hashes. Data
Init A type used to accept values that are compatible of some other type's "new".
Integer The data type of integers. Data, Scalar
Iterable A type that represents all types that allow iteration. Abstract
Iterator A special kind of lazy Iterable suitable for chaining. Abstract
NotUndef A type that represents all types not assignable from the Undef type. Abstract
Numeric The parent type of all numeric data types. Abstract
Object Experimental. Can be a simple object only having attributes, or a more complex object also supporting callable methods.
Optional Either Undef or a specific type. Abstract
Pattern An enumeration of regular expression patterns. Abstract
Regexp The data type of regular expressions. Scalar
Resource A special data type used to declare resources. Catalog
RichData A parent type of all data types except the non serializeable types Callable, Iterator, Iterable, and Runtime. Abstract
Runtime The type of runtime (non Puppet) types. Platform
Scalar Represents the abstract notion of "value". Abstract
ScalarData A parent type of all single valued data types that are directly representable in JSON. Abstract
SemVer A type representing semantic versions. Scalar
SemVerRange A range of SemVer versions. Abstract
Sensitive A type that represents a data type that has "clear text" restrictions. Platform
String The data type of strings. Data, Scalar
Struct A Hash where each entry is individually named and typed. Abstract
Timespan A type representing a duration of time. Scalar
Timestamp A type representing a specific point in time Scalar
Tuple An Array where each slot is typed individually Abstract
Type The type of types. Platform
Typeset Experimental. Represents a collection of Object-based data types.
Undef The "no value" type. Data, Platform
URI A type representing a Uniform Resource Identifier Data
Variant One of a selection of types. Abstract

The Type data type

The data type of literal data type values is Type. By default, Type matches any value that represents a data type, such as Integer, Integer[0,800], String, or Enum["running", "stopped"]. You can use parameters to restrict which values Type matches.

Parameters

The full signature for Type is:
Type[<ANY DATA TYPE>]
This parameter is optional.
Position Parameter Data type Default Description
1 Any data type Type Any A data type, which causes the resulting Type object to only match against that type or types that are more specific subtypes of that type.
Examples:
Type
Matches any data type, such as Integer, String, Any, or Type.
Type[String]
Matches the data type String, as well as any of its more specific subtypes like String[3] or Enum["running", "stopped"].
Type[Resource]
Matches any Resource data type — that is, any resource reference.