| 1 | package HTTP::API::DataTypeMarker; | ||
| 2 | 85 | $HTTP::API::DataTypeMarker::VERSION = '1.24'; | |
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | HTTP::API::DataTypeMarker - mark request data so it serializes as a specific | ||
| 6 | type, since Perl scalars have no native boolean and no native "comma list" | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | use HTTP::API::Client; # re-exports everything below | ||
| 11 | |||
| 12 | $api->post( $url, { | ||
| 13 | enabled => xTRUE, # JSON: true / form: 1 | ||
| 14 | legacy => xTrue, # JSON/form string: "True" | ||
| 15 | tags => xCSV(1, 2, 3), # form: "tags=1,2,3" | ||
| 16 | } ); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Every value here is a blessed arrayref - either C<HTTP::API::DataTypeMarker::BOOL> | ||
| 21 | (from L</"xBOOLEAN($value)">) or C<HTTP::API::DataTypeMarker::CSV> (from | ||
| 22 | L</"xCSV(@values)">), referred to below by their unqualified names, C<BOOL> | ||
| 23 | and C<CSV>, for brevity - namespaced deliberately (not bare C<'BOOL'>/ | ||
| 24 | C<'CSV'>) so an unrelated class of the same short name elsewhere in a | ||
| 25 | calling application can't collide with what C<kvp2json_each>/ | ||
| 26 | C<kvp2str_each>'s C<ref($v) eq ...> checks are actually looking for. | ||
| 27 | C<BOOL> is recognized and unwrapped | ||
| 28 | specially by both C<kvp2json_each> and C<kvp2str_each>, since a bare Perl | ||
| 29 | scalar (C<1>, C<0>, C<"true">, ...) is always ambiguous about whether it | ||
| 30 | means a JSON boolean, a string, or a number. C<CSV> is only special-cased | ||
| 31 | by C<kvp2str_each>, which joins it into one comma-separated | ||
| 32 | C<key=value,value> instead of a key repeated per element - a | ||
| 33 | form-urlencoded-specific problem JSON doesn't have. C<kvp2json_each> has | ||
| 34 | no C<CSV> handling at all; a blessed C<CSV> arrayref satisfies Perl's | ||
| 35 | reftype-based C<ARRAY> check regardless of blessing, so it falls through | ||
| 36 | to the plain-array branch and JSON-encodes as an ordinary array | ||
| 37 | (C<xCSV(1,2,3)> becomes C<[1,2,3]>) - which is the natural JSON | ||
| 38 | representation of a list anyway. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | 85 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | 85 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 85 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | |||
| 46 | 85 | our @EXPORT = qw( xCSV xBOOLEAN | |
| 47 | xTRUE xFALSE | ||
| 48 | xTrue xFalse | ||
| 49 | xtrue xfalse | ||
| 50 | xt__e xf___e | ||
| 51 | ); | ||
| 52 | |||
| 53 | =head2 xCSV(@values) | ||
| 54 | |||
| 55 | Mark a list so it serializes as one comma-joined value instead of one | ||
| 56 | repeated key per element. C<xCSV(1, 2, 3)> becomes C<a=1,2,3> in a | ||
| 57 | form-urlencoded request instead of the default C<a=1&a=2&a=3>. Each | ||
| 58 | value is copied at call time - C<xCSV($x, $y)> then reassigning C<$x> | ||
| 59 | does not change what was already captured. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub xCSV { | ||
| 64 | 22 | ▶ | return bless [ @_ ], 'HTTP::API::DataTypeMarker::CSV'; |
|
Subroutine
xCSV: called CC=1 SCAR=0.0
Pod
documented
| |||
| 65 | } | ||
| 66 | |||
| 67 | =head2 xBOOLEAN($value) | ||
| 68 | |||
| 69 | The building block every C<x*> boolean marker below is made of. Wraps | ||
| 70 | C<$value> (a plain scalar or a scalar ref) so C<kvp2json_each>/ | ||
| 71 | C<kvp2str_each> unwrap and emit it verbatim instead of treating it as an | ||
| 72 | array. Use one of the named markers below rather than this directly unless | ||
| 73 | none of them fit. | ||
| 74 | |||
| 75 | A plain scalar is copied at call time, the same as C<xCSV>'s values - | ||
| 76 | C<xBOOLEAN($flag)> then reassigning C<$flag> does not change what was | ||
| 77 | already captured. Pass a scalar ref instead (C<xBOOLEAN(\$flag)>) to opt | ||
| 78 | into tracking C<$flag> live - the marker then always reflects whatever | ||
| 79 | C<$flag> currently holds when it's later read. Only a plain scalar or a | ||
| 80 | scalar ref is accepted - wrapping anything else (an arrayref, a | ||
| 81 | hashref) dies with a clear message when the marker is later encoded, | ||
| 82 | rather than silently stringifying it. | ||
| 83 | |||
| 84 | =head2 xTRUE / xFALSE | ||
| 85 | |||
| 86 | A real JSON boolean: C<true> / C<false> with no quotes in JSON output, and | ||
| 87 | C<1> / C<0> in a form-urlencoded request. | ||
| 88 | |||
| 89 | =head2 xTrue / xFalse | ||
| 90 | |||
| 91 | The literal string C<"True"> / C<"False">, quoted like any other string in | ||
| 92 | JSON, unescaped in a form-urlencoded request. | ||
| 93 | |||
| 94 | =head2 xtrue / xfalse | ||
| 95 | |||
| 96 | The literal string C<"true"> / C<"false"> (lowercase). | ||
| 97 | |||
| 98 | =head2 xt__e / xf___e | ||
| 99 | |||
| 100 | The single-character string C<"t"> / C<"f">. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub xBOOLEAN { | ||
| 105 | 47 | ▶ | return bless [ @_ ], 'HTTP::API::DataTypeMarker::BOOL'; |
|
Subroutine
xBOOLEAN: called CC=1 SCAR=0.0
Pod
documented
| |||
| 106 | } | ||
| 107 | |||
| 108 | sub xTRUE { | ||
| 109 | 10 | ▶ | return xBOOLEAN(\1); |
|
Subroutine
xTRUE: called CC=1 SCAR=0.0
Pod
documented
| |||
| 110 | } | ||
| 111 | |||
| 112 | sub xFALSE { | ||
| 113 | 6 | ▶ | return xBOOLEAN(\0); |
|
Subroutine
xFALSE: called CC=1 SCAR=0.0
Pod
documented
| |||
| 114 | } | ||
| 115 | |||
| 116 | sub xTrue { | ||
| 117 | 3 | ▶ | return xBOOLEAN('True'); |
|
Subroutine
xTrue: called CC=1 SCAR=0.0
Pod
documented
| |||
| 118 | } | ||
| 119 | |||
| 120 | sub xFalse { | ||
| 121 | 1 | ▶ | return xBOOLEAN('False'); |
|
Subroutine
xFalse: called CC=1 SCAR=0.0
Pod
documented
| |||
| 122 | } | ||
| 123 | |||
| 124 | sub xtrue { | ||
| 125 | 1 | ▶ | return xBOOLEAN('true'); |
|
Subroutine
xtrue: called CC=1 SCAR=0.0
Pod
documented
| |||
| 126 | } | ||
| 127 | |||
| 128 | sub xfalse { | ||
| 129 | 1 | ▶ | return xBOOLEAN('false'); |
|
Subroutine
xfalse: called CC=1 SCAR=0.0
Pod
documented
| |||
| 130 | } | ||
| 131 | |||
| 132 | sub xt__e { | ||
| 133 | 1 | ▶ | return xBOOLEAN('t'); |
|
Subroutine
xt__e: called CC=1 SCAR=0.0
Pod
documented
| |||
| 134 | } | ||
| 135 | |||
| 136 | sub xf___e { | ||
| 137 | 1 | ▶ | return xBOOLEAN('f'); |
|
Subroutine
xf___e: called CC=1 SCAR=0.0
Pod
documented
| |||
| 138 | } | ||
| 139 | |||
| 140 | 85 | 1; | |