| 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 |
498 |
| static HV *pm_registry(pTHX) { |
| 25 |
498 |
▶ | if (!PM_META) PM_META = newHV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PM_META) PM_META = newHV(); |
35 |
463 |
498 |
|
| 26 |
498 |
| 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 |
6208 |
| static SV *pm_get(pTHX_ HV *hv, const char *k) { |
| 38 |
6208 |
| SV **e = hv_fetch(hv, k, (I32)strlen(k), 0); |
| 39 |
6208 |
▶ | return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
2897 |
3311 |
6208 |
| return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
2897 |
0 |
2897 |
|
| 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 |
420 |
| static HV *pm_meta_own(pTHX_ SV *class_sv) { |
| 46 |
420 |
| HE *he = hv_fetch_ent(pm_registry(aTHX), class_sv, 0, 0); |
| 47 |
420 |
▶ | if (!he) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) return NULL; |
243 |
177 |
420 |
|
| 48 |
|
| { |
| 49 |
177 |
| SV *v = HeVAL(he); |
| 50 |
177 |
▶ | return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
177 |
0 |
177 |
| return (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
177 |
0 |
177 |
|
| 51 |
354 |
▶ | ? (HV *)SvRV(v) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(v) : NULL; |
177 |
0 |
177 |
|
| 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 |
|
| /* |
| 62 |
|
| # uncoverable statement |
| 63 |
|
| # uncoverable subroutine */ |
| 64 |
0 |
| static HV *pm_meta_isa(pTHX_ SV *class_sv, int depth) { |
| 65 |
0 |
| HV *own = pm_meta_own(aTHX_ class_sv); /* # uncoverable statement */ |
| 66 |
|
| AV *isa; |
| 67 |
|
| SSize_t i, n; |
| 68 |
|
| |
| 69 |
|
| /* |
| 70 |
|
| # uncoverable statement |
| 71 |
|
| # uncoverable branch true |
| 72 |
|
| # uncoverable branch false |
| 73 |
|
| # uncoverable condition left count:1 |
| 74 |
|
| # uncoverable condition right count:1 */ |
| 75 |
0 |
▶ | if (own) return own; |
Branch: 1 branch
| Branch | True | False | Total |
| if (own) return own; |
0 |
0 |
0 |
|
| 76 |
|
| /* |
| 77 |
|
| # uncoverable statement |
| 78 |
|
| # uncoverable branch true |
| 79 |
|
| # uncoverable branch false |
| 80 |
|
| # uncoverable condition left count:1 |
| 81 |
|
| # uncoverable condition right count:1 */ |
| 82 |
0 |
▶ | if (depth >= 16) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth >= 16) return NULL; |
0 |
0 |
0 |
|
| 83 |
|
| |
| 84 |
0 |
| isa = get_av(form("%s::ISA", SvPV_nolen(class_sv)), 0); /* # uncoverable statement */ |
| 85 |
|
| /* |
| 86 |
|
| # uncoverable statement |
| 87 |
|
| # uncoverable branch true |
| 88 |
|
| # uncoverable branch false |
| 89 |
|
| # uncoverable condition left count:1 |
| 90 |
|
| # uncoverable condition right count:1 */ |
| 91 |
0 |
▶ | if (!isa) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!isa) return NULL; |
0 |
0 |
0 |
|
| 92 |
|
| |
| 93 |
0 |
| n = av_len(isa) + 1; /* # uncoverable statement */ |
| 94 |
|
| /* |
| 95 |
|
| # uncoverable statement |
| 96 |
|
| # uncoverable branch true |
| 97 |
|
| # uncoverable branch false |
| 98 |
|
| # uncoverable condition left count:1 |
| 99 |
|
| # uncoverable condition right count:1 */ |
| 100 |
0 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
0 |
0 |
|
| 101 |
0 |
| SV **e = av_fetch(isa, i, 0); /* # uncoverable statement */ |
| 102 |
|
| HV *m; |
| 103 |
|
| /* |
| 104 |
|
| # uncoverable statement |
| 105 |
|
| # uncoverable branch true |
| 106 |
|
| # uncoverable branch false |
| 107 |
|
| # uncoverable condition left count:1 |
| 108 |
|
| # uncoverable condition right count:1 |
| 109 |
|
| # uncoverable condition left count:2 |
| 110 |
|
| # uncoverable condition right count:2 |
| 111 |
|
| # uncoverable condition left count:3 |
| 112 |
|
| # uncoverable condition right count:3 */ |
| 113 |
0 |
▶ | if (!(e && *e && SvOK(*e))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 114 |
0 |
| m = pm_meta_isa(aTHX_ *e, depth + 1); /* # uncoverable statement */ |
| 115 |
|
| /* |
| 116 |
|
| # uncoverable statement |
| 117 |
|
| # uncoverable branch true |
| 118 |
|
| # uncoverable branch false |
| 119 |
|
| # uncoverable condition left count:1 |
| 120 |
|
| # uncoverable condition right count:1 */ |
| 121 |
0 |
▶ | if (m) return m; |
Branch: 1 branch
| Branch | True | False | Total |
| if (m) return m; |
0 |
0 |
0 |
|
| 122 |
|
| } |
| 123 |
0 |
| return NULL; /* # uncoverable statement */ |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| /* The class and its ancestors that declared metadata, nearest first. */ |
| 127 |
350 |
| static void pm_meta_chain(pTHX_ SV *class_sv, AV *out, int depth) { |
| 128 |
|
| HV *own; |
| 129 |
|
| AV *isa; |
| 130 |
|
| SSize_t i, n; |
| 131 |
|
| |
| 132 |
|
| /* |
| 133 |
|
| # uncoverable branch true |
| 134 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 135 |
350 |
▶ | if (depth >= 16) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth >= 16) return; |
0 |
350 |
350 |
|
| 136 |
350 |
| own = pm_meta_own(aTHX_ class_sv); |
| 137 |
350 |
▶ | if (own) av_push(out, newRV_inc((SV *)own)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (own) av_push(out, newRV_inc((SV *)own)); |
177 |
173 |
350 |
|
| 138 |
|
| |
| 139 |
350 |
| isa = get_av(form("%s::ISA", SvPV_nolen(class_sv)), 0); |
| 140 |
350 |
▶ | if (!isa) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!isa) return; |
172 |
178 |
350 |
|
| 141 |
178 |
| n = av_len(isa) + 1; |
| 142 |
356 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
178 |
178 |
356 |
|
| 143 |
178 |
| SV **e = av_fetch(isa, i, 0); |
| 144 |
|
| /* |
| 145 |
|
| # uncoverable branch false |
| 146 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 147 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 148 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 149 |
178 |
▶ | if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); |
178 |
0 |
178 |
| if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); |
178 |
0 |
178 |
| if (e && *e && SvOK(*e)) pm_meta_chain(aTHX_ *e, out, depth + 1); |
178 |
0 |
178 |
|
| 150 |
|
| } |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
|
| /* The metadata a class reads, with its ancestors' merged in. |
| 154 |
|
| * |
| 155 |
|
| * This is done at LOOKUP and not at `use Punk::Model`, because `table` and |
| 156 |
|
| * `field` are statements in the package body: they run when the class is |
| 157 |
|
| * loaded, long after import decided what the class knew. A subclass seeded at |
| 158 |
|
| * import time would copy an empty parent. |
| 159 |
|
| * |
| 160 |
|
| * Nearest wins: a subclass may add fields, or redeclare one to change its |
| 161 |
|
| * spec, and names it does not mention keep the parent's. The merged hash is |
| 162 |
|
| * cached under its own registry key, so the merge happens once per class and |
| 163 |
|
| * the parent's own entry is never disturbed. |
| 164 |
|
| */ |
| 165 |
172 |
| static HV *pm_meta_merged(pTHX_ SV *class_sv) { |
| 166 |
172 |
| AV *chain = (AV *)sv_2mortal((SV *)newAV()); |
| 167 |
|
| HV *m; |
| 168 |
|
| AV *fields; |
| 169 |
|
| HV *field; |
| 170 |
|
| SSize_t i, n; |
| 171 |
|
| |
| 172 |
172 |
| pm_meta_chain(aTHX_ class_sv, chain, 0); |
| 173 |
172 |
| n = av_len(chain) + 1; |
| 174 |
|
| /* |
| 175 |
|
| # uncoverable branch true |
| 176 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 177 |
172 |
▶ | if (n == 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return NULL; |
0 |
172 |
172 |
|
| 178 |
|
| /* the common case: nothing inherited, so nothing to merge */ |
| 179 |
172 |
▶ | if (n == 1) return (HV *)SvRV(*av_fetch(chain, 0, 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 1) return (HV *)SvRV(*av_fetch(chain, 0, 0)); |
168 |
4 |
172 |
|
| 180 |
|
| |
| 181 |
|
| { /* already merged for this class? */ |
| 182 |
4 |
| SV *key = sv_2mortal(newSVsv(class_sv)); |
| 183 |
|
| HE *he; |
| 184 |
4 |
| sv_catpvn(key, "\0merged", 7); |
| 185 |
4 |
| he = hv_fetch_ent(pm_registry(aTHX), key, 0, 0); |
| 186 |
|
| /* |
| 187 |
|
| # uncoverable branch true |
| 188 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 189 |
4 |
▶ | if (he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (he) { |
0 |
4 |
4 |
|
| 190 |
0 |
| SV *v = HeVAL(he); /* # uncoverable statement */ |
| 191 |
|
| /* |
| 192 |
|
| # uncoverable statement |
| 193 |
|
| # uncoverable branch true |
| 194 |
|
| # uncoverable branch false |
| 195 |
|
| # uncoverable condition left count:1 |
| 196 |
|
| # uncoverable condition right count:1 |
| 197 |
|
| # uncoverable condition left count:2 |
| 198 |
|
| # uncoverable condition right count:2 |
| 199 |
|
| # uncoverable condition left count:3 |
| 200 |
|
| # uncoverable condition right count:3 */ |
| 201 |
0 |
▶ | if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 202 |
0 |
| return (HV *)SvRV(v); /* # uncoverable statement */ |
| 203 |
|
| } |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
4 |
| m = newHV(); |
| 207 |
4 |
| fields = newAV(); |
| 208 |
4 |
| field = newHV(); |
| 209 |
4 |
| (void)hv_stores(m, "database", newSVpvs("default")); |
| 210 |
|
| |
| 211 |
|
| /* furthest ancestor first, so the nearest overwrites */ |
| 212 |
13 |
▶ | for (i = n - 1; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = n - 1; i >= 0; i--) { |
9 |
4 |
13 |
|
| 213 |
9 |
| HV *src = (HV *)SvRV(*av_fetch(chain, i, 0)); |
| 214 |
9 |
| SV *t = pm_get(aTHX_ src, "table"); |
| 215 |
9 |
| SV *p = pm_get(aTHX_ src, "primary"); |
| 216 |
9 |
| SV *v = pm_get(aTHX_ src, "validate"); |
| 217 |
9 |
| SV *d = pm_get(aTHX_ src, "database"); |
| 218 |
9 |
| SV *f = pm_get(aTHX_ src, "fields"); |
| 219 |
9 |
| SV *h = pm_get(aTHX_ src, "field"); |
| 220 |
|
| |
| 221 |
9 |
▶ | if (t && SvOK(t)) (void)hv_stores(m, "table", newSVsv(t)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (t && SvOK(t)) (void)hv_stores(m, "table", newSVsv(t)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
9 |
0 |
9 |
| if (t && SvOK(t)) (void)hv_stores(m, "table", newSVsv(t)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
5 |
4 |
9 |
|
| 222 |
9 |
▶ | if (p && SvOK(p)) (void)hv_stores(m, "primary", newSVsv(p)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (p && SvOK(p)) (void)hv_stores(m, "primary", newSVsv(p)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
9 |
0 |
9 |
| if (p && SvOK(p)) (void)hv_stores(m, "primary", newSVsv(p)); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
4 |
5 |
9 |
|
| 223 |
|
| /* |
| 224 |
|
| # uncoverable branch true |
| 225 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 226 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 227 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 228 |
9 |
▶ | if (v && SvOK(v)) (void)hv_stores(m, "validate", newSVsv(v)); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 229 |
|
| /* |
| 230 |
|
| # uncoverable branch false |
| 231 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 232 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 233 |
9 |
▶ | if (d && SvOK(d)) (void)hv_stores(m, "database", newSVsv(d)); |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 234 |
|
| |
| 235 |
|
| /* |
| 236 |
|
| # uncoverable branch false |
| 237 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 238 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 239 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 240 |
9 |
▶ | if (h && SvROK(h) && SvTYPE(SvRV(h)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 241 |
9 |
| HV *sh = (HV *)SvRV(h); |
| 242 |
|
| HE *he; |
| 243 |
9 |
| hv_iterinit(sh); |
| 244 |
21 |
▶ | while ((he = hv_iternext(sh))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(sh))) { |
12 |
9 |
21 |
|
| 245 |
|
| STRLEN kl; |
| 246 |
12 |
▶ | const char *k = HePV(he, kl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *k = HePV(he, kl); |
0 |
12 |
12 |
|
| 247 |
12 |
| (void)hv_store(field, k, (I32)kl, newSVsv(HeVAL(he)), 0); |
| 248 |
|
| } |
| 249 |
|
| } |
| 250 |
|
| /* declaration order, each name once: a redeclared field keeps the |
| 251 |
|
| * position the parent gave it rather than moving to the end */ |
| 252 |
|
| /* |
| 253 |
|
| # uncoverable branch false |
| 254 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 255 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 256 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 257 |
9 |
▶ | if (f && SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 258 |
9 |
| AV *sf = (AV *)SvRV(f); |
| 259 |
9 |
| SSize_t j, fn = av_len(sf) + 1; |
| 260 |
21 |
▶ | for (j = 0; j < fn; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < fn; j++) { |
12 |
9 |
21 |
|
| 261 |
12 |
| SV **e = av_fetch(sf, j, 0); |
| 262 |
12 |
| SSize_t k, kn = av_len(fields) + 1; |
| 263 |
12 |
| int seen = 0; |
| 264 |
|
| /* |
| 265 |
|
| # uncoverable branch true |
| 266 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 267 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 268 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 269 |
12 |
▶ | if (!(e && *e && SvOK(*e))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 270 |
24 |
▶ | for (k = 0; k < kn; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < kn; k++) { |
13 |
11 |
24 |
|
| 271 |
13 |
| SV **g = av_fetch(fields, k, 0); |
| 272 |
|
| /* |
| 273 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 274 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 275 |
13 |
▶ | if (g && *g && sv_eq(*g, *e)) { seen = 1; break; } |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 276 |
|
| } |
| 277 |
12 |
▶ | if (!seen) av_push(fields, newSVsv(*e)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!seen) av_push(fields, newSVsv(*e)); |
11 |
1 |
12 |
|
| 278 |
|
| } |
| 279 |
|
| } |
| 280 |
|
| } |
| 281 |
|
| |
| 282 |
4 |
| (void)hv_stores(m, "fields", newRV_noinc((SV *)fields)); |
| 283 |
4 |
| (void)hv_stores(m, "field", newRV_noinc((SV *)field)); |
| 284 |
|
| |
| 285 |
|
| { |
| 286 |
4 |
| SV *key = sv_2mortal(newSVsv(class_sv)); |
| 287 |
4 |
| sv_catpvn(key, "\0merged", 7); |
| 288 |
4 |
| (void)hv_store_ent(pm_registry(aTHX), key, |
| 289 |
|
| newRV_noinc((SV *)m), 0); |
| 290 |
|
| } |
| 291 |
4 |
| return m; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
172 |
| static HV *pm_meta_hv(pTHX_ SV *class_sv) { |
| 295 |
172 |
| return pm_meta_merged(aTHX_ class_sv); |
| 296 |
|
| } |
| 297 |
|
| |
| 298 |
|
| |
| 299 |
|
| /* ---- the declaration keywords -------------------------------------------- * |
| 300 |
|
| * Each is a magic CV carrying [ $meta_hashref ], installed into the model |
| 301 |
|
| * class by import. */ |
| 302 |
|
| |
| 303 |
|
| XS_INTERNAL(pm_kw_table); |
| 304 |
63 |
| XS_INTERNAL(pm_kw_table) { |
| 305 |
63 |
| dXSARGS; |
| 306 |
63 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 307 |
|
| HV *m; |
| 308 |
|
| /* |
| 309 |
|
| # uncoverable branch true |
| 310 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 311 |
63 |
▶ | if (!cap) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) XSRETURN_EMPTY; |
0 |
63 |
63 |
|
| 312 |
63 |
| m = (HV *)SvRV(*av_fetch(cap, 0, 0)); |
| 313 |
|
| /* |
| 314 |
|
| # uncoverable branch false |
| 315 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 316 |
63 |
▶ | if (items > 0) (void)hv_stores(m, "table", newSVsv(ST(0))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 0) (void)hv_stores(m, "table", newSVsv(ST(0))); |
63 |
0 |
63 |
|
| 317 |
63 |
| XSRETURN_EMPTY; |
| 318 |
|
| } |
| 319 |
|
| |
| 320 |
|
| /* Which configured database this model lives in (see the app's |
| 321 |
|
| * `database $name => \%opts`); the unnamed default when unset. */ |
| 322 |
|
| XS_INTERNAL(pm_kw_database); |
| 323 |
19 |
| XS_INTERNAL(pm_kw_database) { |
| 324 |
19 |
| dXSARGS; |
| 325 |
19 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 326 |
|
| HV *m; |
| 327 |
|
| /* |
| 328 |
|
| # uncoverable branch true |
| 329 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 330 |
19 |
▶ | if (!cap) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) XSRETURN_EMPTY; |
0 |
19 |
19 |
|
| 331 |
19 |
| m = (HV *)SvRV(*av_fetch(cap, 0, 0)); |
| 332 |
19 |
▶ | (void)hv_stores(m, "database", |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(m, "database", |
19 |
0 |
19 |
| (void)hv_stores(m, "database", |
19 |
0 |
19 |
|
| 333 |
|
| (items > 0 && SvOK(ST(0))) ? newSVsv(ST(0)) : newSVpvs("default")); |
| 334 |
19 |
| XSRETURN_EMPTY; |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| /* field $name => \%spec | field $name => (k => v, ...) */ |
| 338 |
|
| XS_INTERNAL(pm_kw_field); |
| 339 |
164 |
| XS_INTERNAL(pm_kw_field) { |
| 340 |
164 |
| dXSARGS; |
| 341 |
164 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 342 |
|
| HV *m, *spec, *fields; |
| 343 |
|
| AV *order; |
| 344 |
|
| SV *name; |
| 345 |
|
| int i; |
| 346 |
|
| /* |
| 347 |
|
| # uncoverable branch true |
| 348 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 349 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 350 |
164 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
164 |
0 |
164 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
164 |
164 |
|
| 351 |
164 |
| m = (HV *)SvRV(*av_fetch(cap, 0, 0)); |
| 352 |
164 |
| name = ST(0); |
| 353 |
|
| |
| 354 |
164 |
| spec = newHV(); |
| 355 |
|
| /* |
| 356 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 357 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 358 |
327 |
▶ | if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
164 |
0 |
164 |
| if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
163 |
1 |
164 |
| if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
163 |
0 |
163 |
|
| 359 |
163 |
| HV *src = (HV *)SvRV(ST(1)); |
| 360 |
|
| HE *he; |
| 361 |
163 |
| hv_iterinit(src); |
| 362 |
402 |
▶ | while ((he = hv_iternext(src))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) |
239 |
163 |
402 |
|
| 363 |
239 |
▶ | (void)hv_store_ent(spec, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_store_ent(spec, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
239 |
0 |
239 |
| (void)hv_store_ent(spec, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
0 |
239 |
239 |
|
| 364 |
|
| } |
| 365 |
|
| else { |
| 366 |
|
| /* |
| 367 |
|
| # uncoverable branch true |
| 368 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 369 |
1 |
▶ | for (i = 1; i + 1 < items; i += 2) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) |
0 |
1 |
1 |
|
| 370 |
0 |
| (void)hv_store_ent(spec, ST(i), newSVsv(ST(i + 1)), 0); /* # uncoverable statement */ |
| 371 |
|
| } |
| 372 |
|
| |
| 373 |
164 |
| order = (AV *)SvRV(pm_get(aTHX_ m, "fields")); |
| 374 |
164 |
| fields = (HV *)SvRV(pm_get(aTHX_ m, "field")); |
| 375 |
|
| /* |
| 376 |
|
| # uncoverable branch false |
| 377 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 378 |
164 |
▶ | if (!hv_exists_ent(fields, name, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_exists_ent(fields, name, 0)) |
164 |
0 |
164 |
|
| 379 |
164 |
| av_push(order, newSVsv(name)); /* declaration order */ |
| 380 |
164 |
| (void)hv_store_ent(fields, name, newRV_noinc((SV *)spec), 0); |
| 381 |
|
| { |
| 382 |
164 |
| SV *pk = pm_get(aTHX_ spec, "primary"); |
| 383 |
164 |
▶ | if (pk && SvTRUE(pk)) (void)hv_stores(m, "primary", newSVsv(name)); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (pk && SvTRUE(pk)) (void)hv_stores(m, "primary", newSVsv(name)); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
48 |
116 |
164 |
| if (pk && SvTRUE(pk)) (void)hv_stores(m, "primary", newSVsv(name)); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
48 |
0 |
48 |
|
| 384 |
|
| } |
| 385 |
164 |
| XSRETURN_EMPTY; |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
|
| /* Opt in or out of create/update validation explicitly; the default is to |
| 389 |
|
| * validate when any field carries a constraint (see pm_meta_finalize). */ |
| 390 |
|
| XS_INTERNAL(pm_kw_validate); |
| 391 |
2 |
| XS_INTERNAL(pm_kw_validate) { |
| 392 |
2 |
| dXSARGS; |
| 393 |
2 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 394 |
|
| HV *m; |
| 395 |
|
| /* |
| 396 |
|
| # uncoverable branch true |
| 397 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 398 |
2 |
▶ | if (!cap) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) XSRETURN_EMPTY; |
0 |
2 |
2 |
|
| 399 |
2 |
| m = (HV *)SvRV(*av_fetch(cap, 0, 0)); |
| 400 |
2 |
▶ | (void)hv_stores(m, "validate", |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(m, "validate", |
2 |
0 |
2 |
| (void)hv_stores(m, "validate", |
0 |
2 |
2 |
|
| 401 |
|
| newSViv(items > 0 ? (SvTRUE(ST(0)) ? 1 : 0) : 1)); |
| 402 |
2 |
| XSRETURN_EMPTY; |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
|
| /* ---- metadata ------------------------------------------------------------ */ |
| 406 |
|
| |
| 407 |
|
| /* Finalise once: default the primary key, decide whether to validate. |
| 408 |
|
| * Returns 0 when the class declared no table (not a usable model). */ |
| 409 |
172 |
| static int pm_meta_finalize(pTHX_ HV *m) { |
| 410 |
172 |
| SV *table = pm_get(aTHX_ m, "table"); |
| 411 |
|
| SV *fin; |
| 412 |
172 |
▶ | if (!(table && SvOK(table))) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(table && SvOK(table))) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
172 |
0 |
172 |
| if (!(table && SvOK(table))) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
1 |
171 |
172 |
|
| 413 |
171 |
| fin = pm_get(aTHX_ m, "finalized"); |
| 414 |
171 |
▶ | if (fin && SvTRUE(fin)) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (fin && SvTRUE(fin)) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
106 |
65 |
171 |
| if (fin && SvTRUE(fin)) return 1; /* # uncoverable condition right count:2 class:ignore_covered_err */ |
106 |
0 |
106 |
|
| 415 |
|
| |
| 416 |
|
| { /* primary defaults to `id` when a field of that name exists */ |
| 417 |
65 |
| SV *pk = pm_get(aTHX_ m, "primary"); |
| 418 |
65 |
| HV *fields = (HV *)SvRV(pm_get(aTHX_ m, "field")); |
| 419 |
65 |
▶ | if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
65 |
0 |
65 |
| if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
14 |
51 |
65 |
| if (!(pk && SvOK(pk)) && hv_exists(fields, "id", 2)) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
12 |
2 |
14 |
|
| 420 |
12 |
| (void)hv_stores(m, "primary", newSVpvs("id")); |
| 421 |
|
| } |
| 422 |
|
| { /* validate by default when any field carries a constraint */ |
| 423 |
65 |
| AV *order = (AV *)SvRV(pm_get(aTHX_ m, "fields")); |
| 424 |
65 |
| HV *fields = (HV *)SvRV(pm_get(aTHX_ m, "field")); |
| 425 |
65 |
| SV *want = pm_get(aTHX_ m, "validate"); |
| 426 |
65 |
| int constrained = 0; |
| 427 |
65 |
| SSize_t i, n = av_len(order) + 1; |
| 428 |
210 |
▶ | for (i = 0; i < n && !constrained; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < n && !constrained; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
156 |
54 |
210 |
| for (i = 0; i < n && !constrained; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
156 |
0 |
156 |
|
| 429 |
156 |
| SV **nm = av_fetch(order, i, 0); |
| 430 |
156 |
▶ | HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
156 |
0 |
156 |
|
| 431 |
|
| HV *spec; |
| 432 |
|
| int k; |
| 433 |
|
| /* |
| 434 |
|
| # uncoverable branch true |
| 435 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 436 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 437 |
156 |
▶ | if (!he || !SvROK(HeVAL(he))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!he || !SvROK(HeVAL(he))) continue; |
156 |
0 |
156 |
| if (!he || !SvROK(HeVAL(he))) continue; |
0 |
156 |
156 |
|
| 438 |
156 |
| spec = (HV *)SvRV(HeVAL(he)); |
| 439 |
|
| { |
| 440 |
156 |
| SV *req = pm_get(aTHX_ spec, "required"); |
| 441 |
156 |
▶ | if (req && SvTRUE(req)) { constrained = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (req && SvTRUE(req)) { constrained = 1; break; } |
23 |
133 |
156 |
| if (req && SvTRUE(req)) { constrained = 1; break; } |
11 |
12 |
23 |
|
| 442 |
|
| } |
| 443 |
|
| /* every schema key but `type`: a bare type is not a constraint |
| 444 |
|
| * worth compiling a validator for */ |
| 445 |
1595 |
▶ | for (k = 1; PM_SCHEMA_KEYS[k]; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 1; PM_SCHEMA_KEYS[k]; k++) { |
1450 |
145 |
1595 |
|
| 446 |
1450 |
| SV *v = pm_get(aTHX_ spec, PM_SCHEMA_KEYS[k]); |
| 447 |
|
| /* |
| 448 |
|
| # uncoverable branch true |
| 449 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 450 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 451 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 452 |
1450 |
▶ | if (v && SvOK(v)) { constrained = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (v && SvOK(v)) { constrained = 1; break; } |
0 |
1450 |
1450 |
| if (v && SvOK(v)) { constrained = 1; break; } |
0 |
0 |
0 |
|
| 453 |
|
| } |
| 454 |
|
| } |
| 455 |
65 |
▶ | (void)hv_stores(m, "should_validate", |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_stores(m, "should_validate", |
61 |
4 |
65 |
| (void)hv_stores(m, "should_validate", |
2 |
59 |
61 |
|
| 456 |
|
| newSViv((want && SvOK(want)) ? (SvTRUE(want) ? 1 : 0) |
| 457 |
|
| : (constrained ? 1 : 0))); |
| 458 |
|
| } |
| 459 |
65 |
| (void)hv_stores(m, "finalized", newSViv(1)); |
| 460 |
65 |
| return 1; |
| 461 |
|
| } |
| 462 |
|
| |
| 463 |
|
| /* The JSON Schema for create (with required) or update (without). */ |
| 464 |
38 |
| static SV *pm_object_schema(pTHX_ HV *m, int partial) { |
| 465 |
38 |
| AV *order = (AV *)SvRV(pm_get(aTHX_ m, "fields")); |
| 466 |
38 |
| HV *fields = (HV *)SvRV(pm_get(aTHX_ m, "field")); |
| 467 |
38 |
| HV *schema = newHV(), *props = newHV(); |
| 468 |
38 |
| AV *required = newAV(); |
| 469 |
38 |
| SSize_t i, n = av_len(order) + 1; |
| 470 |
|
| |
| 471 |
166 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
128 |
38 |
166 |
|
| 472 |
128 |
| SV **nm = av_fetch(order, i, 0); |
| 473 |
128 |
▶ | HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| HE *he = nm ? hv_fetch_ent(fields, *nm, 0, 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
128 |
0 |
128 |
|
| 474 |
|
| HV *spec, *p; |
| 475 |
|
| int k; |
| 476 |
|
| /* |
| 477 |
|
| # uncoverable branch true |
| 478 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 479 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 480 |
128 |
▶ | if (!he || !SvROK(HeVAL(he))) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!he || !SvROK(HeVAL(he))) continue; |
128 |
0 |
128 |
| if (!he || !SvROK(HeVAL(he))) continue; |
0 |
128 |
128 |
|
| 481 |
128 |
| spec = (HV *)SvRV(HeVAL(he)); |
| 482 |
128 |
| p = newHV(); |
| 483 |
1536 |
▶ | for (k = 0; PM_SCHEMA_KEYS[k]; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; PM_SCHEMA_KEYS[k]; k++) { |
1408 |
128 |
1536 |
|
| 484 |
1408 |
| SV *v = pm_get(aTHX_ spec, PM_SCHEMA_KEYS[k]); |
| 485 |
1408 |
▶ | if (v && SvOK(v)) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (v && SvOK(v)) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
146 |
1262 |
1408 |
| if (v && SvOK(v)) /* # uncoverable condition right count:2 class:ignore_covered_err */ |
146 |
0 |
146 |
|
| 486 |
146 |
| (void)hv_store(p, PM_SCHEMA_KEYS[k], |
| 487 |
|
| (I32)strlen(PM_SCHEMA_KEYS[k]), newSVsv(v), 0); |
| 488 |
|
| } |
| 489 |
128 |
| (void)hv_store_ent(props, *nm, newRV_noinc((SV *)p), 0); |
| 490 |
128 |
▶ | if (!partial) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!partial) { |
64 |
64 |
128 |
|
| 491 |
64 |
| SV *req = pm_get(aTHX_ spec, "required"); |
| 492 |
64 |
▶ | if (req && SvTRUE(req)) av_push(required, newSVsv(*nm)); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (req && SvTRUE(req)) av_push(required, newSVsv(*nm)); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
19 |
45 |
64 |
| if (req && SvTRUE(req)) av_push(required, newSVsv(*nm)); /* # uncoverable condition right count:2 class:ignore_covered_err */ |
19 |
0 |
19 |
|
| 493 |
|
| } |
| 494 |
|
| } |
| 495 |
38 |
| (void)hv_stores(schema, "type", newSVpvs("object")); |
| 496 |
38 |
| (void)hv_stores(schema, "properties", newRV_noinc((SV *)props)); |
| 497 |
38 |
▶ | if (av_len(required) >= 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(required) >= 0) |
19 |
19 |
38 |
|
| 498 |
19 |
| (void)hv_stores(schema, "required", newRV_noinc((SV *)required)); |
| 499 |
|
| else |
| 500 |
19 |
| SvREFCNT_dec((SV *)required); |
| 501 |
38 |
| return newRV_noinc((SV *)schema); |
| 502 |
|
| } |
| 503 |
|
| |
| 504 |
|
| /* ---- validation ----------------------------------------------------------- */ |
| 505 |
|
| |
| 506 |
|
| /* Run a compiled validator; croak with the first error's location and |
| 507 |
|
| * message. A validation failure is the application's bug, so it stops the |
| 508 |
|
| * request the way any other croak does. */ |
| 509 |
41 |
| static void pm_validate(pTHX_ SV *self, SV *cv_sv, SV *data) { |
| 510 |
41 |
| dSP; |
| 511 |
41 |
| int count, ok = 0; |
| 512 |
41 |
| SV *errors = NULL; |
| 513 |
41 |
| ENTER; SAVETMPS; |
| 514 |
41 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
41 |
41 |
|
| 515 |
41 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
41 |
41 |
|
| 516 |
41 |
| PUSHs(cv_sv); |
| 517 |
41 |
| PUSHs(data); |
| 518 |
41 |
| PUTBACK; |
| 519 |
41 |
| count = call_method("validate", G_ARRAY); |
| 520 |
41 |
| SPAGAIN; |
| 521 |
|
| /* Pop into locals before testing. SvTRUE is a macro that evaluates its |
| 522 |
|
| * argument more than once on perls before 5.30 - that is what SvTRUEx |
| 523 |
|
| * exists for - so SvTRUE(POPs) popped repeatedly, walked off the stack |
| 524 |
|
| * and handed sv_2bool garbage, segfaulting the model tier on 5.10 to |
| 525 |
|
| * 5.28. Modern perls make it an inline function, which is why it had |
| 526 |
|
| * never shown up. */ |
| 527 |
41 |
▶ | if (count >= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count >= 2) { |
5 |
36 |
41 |
|
| 528 |
5 |
| SV *e = POPs; |
| 529 |
5 |
| SV *o = POPs; |
| 530 |
5 |
| errors = e; |
| 531 |
5 |
| ok = SvTRUE(o); |
| 532 |
|
| } |
| 533 |
|
| /* |
| 534 |
|
| # uncoverable branch false |
| 535 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 536 |
36 |
▶ | else if (count == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count == 1) { |
36 |
0 |
36 |
|
| 537 |
36 |
| SV *o = POPs; |
| 538 |
36 |
| ok = SvTRUE(o); |
| 539 |
|
| } |
| 540 |
|
| /* NOT sv_2mortal here. SAVETMPS saved the temps floor above, so the |
| 541 |
|
| * FREETMPS on the next line frees anything mortalised inside this |
| 542 |
|
| * scope - including this copy, which is then read below. A freed SV |
| 543 |
|
| * fails SvROK, the location and message are skipped, and EVERY |
| 544 |
|
| * validation failure reports the fallback "does not match the field |
| 545 |
|
| * schema" with no field named. Take a plain reference, leave the |
| 546 |
|
| * scope, and mortalise into the CALLER's temps where it survives long |
| 547 |
|
| * enough to be read. */ |
| 548 |
41 |
▶ | if (errors) errors = newSVsv(errors); |
Branch: 1 branch
| Branch | True | False | Total |
| if (errors) errors = newSVsv(errors); |
5 |
36 |
41 |
|
| 549 |
41 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
41 |
0 |
41 |
|
| 550 |
41 |
▶ | if (errors) sv_2mortal(errors); |
Branch: 1 branch
| Branch | True | False | Total |
| if (errors) sv_2mortal(errors); |
5 |
36 |
41 |
|
| 551 |
41 |
▶ | if (ok) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) return; |
36 |
5 |
41 |
|
| 552 |
|
| { |
| 553 |
5 |
| const char *cls = sv_reftype(SvRV(self), 1); |
| 554 |
5 |
| SV *at = NULL, *msg = NULL; |
| 555 |
|
| /* |
| 556 |
|
| # uncoverable branch false |
| 557 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 558 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 559 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 560 |
5 |
▶ | if (errors && SvROK(errors) && SvTYPE(SvRV(errors)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 561 |
5 |
| AV *ea = (AV *)SvRV(errors); |
| 562 |
5 |
| SV **first = av_fetch(ea, 0, 0); |
| 563 |
|
| /* |
| 564 |
|
| # uncoverable branch false |
| 565 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 566 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 567 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 568 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 569 |
5 |
▶ | if (first && *first && SvROK(*first) |
Branch: 3 branches
| Branch | True | False | Total |
| if (first && *first && SvROK(*first) |
5 |
0 |
5 |
| if (first && *first && SvROK(*first) |
5 |
0 |
5 |
| if (first && *first && SvROK(*first) |
5 |
0 |
5 |
|
| 570 |
5 |
▶ | && SvTYPE(SvRV(*first)) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*first)) == SVt_PVHV) { |
5 |
0 |
5 |
|
| 571 |
5 |
| HV *e = (HV *)SvRV(*first); |
| 572 |
5 |
| at = pm_get(aTHX_ e, "instanceLocation"); |
| 573 |
5 |
| msg = pm_get(aTHX_ e, "message"); |
| 574 |
|
| } |
| 575 |
|
| } |
| 576 |
12 |
▶ | croak("Punk::Model: %s validation failed%s%s: %s", cls, |
Branch: 8 branches
| Branch | True | False | Total |
| 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 |
|
| 577 |
|
| (at && SvOK(at) && SvCUR(at)) ? " at " : "", |
| 578 |
|
| (at && SvOK(at) && SvCUR(at)) ? SvPV_nolen(at) : "", |
| 579 |
|
| (msg && SvOK(msg)) ? SvPV_nolen(msg) |
| 580 |
|
| : "does not match the field schema"); |
| 581 |
|
| } |
| 582 |
|
| } |
| 583 |
|
| |
| 584 |
|
| /* ---- the contract --------------------------------------------------------- */ |
| 585 |
|
| |
| 586 |
1469 |
| static SV *pm_slot(pTHX_ SV *self, const char *k) { |
| 587 |
|
| HV *hv; |
| 588 |
|
| /* |
| 589 |
|
| # uncoverable branch true |
| 590 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 591 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 592 |
1469 |
▶ | if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) |
1469 |
0 |
1469 |
| if (!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) |
0 |
1469 |
1469 |
|
| 593 |
0 |
| croak("Punk::Model: not a model instance"); /* # uncoverable statement */ |
| 594 |
1469 |
| hv = (HV *)SvRV(self); |
| 595 |
1469 |
| return pm_get(aTHX_ hv, k); |
| 596 |
|
| } |
| 597 |
|
| |
| 598 |
|
| /* Forward @_ to the backend's method of the same name, returning whatever |
| 599 |
|
| * it returns. The results land above the caller's own arguments, so they |
| 600 |
|
| * are copied down over them before the caller returns - without that, list |
| 601 |
|
| * context handed back ($self, @args, @results), which scalar context |
| 602 |
|
| * masked by taking the last value. */ |
| 603 |
1109 |
| static void pm_delegate(pTHX_ SV **sp_base, I32 items, const char *method, |
| 604 |
|
| I32 gimme) { |
| 605 |
1109 |
| dSP; |
| 606 |
1109 |
| SV *self = sp_base[0]; |
| 607 |
1109 |
| SV *backend = pm_slot(aTHX_ self, "backend"); |
| 608 |
|
| /* the callee can grow (and so move) the stack, so remember where the |
| 609 |
|
| * caller's frame starts as an offset, never as a pointer */ |
| 610 |
1109 |
| SSize_t base = sp_base - PL_stack_base; |
| 611 |
|
| I32 i; |
| 612 |
|
| /* |
| 613 |
|
| # uncoverable branch true |
| 614 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 615 |
1109 |
▶ | if (!backend) croak("Punk::Model: this model has no backend"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!backend) croak("Punk::Model: this model has no backend"); |
0 |
1109 |
1109 |
|
| 616 |
1109 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1109 |
1109 |
|
| 617 |
1109 |
▶ | EXTEND(SP, items); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, items); |
1109 |
0 |
1109 |
| EXTEND(SP, items); |
0 |
1109 |
1109 |
|
| 618 |
1109 |
| PUSHs(backend); |
| 619 |
2756 |
▶ | for (i = 1; i < items; i++) PUSHs(sp_base[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) PUSHs(sp_base[i]); |
1647 |
1109 |
2756 |
|
| 620 |
1109 |
| PUTBACK; |
| 621 |
1109 |
| call_method(method, gimme); |
| 622 |
952 |
| SPAGAIN; |
| 623 |
|
| { /* results sit above the caller's own arguments; slide them down over |
| 624 |
|
| * them so list context returns @results, not ($self, @args, |
| 625 |
|
| * @results) - which scalar context masked by taking the last value. |
| 626 |
|
| * They are call_method's mortals, and dest stays below source, so an |
| 627 |
|
| * ascending alias copy is safe. */ |
| 628 |
952 |
| SV **b = PL_stack_base + base; |
| 629 |
952 |
| SSize_t count = SP - (b + items - 1); |
| 630 |
|
| SSize_t j; |
| 631 |
|
| /* |
| 632 |
|
| # uncoverable branch true |
| 633 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 634 |
952 |
▶ | if (count < 0) count = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count < 0) count = 0; |
0 |
952 |
952 |
|
| 635 |
1900 |
▶ | for (j = 0; j < count; j++) b[j] = b[items + j]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < count; j++) b[j] = b[items + j]; |
948 |
952 |
1900 |
|
| 636 |
952 |
| SP = b + count - 1; |
| 637 |
|
| } |
| 638 |
952 |
| PUTBACK; |
| 639 |
952 |
| } |
| 640 |
|
| |
| 641 |
|
| #endif /* PUNK_MODEL_H */ |