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