コード例 #1
0
 private object SafeEvaluateArg(FieldContext fieldContext, MappedArg arg)
 {
     try {
         var value     = arg.Evaluator.GetValue(_requestContext);
         var convValue = _requestContext.ValidateConvert(value, arg.ArgDef.TypeRef, arg.Anchor);
         return(convValue);
     } catch (AbortRequestException) {
         return(null);
     } catch (InvalidInputException bvEx) {
         _requestContext.AddInputError(bvEx);
         _failed = true;
         return(null); //continue evaluating args; it will be aborted after all args are done
     } catch (Exception ex) {
         _requestContext.AddInputError($"Failed to evaluate argument {arg.ArgDef.Name}: {ex.Message}", arg.Anchor);
         _failed = true;
         return(null); // continue to next arg
     }
 }
コード例 #2
0
        public IList <MappedArg> MapArguments(IList <InputValue> args, IList <InputValueDef> argDefs, NamedRequestObject owner)
        {
            args ??= InputValue.EmptyList;
            var hasArgs    = args.Count > 0;
            var hasArgDefs = argDefs.Count > 0; // argDefs are never null

            // some corner cases first
            if (!hasArgDefs && !hasArgs)
            {
                return(MappedArg.EmptyList);
            }
            if (!hasArgDefs && hasArgs)
            {
                AddError($"No arguments are expected for '{owner.Name}'", owner);
                return(MappedArg.EmptyList);
            }

            // we have args and argDefs; first check that arg names are valid
            var unknownArgs = args.Where(a => !argDefs.Any(ad => ad.Name == a.Name)).ToList();

            if (unknownArgs.Count > 0)
            {
                foreach (var ua in unknownArgs)
                {
                    AddError($"Field(dir) '{owner.Name}': argument '{ua.Name}' not defined.", ua);
                }
                return(MappedArg.EmptyList);
            }

            // build Mapped Arg list - full list of args in right order matching the resolver method
            var mappedArgs = new List <MappedArg>();

            foreach (var argDef in argDefs)
            {
                var arg = args.FirstOrDefault(a => a.Name == argDef.Name);
                if (arg == null)
                {
                    // nullable args have default value null
                    if (argDef.HasDefaultValue || !argDef.TypeRef.IsNotNull)
                    {
                        var constValue = CreateConstantInputValue(argDef, owner, argDef.TypeRef, argDef.DefaultValue);
                        var MappedSelectionFieldArg = new MappedArg()
                        {
                            Anchor = owner, ArgDef = argDef, Evaluator = constValue
                        };
                        mappedArgs.Add(MappedSelectionFieldArg);
                    }
                    else
                    {
                        AddError($"Field(dir) '{owner.Name}': argument '{argDef.Name}' value is missing.", owner);
                    }
                    continue;
                }
                // arg != null
                try {
                    var argEval = GetInputValueEvaluator(argDef, arg.ValueSource, argDef.TypeRef);
                    var outArg  = new MappedArg()
                    {
                        Anchor = arg, ArgDef = argDef, Evaluator = argEval
                    };
                    mappedArgs.Add(outArg);
                } catch (InvalidInputException bvEx) {
                    _requestContext.AddInputError(bvEx);
                    continue;
                } catch (Exception ex) {
                    throw new InvalidInputException(ex.Message, arg.ValueSource, ex);
                }
            } //foreach argDef
            return(mappedArgs);
        }