public bool ShouldIncludeObject(OdcmObject odcmObject) { bool shouldInclude = true; if (this.IncludedObjects != null) { shouldInclude = this.IncludedObjects.Any(objectName => odcmObject.Name.Equals(objectName)); } else if (this.ExcludedObjects != null) { shouldInclude = this.ExcludedObjects.All(objectName => !odcmObject.Name.Equals(objectName)); } // Include and Exclude have priority over matches. // Only check if the description matches if we should include the object. if (shouldInclude && this.MatchingDescriptions != null) { shouldInclude = this.MatchingDescriptions.Any(objDescp => odcmObject.LongDescriptionContains(objDescp)); } if (shouldInclude && this.IgnoreDescriptions != null) { shouldInclude = !this.IgnoreDescriptions.Any(objDescp => odcmObject.LongDescriptionContains(objDescp)); } return(shouldInclude); }
public void Reset(TemplateFileInfo templateInfo, String templatesDirectory, OdcmObject currentType, OdcmModel currentModel) { this.TemplateFile = templateInfo.FullPath; this.TemplatesDirectory = templatesDirectory; this.CurrentType = currentType; this.CurrentModel = currentModel; }
protected TextFile ProcessTemplate(ITemplateInfo templateInfo, OdcmObject odcmObject, string fileName) { logger.Trace($"Generating file {fileName}"); var host = TemplateProcessor.Host(templateInfo, this.TemplatesDirectory, odcmObject, this.CurrentModel); Func <ITextTemplatingEngineHost, string> preProcessedTemplate; if (!this.preProcessedTemplates.TryGetValue(templateInfo.FullPath, out preProcessedTemplate)) { preProcessedTemplate = this.PreProcessTemplate(templateInfo); this.preProcessedTemplates.Add(templateInfo.FullPath, preProcessedTemplate); } var output = preProcessedTemplate(host); if (!string.IsNullOrEmpty(host.TemplateName)) { fileName = host.TemplateName; } if (host.Errors != null && host.Errors.HasErrors) { var errors = this.LogErrors(host, templateInfo); logger.Error(errors); throw new InvalidOperationException(errors); } var path = this.PathWriter.WritePath(templateInfo, fileName); logger.Debug("Wrote template to path {0}", path); host.TemplateHostStats.RecordProcessed(templateInfo, odcmObject != null ? odcmObject.Name : string.Empty, path); return(new TextFile(path, output)); }
public static OdcmEnum AsOdcmEnum(this OdcmObject odcmObject) { OdcmEnum foo = odcmObject as OdcmEnum; var bar = foo.Members.LastOrDefault(); return(odcmObject as OdcmEnum); }
private OdcmProperty SetPathCapability(OdcmObject odcmObject, IEdmPathExpression expression, string annotationTerm) { var odcmProperty = PropertyFromPathExpression(expression, odcmObject); // The term name serves as a tag for this capability SetBooleanCapability(odcmProperty, false, annotationTerm); return(odcmProperty); }
public static string SanitizePropertyName(this OdcmObject property) { if (ReservedNames.Contains(property.Name.ToLower())) { return(ReservedPrefix + property.Name); } return(property.Name); }
public static string GetDeprecationString(this OdcmObject instance) { if (instance.Deprecation != null) { return(String.Format(DeprecationString, instance.Deprecation.Description)); } return(String.Empty); }
public static string[] GetLongDescriptionSegments(this OdcmObject odcmObject) { if (odcmObject.LongDescription != null) { return(odcmObject.LongDescription.Split(';')); } return(null); }
/// <summary> /// Sets the OdcmCapabilities for the given annotated entity and also for the annotated navigation properties. /// </summary> public void SetCapabilitiesForEntity(OdcmObject odcmObject, IEnumerable <IEdmVocabularyAnnotation> annotations) { if (!(odcmObject is OdcmProperty)) { _propertyCapabilitiesCache.Add(odcmObject, OdcmCapability.DefaultEntitySetCapabilities); } ParseAllCapabilities(odcmObject, annotations); }
public static string SanitizePropertyName(this OdcmObject property) { if (ReservedNames.Contains(property.Name)) { return(ReservedPrefix + property.Name); } return(property.Name.Replace("@", string.Empty).Replace(".", "_")); }
/// <summary> /// Determines whether or not a given ODCM object is marked with a "Immutable" annotation. /// </summary> /// <param name="obj">The ODCM object</param> /// <returns>True if the ODCM object is marked as "Immutable", otherwise false.</returns> public static bool IsImmutable(this OdcmObject obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } return(obj.TryGetCapability(AnnotationTerms.Immutable) is OdcmBooleanCapability capability && capability.Value == true); }
private TextFile ProcessTemplate(ITemplateInfo templateInfo, OdcmObject odcmObject, string className = "", string propertyName = "", string methodName = "", string propertyType = "") { return(this.ProcessTemplate(templateInfo, odcmObject, templateInfo.BaseFileName(this.CurrentModel.EntityContainer.Name, className, propertyName, methodName, propertyType))); }
private static OdcmCapability TryGetCapability(this OdcmObject obj, string termName) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } return(obj.Projection?.Capabilities? .Where(c => c.TermName == termName)? .SingleOrDefault()); }
private IEnumerable <OdcmCapability> GetCapabilitiesOrEmpty(OdcmObject property) { ICollection <OdcmCapability> capabilities; if (!_propertyCache.TryGetValue(property, out capabilities)) { return(Enumerable.Empty <OdcmCapability>()); } return(capabilities); }
private void AddCapability(OdcmObject odcmObject, OdcmCapability capability) { var capabilities = _propertyCapabilitiesCache.GetCapabilities(odcmObject); // Check if this annotation was overridden by the object bool overridden = capabilities.Any(x => x.TermName == capability.TermName); if (!overridden) { capabilities.Add(capability); } }
public void Add(OdcmObject property, ICollection <OdcmCapability> capabilities) { if (property == null) { throw new ArgumentNullException("property"); } if (capabilities == null) { throw new ArgumentNullException("capabilities"); } _propertyCache[property] = capabilities; }
protected CustomHost GetCustomHost(Template template, OdcmObject odcmObject) { if (_hostIntance == null) { _hostIntance = new CustomHost(StrategyName, odcmObject); } _hostIntance.BaseTemplatePath = BaseFilePath; _hostIntance.TemplateFile = template.Path; _hostIntance.Model = Model; _hostIntance.OdcmType = odcmObject; return(_hostIntance); }
private void ParseAllCapabilities(OdcmObject odcmObject, IEnumerable <IEdmVocabularyAnnotation> annotations) { var parser = new CapabilityAnnotationParser(_propertyCapabilitiesCache); // Sort annotations by distance of their target from this odcmObject; // as a result, overridden annotations always precede the inherited ones. // This logic supports EntityType inheritance hierarchy. var annotationsOrdered = annotations.OrderBy(x => GetTargetDepth(x.Target as IEdmSchemaElement, odcmObject as OdcmClass)); foreach (var annotation in annotationsOrdered) { parser.ParseCapabilityAnnotation(odcmObject, annotation); } }
/// <summary> /// Sets the OdcmCapabilities for the given annotated entity and also for the annotated navigation properties. /// </summary> public void SetCapabilitiesForEntity(OdcmObject odcmObject, IEnumerable <IEdmVocabularyAnnotation> annotations) { if (!(odcmObject is OdcmProperty)) { try { _propertyCapabilitiesCache.Add(odcmObject, OdcmCapability.DefaultEntitySetCapabilities); } catch (InvalidOperationException e) { Logger.Warn("Failed to add property to cache", e); } } ParseAllCapabilities(odcmObject, annotations); }
private void ParsePropertyCollection(OdcmObject odcmObject, IEdmCollectionExpression collectionExpression, string annotationTerm) { foreach (IEdmRecordExpression recordExpression in collectionExpression.Elements) { var pathExpression = (IEdmPathExpression)recordExpression.Properties .First(p => p.Value is IEdmPathExpression) .Value; var odcmProperty = SetPathCapability(odcmObject, pathExpression, annotationTerm); foreach (var propertyConstructor in recordExpression.Properties.Where(p => !(p.Value is IEdmPathExpression))) { TryParseCapability(odcmProperty, propertyConstructor.Value, annotationTerm + "/" + propertyConstructor.Name); } } }
public static bool HasDerived(this OdcmObject odcmObject) { if (odcmObject.AsOdcmClass() != null) { return(odcmObject.AsOdcmClass().Derived.Any()); } else if (odcmObject.AsOdcmProperty() != null && odcmObject.AsOdcmProperty().Projection.Type.AsOdcmClass() != null) { return(odcmObject.AsOdcmProperty().Projection.Type.AsOdcmClass().Derived.Any()); } else if (odcmObject.AsOdcmMethod() != null && odcmObject.AsOdcmMethod().ReturnType.AsOdcmClass() != null) { return(odcmObject.AsOdcmMethod().ReturnType.AsOdcmClass().Derived.Any()); } return(false); }
private void AddVocabularyAnnotations(OdcmObject odcmObject, IEdmVocabularyAnnotatable annotatableEdmEntity) { #if false odcmObject.Annotations = ODataVocabularyReader.GetOdcmAnnotations(_edmModel, annotatableEdmEntity).ToList(); #endif odcmObject.Description = _edmModel.GetDescriptionAnnotation(annotatableEdmEntity); odcmObject.LongDescription = _edmModel.GetLongDescriptionAnnotation(annotatableEdmEntity); var annotations = _edmModel.FindVocabularyAnnotations(annotatableEdmEntity) .Where(x => x.Term.Name != "Description" && x.Term.Name != "LongDescription"); if (annotations.Any()) { SetCapabilitiesForEntity(odcmObject, annotations); } }
public ICollection <OdcmCapability> GetCapabilities(OdcmObject property) { if (property == null) { throw new ArgumentNullException("property"); } ICollection <OdcmCapability> capabilities; if (!_propertyCache.TryGetValue(property, out capabilities)) { throw new InvalidOperationException($"Property {property.Name} not found in the cache"); } return(capabilities); }
protected virtual IEnumerable <TextFile> ProcessTemplate(Template template, OdcmObject odcmObject) { var host = GetCustomHost(template, odcmObject); var templateContent = File.ReadAllText(host.TemplateFile); var output = Engine.ProcessTemplate(templateContent, host); if (host.Errors != null && host.Errors.HasErrors) { var errors = LogErrors(host, template); throw new InvalidOperationException(errors); } var path = PathWriter.WritePath(template, odcmObject.Name.ToCheckedCase()); return(new TextFile(path, output).ToIEnumerable()); }
private void TryParseCapability(OdcmObject odcmObject, IEdmExpression expression, string annotationTerm) { if (odcmObject is OdcmProperty prop && prop.ChildPropertyTypes.Any()) { prop.ChildPropertyTypes.ForEach(x => TryParseCapability(x, expression, annotationTerm)); } if (HasSpecializedParser(odcmObject, expression, annotationTerm)) { // Do nothing } else if (expression is IEdmBooleanConstantExpression) { bool value = (expression as IEdmBooleanConstantExpression).Value; SetBooleanCapability(odcmObject, value, annotationTerm); } else if (expression is IEdmEnumMemberExpression) { var values = (expression as IEdmEnumMemberExpression).EnumMembers.Select(v => v.Name); SetEnumCapability(odcmObject, values, annotationTerm); } else if (expression is IEdmStringConstantExpression) { var value = (expression as IEdmStringConstantExpression).Value; SetStringCapability(odcmObject, value, annotationTerm); } else if (expression is IEdmPathExpression) { SetPathCapability(odcmObject, expression as IEdmPathExpression, annotationTerm); } else if (expression is IEdmRecordExpression) { foreach (var propertyConstructor in (expression as IEdmRecordExpression).Properties) { TryParseCapability(odcmObject, propertyConstructor.Value, annotationTerm + "/" + propertyConstructor.Name); } } else if (expression is IEdmCollectionExpression) { ParseCollection(odcmObject, expression as IEdmCollectionExpression, annotationTerm); } else { Logger.Warn($"Unsupported annotation expression of kind {expression.ExpressionKind} for Term \"{annotationTerm}\""); } }
private void AddVocabularyAnnotations(OdcmObject odcmObject, IEdmVocabularyAnnotatable annotatableEdmEntity) { #if false odcmObject.Annotations = ODataVocabularyReader.GetOdcmAnnotations(_edmModel, annotatableEdmEntity).ToList(); #endif odcmObject.Description = _edmModel.GetDescriptionAnnotation(annotatableEdmEntity); odcmObject.LongDescription = _edmModel.GetLongDescriptionAnnotation(annotatableEdmEntity); // https://github.com/OData/odata.net/blob/75df8f44f2b81f984589790be4885b6ee8946ad0/src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs#L196 var annotations = _edmModel.FindVocabularyAnnotations(annotatableEdmEntity) .Where(x => x.Term.Name != "Description" && x.Term.Name != "LongDescription"); if (annotations.Any()) { SetCapabilitiesForEntity(odcmObject, annotations); } }
private void AddCapability(OdcmObject odcmObject, OdcmCapability capability) { try { var capabilities = _propertyCapabilitiesCache.GetCapabilities(odcmObject); // Check if this annotation was overridden by the object bool overridden = capabilities.Any(x => x.TermName == capability.TermName); if (!overridden) { capabilities.Add(capability); } } catch (Exception e) { Logger.Error(e, e.Message); } }
public static OdcmClass BaseClass(this OdcmObject odcmObject) { if (odcmObject.AsOdcmProperty() != null && odcmObject.AsOdcmProperty().Projection.Type.AsOdcmClass() != null) { var baseClass = odcmObject.AsOdcmProperty().Projection.Type.AsOdcmClass().Base; if (baseClass != null && !baseClass.IsAbstract) { return(baseClass); } } else if (odcmObject.AsOdcmClass() != null && odcmObject.AsOdcmClass().Base != null) { if (!odcmObject.AsOdcmClass().Base.IsAbstract) { return(odcmObject.AsOdcmClass().Base); } } return(null); }
protected TextFile ProcessTemplate(TemplateFileInfo templateInfo, OdcmObject odcmObject) { var host = TemplateProcessor.Host(templateInfo, this.TemplatesDirectory, odcmObject, this.CurrentModel); var templateContent = File.ReadAllText(host.TemplateFile); var output = this.T4Engine.ProcessTemplate(templateContent, host); if (host.Errors != null && host.Errors.HasErrors) { var errors = this.LogErrors(host, templateInfo); throw new InvalidOperationException(errors); } String fileName = (odcmObject != null) ? odcmObject.Name.ToUpperFirstChar() : templateInfo.TemplateBaseName.ToUpperFirstChar(); var path = this.PathWriter.WritePath(templateInfo, fileName); return(new TextFile(path, output)); }
private void ParseCollection(OdcmObject odcmObject, IEdmCollectionExpression collectionExpression, string annotationTerm) { if (!collectionExpression.Elements.Any()) { return; } var elementExpression = collectionExpression.Elements.First(); var recordExpression = elementExpression as IEdmRecordExpression; if (recordExpression != null) { if (recordExpression.Properties.Count(p => p.Value is IEdmPathExpression) == 1) { // We assume that if a collection element is a record with a single path expression, the rest // of record properties should be associated with this path expression // (e.g. NavigationRestrictions/RestrictedProperties). ParsePropertyCollection(odcmObject, collectionExpression, annotationTerm); } else { var records = ParseRecordCollection(collectionExpression); SetListCapability(odcmObject, records, annotationTerm); } } else if (elementExpression is IEdmStringConstantExpression) { var strings = collectionExpression.Elements.Select(e => (e as IEdmStringConstantExpression).Value); SetListCapability(odcmObject, strings, annotationTerm); } else if (elementExpression is IEdmPathExpression) { foreach (IEdmPathExpression expression in collectionExpression.Elements) { SetPathCapability(odcmObject, expression, annotationTerm); } } else { Logger.Warn($"Unsupported collection of kind {elementExpression.ExpressionKind.ToString()} for Term \"{annotationTerm}\""); } }