本文整理汇总了C#中ValidationContext.GetRecursiveVariables方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.GetRecursiveVariables方法的具体用法?C# ValidationContext.GetRecursiveVariables怎么用?C# ValidationContext.GetRecursiveVariables使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ValidationContext
的用法示例。
在下文中一共展示了ValidationContext.GetRecursiveVariables方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public INodeVisitor Validate(ValidationContext context)
{
var variableNameDefined = new Dictionary<string, bool>();
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(varDef => variableNameDefined[varDef.Name] = true);
_.Match<Operation>(
enter: op => variableNameDefined = new Dictionary<string, bool>(),
leave: op =>
{
var usages = context.GetRecursiveVariables(op);
usages.Apply(usage =>
{
var varName = usage.Node.Name;
bool found;
if (!variableNameDefined.TryGetValue(varName, out found))
{
var error = new ValidationError(
context.OriginalQuery,
"5.7.4",
UndefinedVarMessage(varName, op.Name),
usage.Node,
op);
context.ReportError(error);
}
});
});
});
}
示例2: Validate
public INodeVisitor Validate(ValidationContext context)
{
var variableDefs = new List<VariableDefinition>();
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(def => variableDefs.Add(def));
_.Match<Operation>(
enter: op => variableDefs = new List<VariableDefinition>(),
leave: op =>
{
var usages = context.GetRecursiveVariables(op).Select(usage => usage.Node.Name);
variableDefs.Apply(variableDef =>
{
var variableName = variableDef.Name;
if (!usages.Contains(variableName))
{
var error = new ValidationError(context.OriginalQuery, "5.7.5", UnusedVariableMessage(variableName, op.Name), variableDef);
context.ReportError(error);
}
});
});
});
}
示例3: Validate
public INodeVisitor Validate(ValidationContext context)
{
var varDefMap = new Dictionary<string, VariableDefinition>();
return new EnterLeaveListener(_ =>
{
_.Match<VariableDefinition>(
varDefAst => varDefMap[varDefAst.Name] = varDefAst
);
_.Match<Operation>(
enter: op => varDefMap = new Dictionary<string, VariableDefinition>(),
leave: op =>
{
var usages = context.GetRecursiveVariables(op);
usages.Apply(usage =>
{
var varName = usage.Node.Name;
VariableDefinition varDef;
if (!varDefMap.TryGetValue(varName, out varDef))
{
return;
}
if (varDef != null && usage.Type != null)
{
var varType = varDef.Type.GraphTypeFromType(context.Schema);
if (varType != null &&
!effectiveType(varType, varDef).IsSubtypeOf(usage.Type, context.Schema))
{
var error = new ValidationError(
context.OriginalQuery,
"5.7.6",
BadVarPosMessage(varName, context.Print(varType), context.Print(usage.Type)));
var source = new Source(context.OriginalQuery);
var varDefPos = new Location(source, varDef.SourceLocation.Start);
var usagePos = new Location(source, usage.Node.SourceLocation.Start);
error.AddLocation(varDefPos.Line, varDefPos.Column);
error.AddLocation(usagePos.Line, usagePos.Column);
context.ReportError(error);
}
}
});
}
);
});
}