Rinci::function(3pm)
β¨ NAME β¨
okay so bestie, this is Rinci::function(3pm) β Metadata for your functions/methods π
π SPECIFICATION VERSION π
1.1 β that's the spec version, periodt β¨
π VERSION π
This doc describes version 1.1.104 of Rinci::function (from Perl distribution Rinci), released on 2023-09-30. Mark your calendars, it's giving fresh π
β¨
π¦ INTRODUCTION π¦
This document describes metadata for functions/methods. Since the metadata properties describe features and the way a function works, this document also describes how a function should support those properties.
This specification is part of Rinci. Please do a read up on it first, if you have not already done so. (No cap, it'll help.)
π SPECIFICATION π
π Enveloped result π
Function should return an enveloped result to express error code/message as well as actual result. The envelope can be produced by the function itself, or added by a wrapper tool. Result envelope is modeled after HTTP or PSGI response; it is an array in the following format:
[STATUS*, MESSAGE, RESULT, META]
STATUS is a 3-digit integer and is the only required element, much like HTTP response status code and is explained further in "Envelope status codes". MESSAGE is a string containing error message. RESULT (or PAYLOAD) is the actual content to be returned and can be omitted or set to undef if the function does not need to return anything. META is called result metadata, a defhash containing extra data, analogous to HTTP response headers. Result metadata is specified further in Rinci::resmeta.
Here are some examples of enveloped results:
[200]
[200, "OK", 42]
[404, "Not found"]
[200, "Account created", {id=>9323}, {}]
[500, "Can't delete foo: permission denied", undef,
{"cmdline.exit_code"=>300, perm_err=>1}]
So yeah, an enveloped result can carry both error code/message and the actual result. It's also super easy to convert to an HTTP response message. Plus it can hold extra data, which is handy for stuff like the transaction protocol (explained in Rinci::Transaction).
β¨ Special arguments β¨
Special arguments are some known arguments that start with dash (-) and serve special purposes. You need not specify them in the "args" metadata property. Examples of special arguments include -dry_run, -tx_action, -action, -confirm/*, -arg_part_start, -arg_part_len, -res_part_start, -res_part_len. Many of them will be explained in other related sections/documents, but some will be described here:
-content_type_ARGNAMEβ This special argument can be used to describe the MIME content type of an argument, whereARGNAMEis the argument name. For example, if you are creating a function that accepts data upload (in Perl):
upload(data => '...', -content_type_data => 'image/jpeg');
πΈ Functions vs methods πΈ
Since in many programming languages (like Perl 5, Python, Ruby, PHP) static functions are not that differentiated from methods, functions and methods share the same Rinci spec. But there are certain properties that can be used to declare if a function is (also) a method or not. See "is_func", "is_meth", "is_class_meth" properties below for details.
π¦ Multiple dispatch π¦
This specification also does not (yet) have any recommendation on how to best handle functions in languages that support multiple dispatch, like Perl 6: whether we should create multiple metadata or just one. It is more up to the tool and what you want to do with the metadata.
π₯ Envelope status codes π₯
In general, status codes map directly to HTTP response status codes. Below are the suggestion on which codes to use (or avoid). An asterisk (*) marks which codes are not defined in HTTP specification and introduced by this specification.
1xxcode β Currently not used. Some tools likePerinci::CmdLineassumes that status code is always>= 200, so you should perhaps never used status less than 200.2xxcode - success200should be used to mean success.201(created) can be used to signal success when creating something.204(no content) can also be used when there is no content to show. In HTTP, browsers won't clear current page if response is206(instead of showing a blank page if response is200). Currently in Rinci there is no difference between200and204.206(partial content) can be used to signal partial content. See"partial"property in the"result"function property.207(multistatus) can be used to signal partial success, for example a function which processed 5 items wanted to report that it successfully processed 2 items but failed to process the rest 3 items. For each item's statuses, you can use result metadata property"results". Note:207is used by WebDAV. Note: When all functions instead of only some/partial items fail, you might want to return4xxor5xxinstead of207.
3xxcode - further actions needs to be taken by user agent (caller)301(moved) can be used to redirect callers to alternate location, although this is very rare.304(not modified, nothing done). Used for example by setup functions to indicate that nothing is being modified or no modifying action has been performed (seeSetup::*modules in CPAN).331*(confirmation required). Function requires confirmation, for example if action to be taken is dangerous or requires user's attention. Confirmation message/prompt from function can be returned in the message, or in the"confirm_prompt"property (e.g. to provide translations). Confirmation from caller is in the form of special argument"-confirm"with boolean value of true (TODO: A more detailed confirmation for different actions can be specified later in the form of"-confirm_XXX"special arguments.) For an example of application of this, seePerinci::Tx::Manager.
4xxcode - client (caller) side error400(bad request, bad arguments) should be returned when the function encounters invalid input. A function wrapper can return this code when the function arguments fail the argument schema validation (specified in the"args"property).401(authentication required).403(forbidden, access denied, authorization failed).404(not found). Can be used for example by an object-retrieval functions (likeget_user()) and the object is not found. For object-listing functions (likelist_users()), when there are no users found matching the requested criteria,200code should still be returned with an empty result (like an empty array or hash). Also in general, an object-deletion function (likedelete_user()) should also return200(or perhaps304, but200is preferred) instead of404when the object specified to be deleted is not found, since the goal of the delete function is reached anyway.408(request timeout).409(conflict). Can be used for example by acreate_user()function when receiving an already existing username.412(precondition failed). Similar to409, but can be used to indicate lack of resources, like disk space or bandwidth. For lacking authentication and authorization, use401and403respectively.413(request range not satisfiable). When client requests partial result outside the range of available. See the"partial"property in"result"property for examples.429(too many requests).- (EXPERIMENTAL)
44x*codes are reserved for function-specific codes. Each function is free to define what each number means. However, this is not really encouraged and should only be used if necessary. Function should perhaps stick to predefined codes here. To return more detailed status, result metadata can be used. 480*is general transaction error, e.g. transaction status is aborted so further requests for this transaction is ignored until transaction is aborted.484*(no such transaction).
5xxcode - server (callee) side error500is the general code to use when a failure occurs during the execution of a function. for example when adelete_file()function fails to delete specified file (though in this case it can also choose to return403instead, which is more specific).501(not implemented)503(service unavailable). You can use this when service is temporarily unavailable, e.g. when system load is too high, a required service is down, etc. Users should try again at a later time.507(insufficient storage)521(maximum retries reached)531*(bad metadata) is used when there is something wrong with the metadata.532*(failure in recording transaction) when there is a failure in updating transaction status or in preparing/committing/rolling back the transaction.- (EXPERIMENTAL)
54x*codes are reserved for function-specific codes. Each function is free to define what each number means. However, this is not really encouraged and should only be used if necessary. Function should perhaps stick to predefined codes here. To return more detailed status, result metadata can be used. - Try not to use code greater than
555, as some tools use (CODE-300) for error codes that must fit in one unsigned byte (likePerinci::CmdLine).
π« is_func π«
Bool. Specify that the function can be called as a static function (i.e. procedural, not as a method). Default is true if unspecified, but becomes false if is_meth or is_class_meth is set to true.
Example:
# specify that function can be called a method *as well as* a static function
is_meth => 1
is_func => 1 # if not specified, will default to false after is_meth set to 1
π« is_meth π«
Bool. Specify that the function can be called as an instance (object) method. Default is false.
Example:
# specify that function is a method
is_meth => 1
π« is_class_meth π«
Bool. Specify that the function can be called as a class method. Examples of class methods include the constructor, but there are others. Default is false.
Example:
# specify that function is a class method
is_class_meth => 1
π¦ args (function property) π¦
Hash. Specify arguments. Property value is defhash of argument names and argument specification. Argument name must only contain letters, numbers, and underscores (and do not start with a number).
Argument specification is a hash containing these keys:
schema(argument property) βData::Sahschema for argument value.default
Any. Give a default value for the argument. This one wins over the schema β the schema can totes specify a default too, but if both exist, this property takes precedence. It's giving "I want to reuse one shared schema across a bunch of arguments but give each one its own lil default" energy. Example: you got a ticket_status schema. In create_ticket you want the default status to be new, but in reply_ticket you want the default status to be answered. Boom, default is your girl. π
summary (argument property)
Str. Straight from DefHash. A one-line plaintext summary β literally the same vibe as the summary property in variable metadata. Short, sweet, iconic. β¨
req
Bool. Say the argument is required (and yes, its value can still be undef/null, bestie β required just means you gotta pass something, ok?). Default is false.
description (argument property)
Str. From DefHash. A longer description, marked-up text, same idea as the description property. Suggestion from the elders: format that text to 74 columns. π
tags (argument property)
Array of (str|hash). From DefHash. An array of tags that tools can use to categorize arguments. Not unlike the tags property, fr fr.
pos
Non-negative integer. The argument's position when it's specified in an ordered fashion, like in an array. Counting starts from zero, no cap. π’
slurpy
Bool. Only matters if pos is specified. Says whether the argument should gobble up all the remaining values in an ordered argument list into an array. She's hungry, she eats. π
Old alias: greedy
greedy
Old alias for slurpy. Will be removed in Rinci 1.2. So likeβ¦ use slurpy bestie, don't get attached. π
partial (argument property)
Bool. Whether or not the argument value can be sent partially. Only argument types that have a notion of length can be set as partial β e.g. str (unit is character), buf (unit is byte), and array (unit is element). π
There should be at most one argument with this property set to true. One. Not two. Don't push it.
To send a partial argument, you use the special arguments -arg_len (total argument length), -arg_part_start (start position of the part being sent), and -arg_part_len (length of the part being sent). Example, in Perl:
# function metadata
{
v => 1.1,
summary => "Upload a file",
args => {
name => {schema=>"str*", req=>1},
data => {schema=>"buf*", req=>1, partial=>1},
},
}
# function usage example: send the first 10MiB of data
upload_file(name=>"myvideo.mp4", data=>substr($data, 0, 10_000_000),
-arg_len => 24_500_000,
-arg_part_start => 0,
-arg_part_len => 10_000_000);
# send the next 10MiB
upload_file(name=>"myvideo.mp4", data=>substr($data, 10_000_000, 10_000_000),
-arg_len => 24_500_000,
-arg_part_start => 10_000_000,
-arg_part_len => 10_000_000);
# send the last 4.5 MiB
upload_file(name=>"myvideo.mp4", data=>substr($data, 20_000_000),
-arg_len => 24_500_000,
-arg_part_start => 20_000_000,
-arg_part_len => 4_500_000);
stream (argument property)
Bool. Set this to true and the function can declare that it accepts streaming data for this argument. Super useful when the value is huge or of undetermined/infinite length (iykyk). To send the value as a stream, the caller has to pass a subroutine reference (a callback) instead, and the function will call it over and over until it gets undef to signify the data is exhausted. π
cmdline_aliases
Hash. Specify aliases for use in command-line options (or any other situation where arguments get parsed from command-line-like options, bestie). Keys are alias names, and each value is itself a hash (the alias specification). Valid alias specification keys:
summaryβ a string, optionalschemaβ optional, defaults to the argument's schemais_flagβ bool, optional; if set to 1 it's a shortcut for specifyingschemato["bool", {"is":1}]codeβ a code to set the argument value, optional, will be given(\%args, $val); if not set, the default behavior is simply to set the argument value
cmdline_on_getopt
Code. A hook that gets called when the argument is specified as a command-line option. In Perl, the hook is called with a hash argument containing these keys:
arg(str, argument name)value(str, option value)args(hash, the argument hash defined so far)
This is a whole vibe if you want to process a command-line option directly, per-option, instead of waiting around for the final resulting argument value. Example, in Perl:
args => {
library => {
schema => ['array*' => of => 'str*'],
cmdline_aliases => { I => {} },
cmdline_on_getopt => sub {
my %args = @_;
require lib;
lib->import($args{value});
},
},
module => {
schema => ['array*' => of => 'str*'],
cmdline_aliases => { M => {} },
cmdline_on_getopt => sub {
my %args = @_;
require Module::Load;
Module::Load::load($args{value});
},
},
}
With command-line arguments like this:
-I dir1 -M mod1 -I dir2 -M mod2
Without any cmdline_on_getopt hooks, the function will receive this argument hash:
{ library => ['dir1', 'dir2'], module => ['mod1', 'mod2'] }
β¦but there's no way to know the order of the options as they were specified on the command-line. With the hooks, the function can load modules correctly (e.g. loading mod1 won't search in dir2, because that directory hasn't been added by -I yet). Slay. π
completion (argument property)
Code. A code to supply argument value completion. Explained in the examples. β¨
index_completion
Code. A code to supply argument element index completion. Applies to these argument types:
- hash: for completing hash pair keys. (See also
element_completionfor completing hash pair values.)
See the examples for how to use this property. π
element_completion
Code. A code to supply argument element value completion. Applies to these argument types:
- array: for completing array element values.
- hash: for completing hash pair values. (See also
index_completionfor completing hash keys.)
See the examples for how to use this property. π¦
is_password
Experimental. Bool. Describes that the argument holds a password. Programs can react to this in several ways β for example they can turn off echoing to the terminal when asking for the value from standard input. Or they can redact values to "****" when logging. π€«
cmdline_src
Str. Normally in a command-line program, command-line options and arguments map to function arguments β e.g. --arg 2 sets the foo argument to 2, and positional arguments (arguments which specify the pos property and optionally also a slurpy property with a true value) get or slurp command-line arguments.
But sometimes that's just not it. When you're supplying a larger amount of data, or a complex structure, or a stream, you might want to pull from other sources. Set cmdline_src to one of these values for exactly that purpose:
- file β The command-line option/argument value will be treated as a filename, and the function argument will be set to the content of the file (or, in the case of a streaming argument, to a callback which can be used to get the file's content).
- stdin β This means the program should get the function argument from the whole standard input until EOF.
Note that only one argument can have its source set tostdinorstdin_or_fileorstdin_or_filesorstdin_or_args.
- stdin_or_file β This means the program should get the argument value from the content of a file (the name of which is given from the first remaining command-line argument after all other arguments/options have been processed), or, if none is provided, from standard input.
- stdin_or_files β This means the program should get the argument value from the content of files (the names of which are taken from the remaining command-line arguments after all other arguments/options have been processed), or, if no command-line argument remains, from standard input. This behavior is similar to the
<>(diamond operator) in Perl. π - stdin_or_args β This means the program should get the argument value from remaining command-line argument(s), or, if no command-line argument remains, from standard input.
- stdin_line β This means the program should get the argument value from a line of standard input; the newline ending will be removed from the argument value. Arguments with
cmdline_srcofstdin_linewill be processed beforestdin/stdin_or_file/stdin_or_files/stdin_or_args.
Other sources might be defined in the future. π
TODO: Define web_src property?
TODO: A way to define record separator?
cmdline_prompt
Str. The string to display when asking for the argument value from stdin (if the cmdline_src property value is stdin_line).
TODO: cmdline_prompt_template?
meta
Experimental. Hash. This lets you specify argument submetadata, used e.g. when dealing with forms (a form field/widget can be a subform, bestie). The value is Rinci function metadata. π
element_meta
Experimental. Hash. This lets you specify argument element submetadata, used e.g. when dealing with forms (a form field/widget can contain an array of records/subforms). The value is Rinci function metadata. π
deps (argument property)
Hash. This property specifies the argument's dependencies to other arguments (but possibly to other things too, in the future, so like⦠watch this space). It's similar to the function's deps (function property) property. It's a hash of dep types and values. The most important dep type is arg (dependency to another argument). Some dep types are special: all, any, none. Example:
# function metadata property
args => {
delete => {
schema=>'bool',
},
force => {
summary => 'Force deletion',
schema => 'bool',
deps => {arg=>'delete'},
},
...
}
The above example states that the argument force "depends on" delete. What that means (usually) is that specifying force only makes sense when delete is also specified. In a CLI context:
% prog --delete --force
Specifying --force without --delete doesn't make sense. Periodt.
To depend on any one of several arguments, use the dep type any, e.g.:
fr fr, in actual metadata it looks like this:
# function metadata property
args => {
delete => {
schema=>'bool',
},
replace => {
schema=>'bool',
},
force => {
summary => 'Force deletion',
schema => 'bool',
deps => {any => [
{arg=>'delete'},
{arg=>'replace'},
]},
},
...
},
So in CLI context, this means --force can be specified when --delete or --replace is specified. like, any one of them, bestie, that's the whole point of the any dep type π
And when you wanna depend on several arguments at once, you use the dep type all, e.g.:
# function metadata property
args => {
red => {
schema=>'uint',
pos => 0,
},
green => {
schema=>'uint',
pos => 1,
},
blue => {
schema=>'uint',
pos => 2,
},
rgb16 => {
schema => 'Use 3x16bits = 48bits to represent RGB value instead of the default 3x8 = 24bits',
deps => {all => [
{arg=>'red'},
{arg=>'green'},
{arg=>'blue'},
]},
},
...
},
In CLI context, this means specifying --rgb16 only makes sense when all of --ed, --green, --blue are specified. (yes it really does say --ed in the original, no cap, we're keeping it exactly π)
filters
Experimental, so like, handle with care bestie π§. Array of string, or code. Filters to apply before argument is converted from text and validated. order matters, it's giving pipeline energy β¨
examples (argument property)
Array. Each element is sample argument values. But if it is a hash, it will be assumed as a DefHash with the actual value put in the value property. which lowkey means if your sample argument is a hash like {} (in JSON), you have to specify it as {"value":{}, "summary":"Optional summary..."} (in JSON). π
Note that a Sah schema can <em>also</em> have an examples clause which you can use to put examples in. And Rinci function metadata also has an examples property too. three places to put examples, pick your fighter π
Example function metadata and its implementation in Perl:
$SPEC{multiply2} = {
v => 1.1,
summary => 'Multiple two numbers',
args => {
a => {
summary => 'The first operand',
description => '... a longer description ...',
schema=>['float*', {
examples => [1, -10, 0, 3.333],
}],
pos => 0,
tags => ['category:operand'],
},
b => {
summary => 'The second operand',
description => '... a longer description ...',
schema => 'float*',
pos => 1,
tags => ['category:operand'],
examples => [
1, -10, 0, 3.333,
{value => 1e-10, summary => 'Blah blah'},
],
},
round => {
summary => 'Whether to round result',
description => '... a longer description ...',
schema => [bool => {default=>0}],
pos => 2,
tags => ['category:options'],
cmdline_aliases => {
r=>{},
R=>{summary=>'Equivalent to --round=0',
code=>sub { my ($args, $val) = @_; $args->{round}=0 }},
},
},
}
};
sub multiply2 {
my %args = @_;
my $res = $args{a} * $args{b};
$res = int($res) if $round;
[200, "OK", $res];
}
By default, without any wrapper, the function is called with a named hash style:
multiply2(a=>4, b=>3); # 12
But with the information from the metadata, a wrapper tool like Perinci::Sub::Wrapper is able to change the calling style to positional:
multiply2(4, 3.1, 1); # 12
A command-line tool will also enable the function to be called with named options <em>as well as</em> positional arguments. both?? both. slay β¨
% multiply2 --a 2 --b 3
% multiply2 2 --b 3
% multiply2 2 3
As mentioned earlier, cmdline_alises is parsed by the command-line option parser:
% multiply2 2 3.5 -r ; # equivalent to multiply2 2 3 --round
% multiply2 2 3.5 -R ; # equivalent to multiply2 2 3 --noround (--round=0)
Aliases in cmdline_aliases are not recognized as real arguments. they're little shortcuts, not actual params, iykyk π
multiply2(a=>4, b=>3, r=>0); # unknown argument r
Another example (demonstrates cmdline_aliases):
$SPEC{smtpd} = {
v => 1.1,
summary => 'Control SMTP daemon',
args => {
action => {
schema => ['str*' => {in=>[qw/status start stop restart/]}],
pos => 0,
req => 1,
cmdline_aliases => {
status => {
schema => [bool=>{is=>1}],
summary => 'Alias for setting action=status',
code => sub { $_[0]{action} = 'status' },
},
start => {
schema => [bool=>{is=>1}],
summary => 'Alias for setting action=start',
code => sub { $_[0]{action} = 'start' },
},
stop => {
schema => [bool=>{is=>1}],
summary => 'Alias for setting action=stop',
code => sub { $_[0]{action} = 'stop' },
},
restart => {
schema => [bool=>{is=>1}],
summary => 'Alias for setting action=restart',
code => sub { $_[0]{action} = 'restart' },
},
},
},
force => {
schema => 'bool',
},
},
};
Another example (demonstrates slurpy):
$SPEC{multiply_many} = {
v => 1.1,
summary => 'Multiple numbers',
args => {
nums => {
schema => ['array*' => {of=>'num*', min_len=>1}],
pos => 0,
slurpy => 1
},
},
};
sub multiply_many {
my %args = @_;
my $nums = $args{nums};
my $ans = 1;
$ans *= $_ for @$nums;
[200, "OK", $ans];
}
After wrapping, in positional mode it can then be called:
multiply_many(2, 3, 4); # 24
which is the same as (in normal named-argument style):
multiply_many(nums => [2, 3, 4]); # 24
In command-line:
% multiply-many 2 3 4
in addition to the normal:
% multiply-many --nums '[2, 3, 4]'
completion
This argument specification key specifies how to complete an argument value (e.g. in shell or Riap::HTTP) and is supplied an anonymous function as value. yes, a coderef, you got this π
The function will be called with these arguments: word=>... (which is the formed word so far), ci=>0|1 (whether completion should be done case-insensitively).
The function should return an array containing a list of possible candidates, <em>or</em> a hash containing these keys: completion (array, list of possible candidates) and extra keys for formatting hints, e.g.:
is_pathβ bool, whether the list of completion is path-like, meaning it can be traversed/dug to multiple levelspath_sepβ string, path separator charactertypeβ string, eitherfilename,env, or other types
For an example of implementation for this, see Perinci::Sub::Complete in Perl which provides tab completion for argument values. tab completion is the moment, honestly β¨
Example:
$SPEC{delete_user} = {
v => 1.1,
args => {
username => {
schema => 'str*',
pos => 0,
completion => sub {
my %args = @_;
my $word = $args{word} // "";
# find users beginning with $word
local $CWD = "/home";
return [grep {-d && $_ ~~ /^\Q$word/} <*>];
},
},
force => {schema=>[bool => {default=>0}]},
},
};
When delete_user is executed over the command line and the Tab key is pressed:
$ delete-user --force --username fo<tab>
$ delete-user fo<tab>
then bash will try to complete with usernames starting with fo. so satisfying omg π¦
element_completion
This is like completion, but for array or hash <em>elements</em>. Argument type must be array or hash. no other types, bestie, that's the rule
Example for array:
$SPEC{delete_users} = {
v => 1.1,
args => {
usernames => {
schema => ['array*' => of => 'str*'],
req => 1,
pos => 0,
slurpy => 1,
element_completion => sub {
my %args = @_;
my $word = $args{word} // "";
# find users beginning with $word
local $CWD = "/home";
my $res = [grep {-d && $_ ~~ /^\Q$word/} <*>];
# exclude users already mentioned by user
my $ary = $args{args}{usernames};
$res = [grep {!($_ ~~ @$ary)}] @$res;
return $res;
},
},
},
};
When delete_users is executed over the command line:
$ delete-users c<tab> ; # will complete with all users beginning with c
$ delete-users charlie c<tab> ; # will complete with users but exclude charlie
$ delete-users charlie chucky <tab> ; # and so on
Example for hash (as well as index_completion property to complete hash keys):
$SPEC{create_file} = {
v => 1.1,
args => {
filename => {
schema => 'str*',
req => 1,
pos => 0,
},
content => {
schema => 'buf*',
req => 1,
pos => 1,
},
mode => {
summary => 'Permission mode',
schema => 'posint*',
},
extra_attrs => {
'x.name.is_plural' => 1,
'x.name.singular' => 'extra_attr',
schema => ['hash*' => of => 'str*'],
index_completion => sub {
# complete with list of known attributes
my %args = @_;
require Complete::Util;
Complete::Util::complete_array_elem(
word => $args{word},
array => [qw/mtime ctime owner group/],
);
},
element_completion => sub {
my %args = @_;
my $word = $args{word} // "";
my $index = $args{index};
if ($index eq 'owner') {
require Complete::Unix;
return Complete::Unix::complete_user(word=>$word);
} elsif ($index eq 'group') {
require Complete::Unix;
return Complete::Unix::complete_group(word=>$word);
} else {
return undef;
}
},
},
},
};
So when create_file actually gets run from the command line π it looks a lil something like this:
$ create-file file1 "hello filesystem" --extra-attr <tab>; # will complete with list of known attributes
$ create-file file1 "hello filesystem" --extra-attr owner=<tab>; # will complete with list of Unix users
args_rels
Hash. Bestie this one is the β¨*drama coordinator*β¨ β it's how you tell the world which arguments are ride-or-die together and which ones absolutely cannot show up to the same party.
The value is actually a Sah schema hash clause set (peep the hash type over in Sah::Type). Your arguments get represented as a hash, and you can use all the various Sah clauses to express relationships between those arguments (the hash keys), because the Sah hash type lowkey supports exactly this kind of clause β think choose_one, choose_all, req_one, req_all, dep_any, dep_all, req_dep_any, req_dep_all. π
Examples, bestie:
args_rels => {
choose_one => ['delete', 'add', 'edit'],
choose_all => ['red', 'green', 'blue'],
}
So what that's saying is: only one of delete, add, edit can be specified. And if any of red, green, blue shows up, then all of them have to show up. In a CLI context this translates to:
% prog --delete item
% prog --delete --add item ; # error, both --delete and --add specified
% prog --red 255 --green 255 --blue 0
% prog --red 255 --blue 0 ; # error, --green is missing
Another example:
XXX
args_as
Str. This tells you in what form the function expects its arguments. And ngl, the value is actually implementation-specific, because it's describing the function implementation itself.
For example in Perinci for Perl, these values are the ones that get recognized: array, hash, arrayref, hashref. Why do you care? Because it's useful for a wrapper β it lets the wrapper convert one form to another. π
The default value is also left up to the implementation. (iykyk)
For interimplementation communication (like via Riap::HTTP or Riap::TCP), named arguments are always used, so this property is just... irrelevant there. π
result
DefHash. This is where you specify the function's return value. It's a defhash containing these keys:
summary(result property) β From DefHash. Just like thesummaryproperty in variable metadata. β¨description(result property) β From DefHash. Just like thedescriptionproperty. Suggested to be formatted to 78 columns.schema(result property) β A Sah schema to validate the result (that's the third element in the envelope result). Important: this schema should only be tested if status is 200. See also:statuses.statusesβ Hash. Lets you specify a different result schema for different statuses. For example:
statuses => {
206 => {
schema => 'str*',
},
}
stream(result property) β Bool. Specify that the function returns streaming output. Note that a function can also signify streaming output by setting the result metadata propertystreamto true.
The function must then return a subroutine reference (a callback) as its actual result, which the caller can call over and over until it gets undef to signify exhaustion. π
partial(result property) β Bool. If set to true, it says it's possible to request a partial result. Classic example: a function that reads contents from (potentially large) files:
# function metadata
{
v => 1.1,
summary => 'Read file contents',
args => {
name => {
summary => 'File name',
schema => 'str*',
req => 1,
},
},
result => {schema=>'buf*', partial=>1},
}
# example function usage: request to read first 10MiB of file content,
# -result_part_start defaults to 0.
my $res = read_file(name=>'myvideo.mp4', -res_part_len=>10000000);
# => [206, "Partial content", "data...", {len=>24500000, part_start=>0, part_len=>10000000}]
# request the next 10MiB
my $res = read_file(name=>'myvideo.mp4', -res_part_start=>10000000, -res_part_len=>10000000);
# => [206, "Partial content", "data...", {len=>24500000, part_start=>10000000, part_len=>10000000}]
# request the next 10MiB, since this actual file size is only 24500000,
# function should return 416 status
my $res = read_file(name=>'myvideo.mp4', -res_part_start=>20000000, -res_part_len=>10000000);
# => [416, "Request range not satisfiable, file size is only 24500000"]
# request the next 4.5MiB, this time succeeds
my $res = read_file(name=>'myvideo.mp4', -res_part_start=>20000000, -res_part_len=>4500000);
# => [206, "Partial content", "data...", {len=>24500000, part_start=>20000000, part_len=>4500000}]
Heads up though: asking a function for a partial result when that function doesn't even support partial results might have the effect of the whole content being returned (status 200), or status 416. No in-between, bestie. π
Also note β since functions normally return an enveloped result, instead of just returning:
RESULT
your functions normally have to return an enveloped result:
[STATUS, MESSAGE, RESULT, METADATA]
Examples, because we love a visual π«Ά:
# result is an integer
result => {schema => 'int*'}
# result is an integer starting from zero
result => {schema => ['int*' => {ge=>0}]}
# result is an array of records
result => {
summary => 'Matching addressbook entries',
schema => ['array*' => {
summary => 'blah blah blah ...',
of => ['hash*' => {allowed_keys=>[qw/name age address/]} ]
}]
}
result_naked
Bool. Set this to true and you're saying the function does not envelope its results. The default is false, because we're all about encouraging functions to create envelopes. π
But β a wrapper should be able to create or strip an envelope if it needs to. Like, say you've got some "traditional" functions that just don't do envelopes? Set this property to true and the wrapper can generate the envelope for them. Slay. β¨
examples (function property)
Array. This property is your moment to put examples in a detailed, structured way β as an alternative to just dumping everything into description.
Every single example is a defhash. It says what arguments are used, what the results are, and some description. You can use it when generating API/usage documentation, and also for testing data. It can also be used for testing (the function gets run with the specified arguments and the result gets matched against the expected result). Known properties, coming right up π:
args(function example property) β Hash. The arguments used to produce the result. A tool can convert this toargv, e.g. when displaying command-line examples.argvβ Array. An alternative toargs, for example when the function is run from the command line. Most of the time it can be converted toargswhen you wanna display examples in Perl instead of on the command line.srcβ Str. An alternative toargsorargv, for providing raw source code. See also:src_plang. This one's for showing more general examples β like how a function is used inside an expression or code block, or how a command-line program is used in a shell script.
Exactly one of args, argv, or src must be specified. Not two, not zero. Periodt. π
src_plangβ Str. The programming language that the example's source codesrcis written in. Valid values include:perl,bash.
Command-line interface tools will typically only show examples written in bash or other shells, while Perl module tools will typically only show perl examples. πΈ
Required if src is specified.
statusβ Int. Status from the envelope. If it's unspecified, it's assumed to be 200.resultβ Any. Expected result.env_resultβ Array. Expected enveloped result.naked_resultβ Any. Expected naked result (payload). πsummary(function example property) β From DefHash. A one-line summary of the example. You should describe, in one phrase or sentence, what the example is trying to demonstrate. You can skip the summary if the example is pretty basic or things are already clear from theargsalone.description(function example property) β From DefHash. Longer marked up text about the example (discussion, things to note, whatever), suggested to be formatted to 72 columns.tags(function example property) β From DefHash. π·οΈtestβ Bool. Defaults to true. Whether to actually test the example or not. Examples are by default run as tests by a test module (e.g. Perl module Test::Rinci). Setting this to 0 disables this example from being included in a test.
TODO: more detailed testing instruction (e.g. only test in release candidate, or under certain environment flag, etc).
Example:
# part of metadata for Math::is_prime function
examples => [
{
args => {num=>10},
result => 0,
# summary no needed here, already clear.
},
{
args => {},
result => 400,
summary => 'Num argument is required',
},
{
argv => [-5],
result => 1,
summary => 'Also works for negative integers',
},
],
Another example showing off src for a function called list_countries:
examples => [
{
src => 'for c in `list-countries`; do wget http://flags.org/country/$c; done',
src_plang => 'bash',
},
{
src => <<'EOT',
my $res = list_countries(detail => 1, sort=>['-popsize']);
die "Can't list countries: $res->[0] - $res->[1]" unless $res->[0] == 200;
my $i = 0;
for my $c (@{ $res->[2] }) { $i++; say "$i. $_->{name}'s population: $_->{popsize}";
EOT
src_plang => 'perl',
},
],
features
DefHash. This is where functions get to flex what they can do. Each hash key holds a feature name, and that name must only contain letters, numbers, and underscores. No dashes, no vibes-based naming, bestie.
Here's the list of defined features. New feature names may be defined by extension. π
reverseβ Bool. Default: false. If you set it to true, it specifies that the function supports a reverse operation. To reverse, the caller can just add the special argument-reverse. For example:
$SPEC{triple} = {
v => 1.1,
args => {num=>{schema=>'num*'}},
features => {reverse=>1}
};
sub triple {
my %args = @_;
my $num = $args{num};
[200, "OK", $args{-reverse} ? $num/3 : $num*3];
}
triple(num=>12); # => 36
triple(num=>12, -reverse=>1); # => 4
txβ Hash. Default is none. bestie this one says "i support transactions", exactly like Rinci::Transaction spells out. the hash has one key:v(int, the protocol version, and if u don't say it, the default is1). π
fr fr go read Rinci::Transaction if u want the whole tea on transactions, this page is not gonna spill it all.
dry_runβ Bool, or hash. set it true and ur saying "yep this func supports dry-run (simulation) mode". u can also hand it a hash like<{default="1"}>which means "supports dry-run AND defaults to dry-run mode". look at this lil angel π
use Log::ger;
$SPEC{rmre} = {
summary => 'Delete files in curdir matching a regex',
args => {re=>{schema=>'str*'}},
features => {dry_run=>1}
};
sub rmre {
my %args = @_;
my $re = qr/$args{re}/;
my $dry_run = $args{-dry_run};
opendir my($dir), ".";
while (my $f = readdir($dir)) {
next unless $f =~ $re;
log_info "Deleting $f ...";
next if $dry_run;
unlink $f;
}
[200, "OK"];
}
so that Perl function deletes files, but if u pass it -dry_run => 1 (simulation mode) it will NOT actually delete anything, it just prints which files match and would have been deleted. fake it till u make it energy β¨
saying a function supports dry_run means, among other things:
- If dry_run is requested, function will have no side effects
it'll act like a pure function, and therefore gets all the pure-function properties. slay.
pureβ Bool. Default: false. true means the function is "pure" and has no "side effects" (these are words from functional programming / computer science, iykyk). a side effect = changing something, somewhere (setting a global var, mutating its args, writing to disk, changing system date/time, etc etc). declaring a function pure means, among other things:- it can safely be inculded in a transaction without recording in a journal;
- it can safely be included during a dry run;
immutableβ Bool. Default is false. true means the function always returns the same result when given the same argument values. that unlocks glow-ups like memoization π . a classic immutable example issub { $_[0]+$_[1] }β results depend on nothing but the args. mutable examples?rand(), orread()pulling contents out of a file. messy.idempotentβ Bool. Default is false. true = the function is idempotent. idempotency means running it over and over (same args every time) hits the same as running it once. in other words, extra invocations do literally nothing. no cap.
some ops are inherently idempotent, like reading a DB row or a file's contents (or to be exact, nullipotent β¨). another example: setting/updating an entity to some specific value, or deleting an entity. spam that and it still sets the entity to the same value, still deletes the entity.
some ops are inherently non-idempotent, like sending an email π§. run it again and boom, multiple emails. tragic.
and then there's the third group: not idempotent, but u can make them idempotent by checking whether the target object(s) has (have) reached the final desired state (and optionally also checking they started in the correct original state). e.g. a file-renaming function can record the original file it renamed (its MD5 checksum, size, whatever) or log the action in a history DB or flag file, and then refuse to rename again if the file sitting there isn't the original one. iconic behavior honestly.
check_argβ Bool. Default is false. true means the function supports checking just one single argument. super useful for form processing where u only wanna validate one form field (one function argument). to check a single arg, u pass the-actionspecial argument with valuecheck_arg, plus the argument she wants checked. the function should check that argument and immediately return 200 status on success, or 400 on validation failure. periodt.
π¦ deps (function property) π¦
Hash. this property lists out the function's dependencies on all kinds of stuff. it's a hash of dep types β values. some dep types are special-special: all, any, and none. π
deps => {
DEPTYPE => DEPVALUE,
...,
all => [
{DEPTYPE=>DEPVALUE, ...},
...,
],
any => [
{DEPTYPE => DEPVALUE, ...},
...,
],
none => [
{DEPTYPE => DEPVALUE, ...},
....,
],
}
a dependency can be any type at all β another function, environment variables, programs, OS software packages, u name it. it's up to the dependency checker library to actually make use of this info.
for the dependencies to count as satisfied, all of the clauses must be satisfied.
here's the list of defined dependency types. new dependency types can be defined by an extension, so the world is ur oyster π¦ͺ
envβ Str. requires that an environment variable exists and is true, where "true" is the Perl sense of true (not an empty string, not"0"; note that" "and"0.0"are BOTH true). example:
env => 'HTTPS'
progβ Str or defhash. requires that a program exists and is executable. if the STR doesn't contain a path separator character/, it'll get searched for inPATH. Windows filesystem should still use Unix-style paths, like"C:/Program Files/Foo/Bar.exe".
prog => 'rsync' # any rsync found on PATH
prog => '/bin/su' # won't accept any other su
instead of just a string, the dep value can also be a defhash. currently known properties for that defhash: name (program name or path, str, required), path (program path, str, optional), min_version (minimum version, dotted number version str, optional).
pkgβ Str. declares a dependency on a Riap package. STR must be a valid Riap package URI string. the checker can check that requestinginfoon this URI succeeds and that the type really ispackage. example:
pkg => '/Foo/'
funcβ Str. declares a dependency on a Riap function. STR must be a valid Riap function URI string. the checker can check that requestinginfoon this URI succeeds and that the type really ispackage. example:
pkg => '/Foo/somefunc'
pkg => 'http://gudangapi.com/ga/list_ubuntu_releases'
codeβ Code. requires that the anonymous function returns a true value after being called, where "true" depends on the host language. example in Perl:
code => sub {$>} # i am not being run as root
example in Ruby:
"code" => Proc.new { Process.euid > 0 } # i am not being run as root
tmp_dirβ Bool. set to 1 and it means the function needs a temporary directory. the caller should hand over the path using the special argument-tmp_dir.trash_dirβ Bool. set to 1 and it means the function needs a trash directory. trash is not unlike a temporary directory, bestie. the caller should provide the path to the trash directory using the special argument-trash_dir.
a trash directory can be provided, e.g. by the transaction manager (peep Rinci::Transaction).
allβ Array of deps. a "meta" type that lets several dependencies get joined together in a logical-AND fashion. every dependency hash must be satisfied. e.g. to declare a dependency on several programs and an environment variable:
all => [
{prog => 'rsync'},
{prog => 'tar'},
{env => 'FORCE'},
],
anyβ Array of deps. likeall, but it's a logical-OR relationship instead. any one of the dependencies will suffice. e.g. to specify a requirement on alternative Perl modules:
or => [
{perl_module => 'HTTP::Daemon'},
{perl_module => 'HTTP::Daemon::SSL'},
],
noneβ Array of deps. says that none of the dependencies must be satisfied for this type to be satisfied. e.g. to specify that the function must not run under SUDO or by root:
none => [
{env => 'SUDO_USER' },
{code => sub {$> != 0} },
],
heads up tho, the above is NOT equivalent to the below:
none => [
{env => 'SUDO_USER', code => sub {$> != 0} },
],
which means that if none or only one of 'env'/'code' is satisfied, the whole dependency becomes a success (since it's negated by 'none'). probably <em>not</em> what u want. π
if u add a new language-specific dependency type, pls pls pls prefix it with the language code, e.g. perl_module, perl_func, ruby_gem, python_egg. these dependency types have also been defined by some existing tools: deb (dependency to a Debian package), rpm (dependency to an RPM package), js_url (loading a remote JavaScript script URL), file (existence of a), perl_run_func (running a Perl subroutine and getting a successful enveloped result). some of these might get declared as part of the core dependency types in the future π
π FAQ π
πΈ What is the difference between summary or description in the Sah schema and arg specification?
Bestie, look at this example:
{
args => {
src => {
summary => "Source path",
description => "...",
schema => ["str*", {
summary => "...",
description => "...",
...
}],
...
},
dest => {
summary => "Target path",
description => "...",
schema => ["str*", {
summary => "...",
description => "...",
...
}],
...
},
...
},
}
So like, see how every argument has its own summary and description, but the schema for each argument ALSO has summary and description schema clauses? Two summaries. Two descriptions. Which one goes where?? π
Here's the tea β: the argument specification's summary (and description) describe the argument itself. In this example it's saying src means "The source path" and dest means "The target path". Meanwhile the argument schema's summary (and description) describe the data type and valid values. In this example that could say, like, "a Unix-path string with a maximum length of 255 characters". π
And honestly? src and dest are probably the same type (Unix path) and can totally share a schema, like:
{
...
args => {
src => {
...
schema => "unix_path",
},
dest => {
...
schema => "unix_path",
},
...
},
}
Sharing is caring, no cap. π«Ά
π¦ What is the difference between setting req=>1 in the argument specification and req=>1 in schema?
Okay this one is iconic and also lowkey a trap, so pay attention:
# Note: remember that in Sah, str* is equivalent to [str => {req=>1}]
args => {
a => { schema=>"str" },
b => { schema=>"str*" },
c => { req=>1, schema=>"str" },
d => { req=>1, schema=>"str*" },
}
Look at b and c specifically, because they are not the same thing and the way that they're different is so sneaky. π
bis not a required argument (noreq=>1in the argument spec), but if it is specified, then it cannot beundef/null(since the schema says[str=>{req=>1}], a.k.astr*).cis a required argument (req=>1in the argument spec), but you can specifyundef/nullas the value.
So the following are valid:
func(c=>undef, d=>1);
But the following are not:
func(b=>1, d=>1); # c is not specified
func(b=>undef, c=>1, d=>1); # b has undef value
func(b=>1, c=>1, d=>undef); # d has undef value
Required and non-null are two different vibes. Don't confuse them. π¨
β¨ Should I add a new metadata property, or add a new feature name to the features property, or add a new dependency type to the deps property?
The flowchart is giving very clear energy:
- If your property describes a dependency to something, it should definitely be a new dependency type.
- If your property only describes what the function can do and does not include any wrapper code, then it probably goes into
features. - Otherwise, it should probably become a new metadata property.
Example time: if you wanna declare that your function can only be run under a certain moon phase (e.g. full moon π), that should definitely go as a new dependency type, so it becomes: deps => { moon_phase => 'full' }. Slay, honestly.
Another example: reverse is a feature name, because it just states that if we pass "-reverse" => 1 special argument to a reversible function, it can do a reverse operation. It doesn't include any wrapper code β all functionality is realized by the function itself. Meanwhile, timeout is a metadata property, because it involves adding adding some wrapping code (a timeout mechanism, e.g. an eval() block and alarm() in Perl). Wrap it up, literally. π
π HOMEPAGE
Please visit the project's homepage at <https://metacpan.org/release/Rinci>.
π§ SOURCE
Source repository is at <https://github.com/perlancar/perl-Rinci>.
π SEE ALSO
Related specifications: Sah, HTTP/1.1 (RFC 2068)
Rinci
π AUTHOR
perlancar <perlancar@cpan.org>
π CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub. So many options bestie, pick your fave. β¨
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. π
π COPYRIGHT AND LICENSE
This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
π BUGS
Please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=Rinci>
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
And that's the whole entire page, bestie!! π«Ά We did it. This was Rinci::function(3pm), shipped with perl v5.38.2, dated 2024-01-20. Go forth and specify your functions gorgeously. Periodt. π
β¨
π¦ where this page comes from
- debian trixie: librinci-perl 1.1.104-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: librinci-perl 1.1.104-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: librinci-perl 1.1.104-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: librinci-perl 1.1.104-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms