Esempio n. 1
0
        // Call a function with native arguments, get a return value or null if void
        public Result Evaluate(string name, params object[] arguments)
        {
            var kind = _catvars.GetKind(name);

            if (kind != EntryKinds.Code)
            {
                return(Result.Failure("unknown or invalid name"));
            }

            var expr = (_catvars.GetValue(name) as CodeValue).Value;

            if (arguments.Length != expr.Lookup.Degree)
            {
                return(Result.Failure("wrong no of args"));
            }

            var        argvalues = arguments.Select((a, x) => TypeMaker.FromNativeValue(a, expr.Lookup.Columns[x].DataType)).ToArray();
            var        args      = DataRow.CreateNonTuple(expr.Lookup, argvalues);
            TypedValue value     = null;

            try {
                value = _evaluator.Exec(expr.Code, args);
            } catch (ProgramException ex) {
                return(Result.Failure(ex.ToString()));
            }
            var nvalue = (value == VoidValue.Void) ? null : TypeMaker.ToNativeValue(value);

            return(Result.Success(nvalue));
        }
Esempio n. 2
0
        // Get a native value from a variable or parameterless function
        public Result GetValue(string name)
        {
            var kind = _catvars.GetKind(name);

            if (kind == EntryKinds.Code)
            {
                return(Evaluate(name));
            }
            if (kind != EntryKinds.Value)
            {
                return(Result.Failure("unknown or invalid name"));
            }

            var nvalue = TypeMaker.ToNativeValue(_catvars.GetValue(name));

            return(Result.Success(nvalue));
        }
Esempio n. 3
0
        // call a function with args passed in json, return Result in json
        public Result JsonCall(string name, params string[] jsonargs)
        {
            var kind = _catvars.GetKind(name);

            if (kind != EntryKinds.Code)
            {
                return(Result.Failure("unknown or invalid name"));
            }
            var expr = (_catvars.GetValue(name) as CodeValue).Value;

            if (expr.Lookup.Degree != jsonargs.Length)
            {
                return(Result.Failure("wrong no of args"));
            }

            DataRow    argvalue;
            TypedValue retvalue;

            try {
                var argvalues = jsonargs.Select((a, x) => {
                    var datatype = expr.Lookup.Columns[x].DataType;
                    var nvalue   = JsonConvert.DeserializeObject(a, datatype.NativeType);
                    return(TypeMaker.FromNativeValue(nvalue, datatype));
                }).ToArray();
                argvalue = DataRow.CreateNonTuple(expr.Lookup, argvalues);
            } catch {
                return(Result.Failure("argument conversion error"));
            }
            try {
                retvalue = _evaluator.Exec(expr.Code, argvalue);
            } catch (ProgramException ex) {
                return(Result.Failure(ex.ToString()));
            }
            if (retvalue != VoidValue.Void)
            {
                var nret = TypeMaker.ToNativeValue(retvalue);
                if (_runtime.JsonReturnFlag) // FIX: s/b default
                {
                    var jret = JsonConvert.SerializeObject(nret);
                    return(Result.Success(jret));
                }
                return(Result.Success(nret));
            }
            return(Result.Success(null));
        }
Esempio n. 4
0
        // call a function with args passed as id, query and json, return Result with message or json
        // NOTE: empty query array here is NOT the same as a null
        public Result JsonCall(string name, string id, KeyValuePair <string, string>[] query, string json)
        {
            var kind = _catvars.GetKind(name);

            if (kind != EntryKinds.Code)
            {
                return(Result.Failure("unknown or invalid name: " + name));
            }
            var expr = (_catvars.GetValue(name) as CodeValue).Value;

            var argcount = (id != null ? 1 : 0) + (query != null ? 1 : 0) + (json != null ? 1 : 0);

            if (expr.Lookup.Degree != argcount)
            {
                return(Result.Failure("wrong no of args, expected " + expr.Lookup.Degree.ToString()));
            }

            DataRow       argvalue;
            List <object> nargvalues = new List <object>();
            TypedValue    retvalue;

            // convert each argument to a native form TypeMaker will understand
            if (id != null)
            {
                nargvalues.Add(id);
            }
            if (query != null && query.Length > 0)
            {
                nargvalues.Add(query.Select(kvp => new KeyValue {
                    Key = kvp.Key, Value = kvp.Value
                }).ToList());
            }
            if (json != null)
            {
                try {
                    var datatype = expr.Lookup.Columns[nargvalues.Count].DataType;
                    nargvalues.Add(JsonConvert.DeserializeObject(json, datatype.NativeType));
                } catch {
                    return(Result.Failure("json conversion error"));
                }
            }
            try {
                var argvalues = nargvalues.Select((nv, x) => {
                    var datatype = expr.Lookup.Columns[x].DataType;
                    return(TypeMaker.FromNativeValue(nv, datatype));
                }).ToArray();
                argvalue = DataRow.CreateNonTuple(expr.Lookup, argvalues);
            } catch {
                return(Result.Failure("argument conversion error"));
            }
            try {
                retvalue = _evaluator.Exec(expr.Code, argvalue);
            } catch (ProgramException ex) {
                return(Result.Failure(ex.ToString()));
            }
            var nret = (retvalue == VoidValue.Void) ? null : TypeMaker.ToNativeValue(retvalue);

            if (_runtime.JsonReturnFlag) // FIX: s/b default
            {
                var jret = JsonConvert.SerializeObject(nret);
                return(Result.Success(jret));
            }
            return(Result.Success(nret));
        }