include/punk/punk_model.h

filter: Statementstmt 89.8% 221 / 246 Branchbran 58.8% 160 / 272 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.5% 381 / 518 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_csrf.h ↑ Summary xs/oamount.xs »
1 /* punk_model.h - the storage-agnostic model tier.
2 *
3 * Two halves, both here. At boot: `use Punk::Model` installs the
4 * table/field/database/validate keywords into the model class (magic CVs
5 * carrying that class's metadata - the punk_static.h closure pattern), and
6 * _instantiate resolves the backend and compiles the create/update
7 * validators once per worker. Per call: the six-method contract, which is
8 * a metadata read plus one method call into the backend, with no Perl
9 * frame of its own.
10 *
11 * Metadata stays a Perl hash: the field specs come from user code and are
12 * handed to JSON::Schema::Fast unchanged, so there is nothing to gain by
13 * copying them into a C struct - only shapes to lose.
14 */
15  
16 #ifndef PUNK_MODEL_H
17 #define PUNK_MODEL_H
18  
19 /* class name -> metadata HV. One entry per model class, for the life of
20 * the process; model classes are declared at compile time and never go
21 * away, so this is a registry rather than a cache. */
22 static HV *PM_META = NULL;
23  
24 354 static HV *pm_registry(pTHX) {
25 354 ▶ if (!PM_META) PM_META = newHV();
Branch: 1 branch
BranchTrueFalseTotal
if (!PM_META) PM_META = newHV(); 24 330 354
26 354 return PM_META;
27 }
28  
29 /* The JSON-Schema-relevant field keys, copied straight into the property
30 * schema; type coercion and the rest are the validator's business. */
31 static const char *const PM_SCHEMA_KEYS[] = {
32 "type", "format", "pattern", "enum",
33 "minLength", "maxLength", "minimum", "maximum",
34 "multipleOf", "minItems", "maxItems", NULL
35 };
36  
37 4449 static SV *pm_get(pTHX_ HV *hv, const char *k) {
38 4449 SV **e = hv_fetch(hv, k, (I32)strlen(k), 0);
39 4449 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 2023 2426 4449
return (e && *e) ? *e : NULL; 2023 0 2023
40 }
41  
42 /* The class's OWN metadata: an exact registry hit, no inheritance. This is
43 * what `import` asks, so that a subclass gets its own entry to declare into
44 * rather than being mistaken for one that has already been set up. */
45 296 static HV *pm_meta_own(pTHX_ SV *class_sv) {
46 296 HE *he = hv_fetch_ent(pm_registry(aTHX), class_sv, 0, 0);
47 296 ▶ if (!he) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!he) return NULL; 171 125 296
48 {
49 125 SV *v = HeVAL(he);
50 125 ▶ return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) 125 0 125
return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) 125 0 125
51 250 ▶ ? (HV *)SvRV(v) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(v) : NULL; 125 0 125
52 }
53 }
54  
55 /* The metadata a class reads: its own, else the nearest ancestor's.
56 *
57 * A subclass that only says `use parent 'Some::Model'` declares nothing of
58 * its own and inherits the whole table, which is what a subclass of a model
59 * is for. Depth-first over @ISA, the order method resolution uses, with a
60 * depth bound so a cyclic @ISA cannot spin. */
61 0 static HV *pm_meta_isa(pTHX_ SV *class_sv, int depth) {
62 0 HV *own = pm_meta_own(aTHX_ class_sv);
63 AV *isa;
64 SSize_t i, n;
65  
66 0 ▶ if (own) return own;
Branch: 1 branch
BranchTrueFalseTotal
if (own) return own; 0 0 0
67 0 ▶ if (depth >= 16) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= 16) return NULL; 0 0 0
68  
69 0 isa = get_av(form("%s::ISA", SvPV_nolen(class_sv)), 0);
70 0 ▶ if (!isa) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!isa) return NULL; 0 0 0
71  
72 0 n = av_len(isa) + 1;
73 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
74 0 SV **e = av_fetch(isa, i, 0);
75 HV *m;
76 0 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 0 0 0
if (!(e && *e && SvOK(*e))) continue; 0 0 0
if (!(e && *e && SvOK(*e))) continue; 0 0 0
77 0 m = pm_meta_isa(aTHX_ *e, depth + 1);
78 0 ▶ if (m) return m;
Branch: 1 branch
BranchTrueFalseTotal
if (m) return m; 0 0 0
79 }
80 0 return NULL;
81 }
82  
83 /* The class and its ancestors that declared metadata, nearest first. */
84 246 static void pm_meta_chain(pTHX_ SV *class_sv, AV *out, int depth) {
85 HV *own;
86 AV *isa;
87 SSize_t i, n;
88  
89 246 ▶ if (depth >= 16) return;
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= 16) return; 0 246 246
90 246 own = pm_meta_own(aTHX_ class_sv);
91 246 ▶ if (own) av_push(out, newRV_inc((SV *)own));
Branch: 1 branch
BranchTrueFalseTotal
if (own) av_push(out, newRV_inc((SV *)own)); 125 121 246
92  
93 246 isa = get_av(form("%s::ISA", SvPV_nolen(class_sv)), 0);
94 246 ▶ if (!isa) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!isa) return; 120 126 246
95 126 n = av_len(isa) + 1;
96 252 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 126 126 252
97 126 SV **e = av_fetch(isa, i, 0);
98 126 ▶ if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); 126 0 126
if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); 126 0 126
if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); 126 0 126
99 }
100 }
101  
102 /* The metadata a class reads, with its ancestors' merged in.
103 *
104 * This is done at LOOKUP and not at `use Punk::Model`, because `table` and
105 * `field` are statements in the package body: they run when the class is
106 * loaded, long after import decided what the class knew. A subclass seeded at
107 * import time would copy an empty parent.
108 *
109 * Nearest wins: a subclass may add fields, or redeclare one to change its
110 * spec, and names it does not mention keep the parent's. The merged hash is
111 * cached under its own registry key, so the merge happens once per class and
112 * the parent's own entry is never disturbed.
113 */
114 120 static HV *pm_meta_merged(pTHX_ SV *class_sv) {
115 120 AV *chain = (AV *)sv_2mortal((SV *)newAV());
116 HV *m;
117 AV *fields;
118 HV *field;
119 SSize_t i, n;
120  
121 120 pm_meta_chain(aTHX_ class_sv, chain, 0);
122 120 n = av_len(chain) + 1;
123 120 ▶ if (n == 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return NULL; 0 120 120
124 /* the common case: nothing inherited, so nothing to merge */
125 120 ▶ if (n == 1) return (HV *)SvRV(*av_fetch(chain, 0, 0));
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) return (HV *)SvRV(*av_fetch(chain, 0, 0)); 116 4 120
126  
127 { /* already merged for this class? */
128 4 SV *key = sv_2mortal(newSVsv(class_sv));
129 HE *he;
130 4 sv_catpvn(key, "\0merged", 7);
131 4 he = hv_fetch_ent(pm_registry(aTHX), key, 0, 0);
132 4 ▶ if (he) {
Branch: 1 branch
BranchTrueFalseTotal
if (he) { 0 4 4
133 0 SV *v = HeVAL(he);
134 0 ▶ if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) 0 0 0
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) 0 0 0
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) 0 0 0
135 0 return (HV *)SvRV(v);
136 }
137 }
138  
139 4 m = newHV();
140 4 fields = newAV();
141 4 field = newHV();
142 4 (void)hv_stores(m, "database", newSVpvs("default"));
143  
144 /* furthest ancestor first, so the nearest overwrites */
145 13 ▶ for (i = n - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i >= 0; i--) { 9 4 13
146 9 HV *src = (HV *)SvRV(*av_fetch(chain, i, 0));
147 9 SV *t = pm_get(aTHX_ src, "table");
148 9 SV *p = pm_get(aTHX_ src, "primary");
149 9 SV *v = pm_get(aTHX_ src, "validate");
150 9 SV *d = pm_get(aTHX_ src, "database");
151 9 SV *f = pm_get(aTHX_ src, "fields");
152 9 SV *h = pm_get(aTHX_ src, "field");
153  
154 9 ▶ if (t && SvOK(t)) (void)hv_stores(m, "table", newSVsv(t));
Branch: 2 branches
BranchTrueFalseTotal
if (t && SvOK(t)) (void)hv_stores(m, "table", newSVsv(t)); 9 0 9
if (t && SvOK(t)) (void)hv_stores(m, "table", newSVsv(t)); 5 4 9
155 9 ▶ if (p && SvOK(p)) (void)hv_stores(m, "primary", newSVsv(p));
Branch: 2 branches
BranchTrueFalseTotal
if (p && SvOK(p)) (void)hv_stores(m, "primary", newSVsv(p)); 9 0 9
if (p && SvOK(p)) (void)hv_stores(m, "primary", newSVsv(p)); 4 5 9
156 9 ▶ if (v && SvOK(v)) (void)hv_stores(m, "validate", newSVsv(v));
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) (void)hv_stores(m, "validate", newSVsv(v)); 9 0 9
if (v && SvOK(v)) (void)hv_stores(m, "validate", newSVsv(v)); 0 9 9
157 9 ▶ if (d && SvOK(d)) (void)hv_stores(m, "database", newSVsv(d));
Branch: 2 branches
BranchTrueFalseTotal
if (d && SvOK(d)) (void)hv_stores(m, "database", newSVsv(d)); 9 0 9
if (d && SvOK(d)) (void)hv_stores(m, "database", newSVsv(d)); 9 0 9
158  
159 9 ▶ if (h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV) { 9 0 9
if (h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV) { 9 0 9
if (h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV) { 9 0 9
160 9 HV *sh = (HV *)SvRV(h);
161 HE *he;
162 9 hv_iterinit(sh);
163 21 ▶ while ((he = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(sh))) { 12 9 21
164 STRLEN kl;
165 12 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 12 12
166 12 (void)hv_store(field, k, (I32)kl, newSVsv(HeVAL(he)), 0);
167 }
168 }
169 /* declaration order, each name once: a redeclared field keeps the
170 * position the parent gave it rather than moving to the end */
171 9 ▶ if (f && SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVAV) { 9 0 9
if (f && SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVAV) { 9 0 9
if (f && SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVAV) { 9 0 9
172 9 AV *sf = (AV *)SvRV(f);
173 9 SSize_t j, fn = av_len(sf) + 1;
174 21 ▶ for (j = 0; j < fn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < fn; j++) { 12 9 21
175 12 SV **e = av_fetch(sf, j, 0);
176 12 SSize_t k, kn = av_len(fields) + 1;
177 12 int seen = 0;
178 12 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 12 0 12
if (!(e && *e && SvOK(*e))) continue; 12 0 12
if (!(e && *e && SvOK(*e))) continue; 0 12 12
179 24 ▶ for (k = 0; k < kn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < kn; k++) { 13 11 24
180 13 SV **g = av_fetch(fields, k, 0);
181 13 ▶ if (g && *g && sv_eq(*g, *e)) { seen = 1; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (g && *g && sv_eq(*g, *e)) { seen = 1; break; } 13 0 13
if (g && *g && sv_eq(*g, *e)) { seen = 1; break; } 13 0 13
if (g && *g && sv_eq(*g, *e)) { seen = 1; break; } 1 12 13
182 }
183 12 ▶ if (!seen) av_push(fields, newSVsv(*e));
Branch: 1 branch
BranchTrueFalseTotal
if (!seen) av_push(fields, newSVsv(*e)); 11 1 12
184 }
185 }
186 }
187  
188 4 (void)hv_stores(m, "fields", newRV_noinc((SV *)fields));
189 4 (void)hv_stores(m, "field", newRV_noinc((SV *)field));
190  
191 {
192 4 SV *key = sv_2mortal(newSVsv(class_sv));
193 4 sv_catpvn(key, "\0merged", 7);
194 4 (void)hv_store_ent(pm_registry(aTHX), key,
195 newRV_noinc((SV *)m), 0);
196 }
197 4 return m;
198 }
199  
200 120 static HV *pm_meta_hv(pTHX_ SV *class_sv) {
201 120 return pm_meta_merged(aTHX_ class_sv);
202 }
203  
204  
205 /* ---- the declaration keywords -------------------------------------------- *
206 * Each is a magic CV carrying [ $meta_hashref ], installed into the model
207 * class by import. */
208  
209 XS_INTERNAL(pm_kw_table);
210 45 XS_INTERNAL(pm_kw_table) {
211 45 dXSARGS;
212 45 AV *cap = punk_clos_cap(aTHX_ cv);
213 HV *m;
214 45 ▶ if (!cap) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) XSRETURN_EMPTY; 0 45 45
215 45 m = (HV *)SvRV(*av_fetch(cap, 0, 0));
216 45 ▶ if (items > 0) (void)hv_stores(m, "table", newSVsv(ST(0)));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 0) (void)hv_stores(m, "table", newSVsv(ST(0))); 45 0 45
217 45 XSRETURN_EMPTY;
218 }
219  
220 /* Which configured database this model lives in (see the app's
221 * `database $name => \%opts`); the unnamed default when unset. */
222 XS_INTERNAL(pm_kw_database);
223 10 XS_INTERNAL(pm_kw_database) {
224 10 dXSARGS;
225 10 AV *cap = punk_clos_cap(aTHX_ cv);
226 HV *m;
227 10 ▶ if (!cap) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) XSRETURN_EMPTY; 0 10 10
228 10 m = (HV *)SvRV(*av_fetch(cap, 0, 0));
229 10 ▶ (void)hv_stores(m, "database",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(m, "database", 10 0 10
(void)hv_stores(m, "database", 10 0 10
230 (items > 0 && SvOK(ST(0))) ? newSVsv(ST(0)) : newSVpvs("default"));
231 10 XSRETURN_EMPTY;
232 }
233  
234 /* field $name => \%spec | field $name => (k => v, ...) */
235 XS_INTERNAL(pm_kw_field);
236 115 XS_INTERNAL(pm_kw_field) {
237 115 dXSARGS;
238 115 AV *cap = punk_clos_cap(aTHX_ cv);
239 HV *m, *spec, *fields;
240 AV *order;
241 SV *name;
242 int i;
243 115 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 115 0 115
if (!cap || items < 1) XSRETURN_EMPTY; 0 115 115
244 115 m = (HV *)SvRV(*av_fetch(cap, 0, 0));
245 115 name = ST(0);
246  
247 115 spec = newHV();
248 229 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 115 0 115
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 114 1 115
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 114 0 114
249 114 HV *src = (HV *)SvRV(ST(1));
250 HE *he;
251 114 hv_iterinit(src);
252 282 ▶ while ((he = hv_iternext(src)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) 168 114 282
253 168 ▶ (void)hv_store_ent(spec, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store_ent(spec, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); 168 0 168
(void)hv_store_ent(spec, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); 0 168 168
254 }
255 else {
256 1 ▶ for (i = 1; i + 1 < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) 0 1 1
257 0 (void)hv_store_ent(spec, ST(i), newSVsv(ST(i + 1)), 0);
258 }
259  
260 115 order = (AV *)SvRV(pm_get(aTHX_ m, "fields"));
261 115 fields = (HV *)SvRV(pm_get(aTHX_ m, "field"));
262 115 ▶ if (!hv_exists_ent(fields, name, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists_ent(fields, name, 0)) 115 0 115
263 115 av_push(order, newSVsv(name)); /* declaration order */
264 115 (void)hv_store_ent(fields, name, newRV_noinc((SV *)spec), 0);
265 {
266 115 SV *pk = pm_get(aTHX_ spec, "primary");
267 115 ▶ if (pk && SvTRUE(pk)) (void)hv_stores(m, "primary", newSVsv(name));
Branch: 2 branches
BranchTrueFalseTotal
if (pk && SvTRUE(pk)) (void)hv_stores(m, "primary", newSVsv(name)); 34 81 115
if (pk && SvTRUE(pk)) (void)hv_stores(m, "primary", newSVsv(name)); 34 0 34
268 }
269 115 XSRETURN_EMPTY;
270 }
271  
272 /* Opt in or out of create/update validation explicitly; the default is to
273 * validate when any field carries a constraint (see pm_meta_finalize). */
274 XS_INTERNAL(pm_kw_validate);
275 0 XS_INTERNAL(pm_kw_validate) {
276 0 dXSARGS;
277 0 AV *cap = punk_clos_cap(aTHX_ cv);
278 HV *m;
279 0 ▶ if (!cap) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) XSRETURN_EMPTY; 0 0 0
280 0 m = (HV *)SvRV(*av_fetch(cap, 0, 0));
281 0 ▶ (void)hv_stores(m, "validate",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(m, "validate", 0 0 0
(void)hv_stores(m, "validate", 0 0 0
282 newSViv(items > 0 ? (SvTRUE(ST(0)) ? 1 : 0) : 1));
283 0 XSRETURN_EMPTY;
284 }
285  
286 /* ---- metadata ------------------------------------------------------------ */
287  
288 /* Finalise once: default the primary key, decide whether to validate.
289 * Returns 0 when the class declared no table (not a usable model). */
290 120 static int pm_meta_finalize(pTHX_ HV *m) {
291 120 SV *table = pm_get(aTHX_ m, "table");
292 SV *fin;
293 120 ▶ if (!(table && SvOK(table))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(table && SvOK(table))) return 0; 120 0 120
if (!(table && SvOK(table))) return 0; 0 120 120
294 120 fin = pm_get(aTHX_ m, "finalized");
295 120 ▶ if (fin && SvTRUE(fin)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (fin && SvTRUE(fin)) return 1; 73 47 120
if (fin && SvTRUE(fin)) return 1; 73 0 73
296  
297 { /* primary defaults to `id` when a field of that name exists */
298 47 SV *pk = pm_get(aTHX_ m, "primary");
299 47 HV *fields = (HV *)SvRV(pm_get(aTHX_ m, "field"));
300 47 ▶ if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2))
Branch: 3 branches
BranchTrueFalseTotal
if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) 47 0 47
if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) 10 37 47
if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) 10 0 10
301 10 (void)hv_stores(m, "primary", newSVpvs("id"));
302 }
303 { /* validate by default when any field carries a constraint */
304 47 AV *order = (AV *)SvRV(pm_get(aTHX_ m, "fields"));
305 47 HV *fields = (HV *)SvRV(pm_get(aTHX_ m, "field"));
306 47 SV *want = pm_get(aTHX_ m, "validate");
307 47 int constrained = 0;
308 47 SSize_t i, n = av_len(order) + 1;
309 147 ▶ for (i = 0; i < n && !constrained; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && !constrained; i++) { 108 39 147
for (i = 0; i < n && !constrained; i++) { 108 0 108
310 108 SV **nm = av_fetch(order, i, 0);
311 108 ▶ HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL; 108 0 108
312 HV *spec;
313 int k;
314 108 ▶ if (!he || !SvROK(HeVAL(he))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!he || !SvROK(HeVAL(he))) continue; 108 0 108
if (!he || !SvROK(HeVAL(he))) continue; 0 108 108
315 108 spec = (HV *)SvRV(HeVAL(he));
316 {
317 108 SV *req = pm_get(aTHX_ spec, "required");
318 108 ▶ if (req && SvTRUE(req)) { constrained = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (req && SvTRUE(req)) { constrained = 1; break; } 15 93 108
if (req && SvTRUE(req)) { constrained = 1; break; } 8 7 15
319 }
320 /* every schema key but `type`: a bare type is not a constraint
321 * worth compiling a validator for */
322 1100 ▶ for (k = 1; PM_SCHEMA_KEYS[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; PM_SCHEMA_KEYS[k]; k++) { 1000 100 1100
323 1000 SV *v = pm_get(aTHX_ spec, PM_SCHEMA_KEYS[k]);
324 1000 ▶ if (v && SvOK(v)) { constrained = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { constrained = 1; break; } 0 1000 1000
if (v && SvOK(v)) { constrained = 1; break; } 0 0 0
325 }
326 }
327 47 ▶ (void)hv_stores(m, "should_validate",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(m, "should_validate", 43 4 47
(void)hv_stores(m, "should_validate", 0 43 43
328 newSViv((want && SvOK(want)) ? (SvTRUE(want) ? 1 : 0)
329 : (constrained ? 1 : 0)));
330 }
331 47 (void)hv_stores(m, "finalized", newSViv(1));
332 47 return 1;
333 }
334  
335 /* The JSON Schema for create (with required) or update (without). */
336 30 static SV *pm_object_schema(pTHX_ HV *m, int partial) {
337 30 AV *order = (AV *)SvRV(pm_get(aTHX_ m, "fields"));
338 30 HV *fields = (HV *)SvRV(pm_get(aTHX_ m, "field"));
339 30 HV *schema = newHV(), *props = newHV();
340 30 AV *required = newAV();
341 30 SSize_t i, n = av_len(order) + 1;
342  
343 140 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 110 30 140
344 110 SV **nm = av_fetch(order, i, 0);
345 110 ▶ HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL; 110 0 110
346 HV *spec, *p;
347 int k;
348 110 ▶ if (!he || !SvROK(HeVAL(he))) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!he || !SvROK(HeVAL(he))) continue; 110 0 110
if (!he || !SvROK(HeVAL(he))) continue; 0 110 110
349 110 spec = (HV *)SvRV(HeVAL(he));
350 110 p = newHV();
351 1320 ▶ for (k = 0; PM_SCHEMA_KEYS[k]; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; PM_SCHEMA_KEYS[k]; k++) { 1210 110 1320
352 1210 SV *v = pm_get(aTHX_ spec, PM_SCHEMA_KEYS[k]);
353 1210 ▶ if (v && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) 128 1082 1210
if (v && SvOK(v)) 128 0 128
354 128 (void)hv_store(p, PM_SCHEMA_KEYS[k],
355 (I32)strlen(PM_SCHEMA_KEYS[k]), newSVsv(v), 0);
356 }
357 110 (void)hv_store_ent(props, *nm, newRV_noinc((SV *)p), 0);
358 110 ▶ if (!partial) {
Branch: 1 branch
BranchTrueFalseTotal
if (!partial) { 55 55 110
359 55 SV *req = pm_get(aTHX_ spec, "required");
360 55 ▶ if (req && SvTRUE(req)) av_push(required, newSVsv(*nm));
Branch: 2 branches
BranchTrueFalseTotal
if (req && SvTRUE(req)) av_push(required, newSVsv(*nm)); 15 40 55
if (req && SvTRUE(req)) av_push(required, newSVsv(*nm)); 15 0 15
361 }
362 }
363 30 (void)hv_stores(schema, "type", newSVpvs("object"));
364 30 (void)hv_stores(schema, "properties", newRV_noinc((SV *)props));
365 30 ▶ if (av_len(required) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(required) >= 0) 15 15 30
366 15 (void)hv_stores(schema, "required", newRV_noinc((SV *)required));
367 else
368 15 SvREFCNT_dec((SV *)required);
369 30 return newRV_noinc((SV *)schema);
370 }
371  
372 /* ---- validation ----------------------------------------------------------- */
373  
374 /* Run a compiled validator; croak with the first error's location and
375 * message. A validation failure is the application's bug, so it stops the
376 * request the way any other croak does. */
377 34 static void pm_validate(pTHX_ SV *self, SV *cv_sv, SV *data) {
378 34 dSP;
379 34 int count, ok = 0;
380 34 SV *errors = NULL;
381 34 ENTER; SAVETMPS;
382 34 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 34 34
383 34 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 34 34
384 34 PUSHs(cv_sv);
385 34 PUSHs(data);
386 34 PUTBACK;
387 34 count = call_method("validate", G_ARRAY);
388 34 SPAGAIN;
389 /* Pop into locals before testing. SvTRUE is a macro that evaluates its
390 * argument more than once on perls before 5.30 - that is what SvTRUEx
391 * exists for - so SvTRUE(POPs) popped repeatedly, walked off the stack
392 * and handed sv_2bool garbage, segfaulting the model tier on 5.10 to
393 * 5.28. Modern perls make it an inline function, which is why it had
394 * never shown up. */
395 34 ▶ if (count >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 2) { 5 29 34
396 5 SV *e = POPs;
397 5 SV *o = POPs;
398 5 errors = e;
399 5 ok = SvTRUE(o);
400 }
401 29 ▶ else if (count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 1) { 29 0 29
402 29 SV *o = POPs;
403 29 ok = SvTRUE(o);
404 }
405 /* NOT sv_2mortal here. SAVETMPS saved the temps floor above, so the
406 * FREETMPS on the next line frees anything mortalised inside this
407 * scope - including this copy, which is then read below. A freed SV
408 * fails SvROK, the location and message are skipped, and EVERY
409 * validation failure reports the fallback "does not match the field
410 * schema" with no field named. Take a plain reference, leave the
411 * scope, and mortalise into the CALLER's temps where it survives long
412 * enough to be read. */
413 34 ▶ if (errors) errors = newSVsv(errors);
Branch: 1 branch
BranchTrueFalseTotal
if (errors) errors = newSVsv(errors); 5 29 34
414 34 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 34 0 34
415 34 ▶ if (errors) sv_2mortal(errors);
Branch: 1 branch
BranchTrueFalseTotal
if (errors) sv_2mortal(errors); 5 29 34
416 34 ▶ if (ok) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ok) return; 29 5 34
417 {
418 5 const char *cls = sv_reftype(SvRV(self), 1);
419 5 SV *at = NULL, *msg = NULL;
420 5 ▶ if (errors && SvROK(errors) && SvTYPE(SvRV(errors)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (errors && SvROK(errors) && SvTYPE(SvRV(errors)) == SVt_PVAV) { 5 0 5
if (errors && SvROK(errors) && SvTYPE(SvRV(errors)) == SVt_PVAV) { 5 0 5
if (errors && SvROK(errors) && SvTYPE(SvRV(errors)) == SVt_PVAV) { 5 0 5
421 5 AV *ea = (AV *)SvRV(errors);
422 5 SV **first = av_fetch(ea, 0, 0);
423 5 ▶ if (first && *first && SvROK(*first)
Branch: 3 branches
BranchTrueFalseTotal
if (first && *first && SvROK(*first) 5 0 5
if (first && *first && SvROK(*first) 5 0 5
if (first && *first && SvROK(*first) 5 0 5
424 5 ▶ && SvTYPE(SvRV(*first)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*first)) == SVt_PVHV) { 5 0 5
425 5 HV *e = (HV *)SvRV(*first);
426 5 at = pm_get(aTHX_ e, "instanceLocation");
427 5 msg = pm_get(aTHX_ e, "message");
428 }
429 }
430 12 ▶ croak("Punk::Model: %s validation failed%s%s: %s", cls,
Branch: 8 branches
BranchTrueFalseTotal
croak("Punk::Model: %s validation failed%s%s: %s", cls, 5 0 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 5 0 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 5 0 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 5 0 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 2 3 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 5 0 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 5 0 5
croak("Punk::Model: %s validation failed%s%s: %s", cls, 2 3 5
431 (at && SvOK(at) && SvCUR(at)) ? " at " : "",
432 (at && SvOK(at) && SvCUR(at)) ? SvPV_nolen(at) : "",
433 (msg && SvOK(msg)) ? SvPV_nolen(msg)
434 : "does not match the field schema");
435 }
436 }
437  
438 /* ---- the contract --------------------------------------------------------- */
439  
440 891 static SV *pm_slot(pTHX_ SV *self, const char *k) {
441 HV *hv;
442 891 ▶ if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 891 0 891
if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) 0 891 891
443 0 croak("Punk::Model: not a model instance");
444 891 hv = (HV *)SvRV(self);
445 891 return pm_get(aTHX_ hv, k);
446 }
447  
448 /* Forward @_ to the backend's method of the same name, returning whatever
449 * it returns. The results land above the caller's own arguments, so they
450 * are copied down over them before the caller returns - without that, list
451 * context handed back ($self, @args, @results), which scalar context
452 * masked by taking the last value. */
453 712 static void pm_delegate(pTHX_ SV **sp_base, I32 items, const char *method,
454 I32 gimme) {
455 712 dSP;
456 712 SV *self = sp_base[0];
457 712 SV *backend = pm_slot(aTHX_ self, "backend");
458 /* the callee can grow (and so move) the stack, so remember where the
459 * caller's frame starts as an offset, never as a pointer */
460 712 SSize_t base = sp_base - PL_stack_base;
461 I32 i;
462 712 ▶ if (!backend) croak("Punk::Model: this model has no backend");
Branch: 1 branch
BranchTrueFalseTotal
if (!backend) croak("Punk::Model: this model has no backend"); 0 712 712
463 712 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 712 712
464 712 ▶ EXTEND(SP, items);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, items); 712 0 712
EXTEND(SP, items); 0 712 712
465 712 PUSHs(backend);
466 1862 ▶ for (i = 1; i < items; i++) PUSHs(sp_base[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) PUSHs(sp_base[i]); 1150 712 1862
467 712 PUTBACK;
468 712 call_method(method, gimme);
469 637 SPAGAIN;
470 { /* results sit above the caller's own arguments; slide them down over
471 * them so list context returns @results, not ($self, @args,
472 * @results) - which scalar context masked by taking the last value.
473 * They are call_method's mortals, and dest stays below source, so an
474 * ascending alias copy is safe. */
475 637 SV **b = PL_stack_base + base;
476 637 SSize_t count = SP - (b + items - 1);
477 SSize_t j;
478 637 ▶ if (count < 0) count = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (count < 0) count = 0; 0 637 637
479 1270 ▶ for (j = 0; j < count; j++) b[j] = b[items + j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) b[j] = b[items + j]; 633 637 1270
480 637 SP = b + count - 1;
481 }
482 637 PUTBACK;
483 637 }
484  
485 #endif /* PUNK_MODEL_H */
« include/punk/punk_csrf.h ↑ Summary xs/oamount.xs »