/// <summary> /// Find <see cref="SemanticSchemaField"/> with given semantics. /// </summary> /// <param name="fieldSemantics">The semantics to check against</param> /// <param name="includeSelf">If <c>true</c> the field itself will be returned if it matches the given semantics.</param> /// <returns>This field or one of its embedded fields that match with the given semantics, null if a match cannot be found</returns> public SemanticSchemaField FindFieldBySemantics(FieldSemantics fieldSemantics) { // Perform a breadth-first lookup: first see if any of the embedded fields themselves match. SemanticSchemaField matchingEmbeddedField = Fields.FirstOrDefault(ssf => ssf.HasSemantics(fieldSemantics)); if (matchingEmbeddedField != null) { return(matchingEmbeddedField); } // If none of the embedded fields match: let each embedded field do a breadth-first lookup of its embedded fields (recursive). return(Fields.Select(ssf => ssf.FindFieldBySemantics(fieldSemantics)).FirstOrDefault(matchingField => matchingField != null)); }
/// <summary> /// Find <see cref="SemanticSchemaField"/> with given semantics. /// </summary> /// <param name="fieldSemantics">The semantics to check against</param> /// <returns>Schema field or one of its embedded fields that match with the given semantics, null if a match cannot be found</returns> public SemanticSchemaField FindFieldBySemantics(FieldSemantics fieldSemantics) { foreach (SemanticSchemaField field in Fields) { SemanticSchemaField matchingField = field.FindFieldBySemantics(fieldSemantics); if (matchingField != null) { return(matchingField); } } return(null); }
/// <summary> /// Find <see cref="SemanticSchemaField"/> with given semantics. /// </summary> /// <param name="fieldSemantics">The semantics to check against</param> /// <returns>This field or one of its embedded fields that match with the given semantics, null if a match cannot be found</returns> public SemanticSchemaField FindFieldBySemantics(FieldSemantics fieldSemantics) { if (ContainsSemantics(fieldSemantics)) { return(this); } foreach (SemanticSchemaField embeddedField in Fields) { SemanticSchemaField field = embeddedField.FindFieldBySemantics(fieldSemantics); if (field != null) { return(field); } } return(null); }
private static IField ExtractMatchedField(SemanticSchemaField matchingField, IFieldSet fields, int embedLevel, string path = null) { if (path==null) { path = matchingField.Path; while (embedLevel >= -1 && path.Contains("/")) { int pos = path.IndexOf("/", StringComparison.Ordinal); path = path.Substring(pos+1); embedLevel--; } } string[] bits = path.Split('/'); if (fields.ContainsKey(bits[0])) { if (bits.Length > 1) { int pos = path.IndexOf("/", StringComparison.Ordinal); return ExtractMatchedField(matchingField, fields[bits[0]].EmbeddedValues[0], embedLevel, path.Substring(pos + 1)); } return fields[bits[0]]; } return null; }