Beispiel #1
0
 private static bool VocabularyElementRegistered(OdcmVocabularyAnnotation e)
 {
     if (e == null)
     {
         return(false);
     }
     return(VocabularyElementRegistered(e.Namespace, e.Name));
 }
Beispiel #2
0
        /// <summary>
        ///     Gets stored IEdmAnnotations from a IEdmModel that correspond to an IEdm object and converts them to an enumerable
        ///     of OdcmAnnotations
        /// </summary>
        /// <param name="model">An IEdmModel which contains annotations and IEdm Objects</param>
        /// <param name="e">An object contained within the IEdmModel, such as an IEdmMethod</param>
        /// <returns>
        ///     An empty enumerable of annotations for objects which lack annotations (or cannot be annotated in EDM), or an
        ///     enumerable of converted annotations
        /// </returns>
        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.ValueTerm && elementType is IEdmValueTerm)
                    {
                        var valueTerm = (IEdmValueTerm)elementType;
                        var valueType = valueTerm.Type;

                        var valueAnnotation = annotation as IEdmValueAnnotation;

                        if (valueAnnotation == null)
                        {
                            throw new InvalidOperationException("Unexpected non-delayedValue annotation");
                        }

                        IEdmExpression valueExpression = valueAnnotation.Value;
                        var            result          = MapToClr(valueType, valueExpression);

                        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));
                    }
                }
            }
        }