Add noeval to PYSONDecoder

issue1616
review25491002
This commit is contained in:
Sergi Almacellas Abellana
2016-09-13 10:15:42 +02:00
parent 0f2a922ffe
commit d6b3ae4ba0
3 changed files with 84 additions and 2 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add noeval to PYSONDecoder
* Add support for pyson_order on action window
* Make datetimepicker locale aware
* Restore tab default order when no manual change
+56 -2
View File
@@ -19,6 +19,9 @@
Sao.PYSON.PYSON.eval_ = function(value, context) {
throw 'NotImplementedError';
};
Sao.PYSON.PYSON.init_from_object = function(object) {
throw 'NotImplementedError';
};
Sao.PYSON.Encoder = Sao.class_(Object, {
encode: function(pyson) {
@@ -50,15 +53,23 @@
});
Sao.PYSON.Decoder = Sao.class_(Object, {
init: function(context) {
init: function(context, noeval) {
this.__context = context || {};
this.noeval = noeval || false;
},
decode: function(str) {
var reviver = function(k, v) {
if (typeof v == 'object' && v !== null) {
var cls = Sao.PYSON[v.__class__];
if (cls) {
return cls.eval_(v, this.__context);
if (!this.noeval) {
return cls.eval_(v, this.__context);
} else {
var args = jQuery.extend({}, v);
delete args.__class__;
return Sao.PYSON[v.__class__].init_from_object(
args);
}
}
}
return v;
@@ -99,6 +110,9 @@
return value.d;
}
};
Sao.PYSON.Eval.init_from_object = function(obj) {
return new Sao.PYSON.Eval(obj.v, obj.d);
};
Sao.PYSON.Not = Sao.class_(Sao.PYSON.PYSON, {
init: function(value) {
@@ -129,6 +143,9 @@
Sao.PYSON.Not.eval_ = function(value, context) {
return !value.v;
};
Sao.PYSON.Not.init_from_object = function(obj) {
return new Sao.PYSON.Not(obj.v);
};
Sao.PYSON.Bool = Sao.class_(Sao.PYSON.PYSON, {
init: function(value) {
@@ -153,6 +170,9 @@
return Boolean(value.v);
}
};
Sao.PYSON.Bool.init_from_object = function(obj) {
return new Sao.PYSON.Bool(obj.v);
};
Sao.PYSON.And = Sao.class_(Sao.PYSON.PYSON, {
@@ -198,6 +218,9 @@
}
return result;
};
Sao.PYSON.And.init_from_object = function(obj) {
return new Sao.PYSON.And(obj.s);
};
Sao.PYSON.Or = Sao.class_(Sao.PYSON.And, {
@@ -216,6 +239,9 @@
}
return result;
};
Sao.PYSON.Or.init_from_object= function(obj) {
return new Sao.PYSON.Or(obj.s);
};
Sao.PYSON.Equal = Sao.class_(Sao.PYSON.PYSON, {
init: function(statement1, statement2) {
@@ -257,6 +283,9 @@
return value.s1 == value.s2;
}
};
Sao.PYSON.Equal.init_from_object = function(obj) {
return new Sao.PYSON.Equal(obj.s1, obj.s2);
};
Sao.PYSON.Greater = Sao.class_(Sao.PYSON.PYSON, {
init: function(statement1, statement2, equal) {
@@ -319,6 +348,9 @@
return value.s1 > value.s2;
}
};
Sao.PYSON.Greater.init_from_object = function(obj) {
return new Sao.PYSON.Greater(obj.s1, obj.s2, obj.e);
};
Sao.PYSON.Less = Sao.class_(Sao.PYSON.Greater, {
pyson: function() {
@@ -338,6 +370,9 @@
return value.s1 < value.s2;
}
};
Sao.PYSON.Less.init_from_object = function(obj) {
return new Sao.PYSON.Less(obj.s1, obj.s2, obj.e);
};
Sao.PYSON.If = Sao.class_(Sao.PYSON.PYSON, {
init: function(condition, then_statement, else_statement) {
@@ -398,6 +433,9 @@
return value.e;
}
};
Sao.PYSON.If.init_from_object = function(obj) {
return new Sao.PYSON.If(obj.c, obj.t, obj.e);
};
Sao.PYSON.Get = Sao.class_(Sao.PYSON.PYSON, {
init: function(obj, key, default_) {
@@ -453,6 +491,9 @@
return value.d;
}
};
Sao.PYSON.Get.init_from_object = function(obj) {
return new Sao.PYSON.Get(obj.v, obj.k, obj.d);
};
Sao.PYSON.In = Sao.class_(Sao.PYSON.PYSON, {
init: function(key, obj) {
@@ -497,6 +538,9 @@
return !!value.v[value.k];
}
};
Sao.PYSON.In.init_from_object = function(obj) {
return new Sao.PYSON.In(obj.k, obj.v);
};
Sao.PYSON.Date = Sao.class_(Sao.PYSON.PYSON, {
init: function(year, month, day, delta_years, delta_months, delta_days)
@@ -558,6 +602,9 @@
if (value.dd) date.add(value.dd, 'd');
return date;
};
Sao.PYSON.Date.init_from_object = function(obj) {
return new Sao.PYSON.Date(obj.y, obj.M, obj.d, obj.dy, obj.dM, obj.dd);
};
Sao.PYSON.DateTime = Sao.class_(Sao.PYSON.Date, {
init: function(year, month, day, hour, minute, second, microsecond,
@@ -619,6 +666,10 @@
if (value.dms) date.add(value.dms / 1000, 'ms');
return date;
};
Sao.PYSON.DateTime.init_from_object = function(obj) {
return new Sao.PYSON.DateTime(obj.y, obj.M, obj.d, obj.h, obj.m, obj.s,
obj.ms, obj.dy, obj.dM, obj.dd, obj.dh, obj.dm, obj.ds, obj.dms);
};
Sao.PYSON.Len = Sao.class_(Sao.PYSON.PYSON, {
init: function(value) {
@@ -653,4 +704,7 @@
return value.v.length;
}
};
Sao.PYSON.Len.init_from_object = function(obj) {
return new Sao.PYSON.Len(obj.v);
};
}());
+27
View File
@@ -838,6 +838,33 @@
QUnit.strictEqual(new Sao.PYSON.Decoder().decode(eval_), 7,
"decode(Len('foo bar'))");
});
QUnit.test('PYSON noeval', function() {
var decoder = new Sao.PYSON.Decoder({}, true);
var encoder = new Sao.PYSON.Encoder();
var noeval_tests = [
new Sao.PYSON.Eval('test', 0),
new Sao.PYSON.Not(true),
new Sao.PYSON.Bool('test'),
new Sao.PYSON.Not(true),
new Sao.PYSON.Bool('test'),
new Sao.PYSON.And(true, false, true),
new Sao.PYSON.Or(false, true, true),
new Sao.PYSON.Equal('foo', 'bar'),
new Sao.PYSON.Greater(1, 0),
new Sao.PYSON.Less(0, 1),
new Sao.PYSON.If(true, 'foo', 'bar'),
new Sao.PYSON.Get({'foo': 'bar'}, 'foo', 'default'),
new Sao.PYSON.In('foo', ['foo', 'bar']),
new Sao.PYSON.Date(),
new Sao.PYSON.DateTime(),
new Sao.PYSON.Len([1, 2, 3]),
];
noeval_tests.forEach(function(instance) {
QUnit.ok(
Sao.common.compare(decoder.decode(encoder.encode(instance)),
instance));
});
});
QUnit.test('DomainParser.group_operator', function() {
var parser = new Sao.common.DomainParser();