Debian Codegraph @

extra/haxelib_src/src/haxelib/Validator.hx

raw
1 /*
2 * Copyright (C)2005-2016 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22 package haxelib;
23
24 import haxe.ds.Option;
25
26 #if macro
27 import haxe.macro.Expr;
28 import haxe.macro.Type;
29 import haxe.macro.Context;
30
31 using haxe.macro.Tools;
32 #end
33
34 typedef Validatable = {
35 function validate():Option<String>;
36 }
37
38 class Validator {
39 #if macro
40 static var ARG = 'v';
41 var pos:Position;
42 var IARG:Expr;
43 function new(pos) {
44 this.pos = pos;
45 IARG = macro @:pos(pos) $i{ARG};
46 }
47
48 function doCheck(t:Type, e:Expr) {
49 var ct = t.toComplexType();
50 return
51 macro @:pos (function ($ARG : $ct) ${makeCheck(t)})($e);
52 }
53
54 function isAtom(s:String)
55 return switch s {
56 case 'String', 'Int', 'Bool', 'Float': true;
57 default: false;
58 }
59
60 function enforce(type:String)
61 return
62 macro @:pos(pos) if (!Std.is($i{ARG}, $i{type})) throw $v{'$type expected'};
63
64 function rename(e:Expr)
65 return switch e {
66 case macro $i{name} if (name == '_'): IARG;
67 default: e.map(rename);
68 }
69
70 function makeCheck(t:Type):Expr
71 return
72 switch Context.follow(t) {
73 case TAnonymous(_.get().fields => fields):
74
75 var block:Array<Expr> = [
76 for (f in fields)
77 if (f.kind.match(FVar(AccNormal, _)))
78 {
79 var name = f.name;
80 var rec = doCheck(f.type, macro @:pos(pos) $IARG.$name);
81
82 if (f.meta.has(':requires')) {
83 var body = [];
84 for (m in f.meta.get())
85 if (m.name == ':requires')
86 for (p in m.params)
87 switch p {
88 case macro $msg => $p:
89 body.push(rename(
90 macro @:pos(pos) if (!$p) throw $msg
91 ));
92 default:
93 Context.error('Should be "<message>" =>" <condition>', p.pos);
94 }
95 //{
96 //p = rename(p);
97 //cond = macro @:pos(pos) $p && $cond;
98 //}
99
100 var t = f.type.toComplexType();
101 rec = macro @:pos(pos) {
102 $rec;
103 (function($ARG : $t) $b{body})($IARG.$name);
104 }
105 }
106
107 if (f.meta.has(':optional')) {
108 rec = macro @:pos(pos) if (Reflect.hasField($IARG, $v{name}) && $IARG.$name != null) $rec;
109 }
110 else
111 rec = macro @:pos(pos)
112 if (!Reflect.hasField($IARG, $v{name}))
113 throw ("missing field " + $v{name});
114 else
115 $rec;
116
117 rec;
118 }
119 ];
120
121 block.unshift(
122 macro @:pos(pos) if (!Reflect.isObject($IARG)) throw 'object expected'
123 );
124
125 macro @:pos(pos) $b{block};
126
127 case _.toString() => atom if (isAtom(atom)):
128
129 enforce(atom);
130
131 case TInst(_.get().module => 'Array', [p]):
132
133 macro @:pos(pos) {
134 ${enforce('Array')};
135 for ($IARG in $IARG)
136 ${doCheck(p, IARG)};
137 }
138
139 case TAbstract(_.get() => { from: [ { t: t, field: null } ] }, _):
140
141 makeCheck(t);
142
143 case TAbstract(_.get() => a, _) if (a.meta.has(':enum')):
144 var name = a.module + '.' + a.name;
145 var options:Array<Expr> = [
146 for (f in a.impl.get().statics.get())
147 if (f.kind.match(FVar(_, _)))
148 macro @:pos(pos) $p{(name+'.'+f.name).split('.')}
149 ];
150
151 macro if (!Lambda.has($a { options }, $IARG)) throw 'Invalid value ' + $IARG + ' for ' + $v { a.name };
152
153 case TAbstract(_.get() => a, _):
154
155 macro @:pos(pos) switch ($IARG : haxelib.Validator.Validatable).validate() {
156 case Some(e): throw e;
157 case None:
158 }
159
160 case TDynamic(k):
161 var checker = makeCheck(k);
162 var ct = k.toComplexType();
163 macro @:pos(pos) {
164 if (!Reflect.isObject($i{ARG})) throw 'object expected';
165 for (f in Reflect.fields($i{ARG})) {
166 var $ARG:$ct = Reflect.field($i{ARG}, f);
167 $checker;
168 }
169 }
170 case v:
171 throw t.toString();
172 }
173 #end
174 macro static public function validate(e:Expr)
175 return
176 new Validator(e.pos).doCheck(Context.typeof(e), e);
177 }