in src/Readers/Vipr.Reader.OData.v4/ODataVocabularyReader.cs [69:129]
public static IEnumerable<OdcmVocabularyAnnotation> GetOdcmAnnotations(IEdmModel model, object e)
{
// Only annotatable types will have annotations; return an empty list
if (!(e is IEdmVocabularyAnnotatable))
{
yield break;
}
var annotatable = (IEdmVocabularyAnnotatable) e;
// We must use the model to obtain annotations
var annotations = model.FindVocabularyAnnotations(annotatable);
foreach (var annotation in annotations)
{
// Perform the mapping from IEdmAnnotation types to OcdmAnnotations
// Mapping name and name space is the simplest piece
OdcmVocabularyAnnotation odcmAnnotation = new OdcmVocabularyAnnotation
{
Name = annotation.Term.Name,
Namespace = annotation.Term.Namespace
};
// Vocabulary elements are registered when we have parsed their model and cached it
// Unmapped elements are either invalid vocabulary terms (mispelled or otherwise) or have not been registered
if (VocabularyElementRegistered(odcmAnnotation))
{
var elementType = _registeredVocabularyTypes[odcmAnnotation.Namespace][odcmAnnotation.Name];
// We have a delayedValue that will get us to the corresponding type of the annotation
if (elementType.SchemaElementKind == EdmSchemaElementKind.Term && elementType is IEdmTerm)
{
var term = (IEdmTerm)elementType;
var valueType = term.Type;
var valueAnnotation = annotation as IEdmVocabularyAnnotation;
if (valueAnnotation == null)
{
throw new InvalidOperationException("Unexpected non-delayedValue annotation");
}
IEdmExpression valueExpression = valueAnnotation.Value;
var result = MapToClr(valueType, valueExpression);
if (result == null) continue;
odcmAnnotation.Value = result;
yield return odcmAnnotation;
}
else
{
throw new InvalidOperationException(
string.Format(
"Cannot return annotation values for EDM element types that are not ValueTerms. Type was {0} for element name {1}",
elementType.SchemaElementKind, elementType.Name));
}
}
}
}