internal static async Task ProcessTraitsAndAnnotationsToData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits) { if (traits == null) { return; } var annotations = new List <Annotation>(); var extensions = new List <JToken>(); foreach (var trait in traits) { if (ExtensionHelper.TraitRefIsExtension(trait)) { // Safe to cast since extensions can only be trait refs, not trait group refs ExtensionHelper.ProcessExtensionTraitToObject(trait as CdmTraitReference, obj); } else if (trait.NamedReference == "is.modelConversion.otherAnnotations") { // Safe to cast since "is.modelConversion.otherAnnotations" is a trait, not trait group foreach (var annotation in (trait as CdmTraitReference).Arguments[0].Value) { if (annotation is JObject jAnnotation) { annotations.Add(jAnnotation.ToObject <Annotation>()); } else if (annotation is NameValuePair) { Annotation element = new Annotation() { Name = annotation.Name, Value = annotation.Value }; annotations.Add(element); } else { Logger.Warning(ctx, Tag, nameof(ProcessTraitsAndAnnotationsToData), null, CdmLogCode.WarnAnnotationTypeNotSupported); } } } else if (trait is CdmTraitReference && traitToAnnotationMap.ContainsKey(trait.NamedReference)) { Annotation element = await ArgumentPersistence.ToData((trait as CdmTraitReference).Arguments[0], null, null); element.Name = ConvertTraitToAnnotation(trait.NamedReference); annotations.Add(element); } else if ( !ignoredTraits.Contains(trait.NamedReference) && !trait.NamedReference.StartsWith("is.dataFormat") && !(modelJsonPropertyTraits.Contains(trait.NamedReference) && trait is CdmTraitReference && (trait as CdmTraitReference).IsFromProperty) && ShouldPersistTrait(trait)) { var extension = trait is CdmTraitGroupReference? CdmFolder.TraitGroupReferencePersistence.ToData(trait as CdmTraitGroupReference, null, null) : CdmFolder.TraitReferencePersistence.ToData(trait as CdmTraitReference, null, null); extensions.Add(JToken.FromObject(extension, JsonSerializationUtil.JsonSerializer)); } } if (annotations.Count > 0) { obj.Annotations = annotations; } if (extensions.Count > 0) { obj.Traits = extensions; } }
internal static async Task ProcessAnnotationsToData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits) { if (traits == null) { return; } var annotations = new List <Annotation>(); var extensions = new List <JToken>(); foreach (var trait in traits) { if (ExtensionHelper.TraitRefIsExtension(trait)) { ExtensionHelper.ProcessExtensionTraitToObject(trait, obj); continue; } if (trait.NamedReference == "is.modelConversion.otherAnnotations") { foreach (var annotation in trait.Arguments[0].Value) { if (annotation is JObject jAnnotation) { annotations.Add(jAnnotation.ToObject <Annotation>()); } else if (annotation is Annotation) { annotations.Add(annotation); } else { Logger.Warning(nameof(Utils), ctx, "Unsupported annotation type."); } } } else if (!trait.IsFromProperty) { var annotationName = TraitToAnnotationName(trait.NamedReference); if (annotationName != null && trait.Arguments != null && trait.Arguments.Count == 1) { var argument = await ArgumentPersistence.ToData(trait.Arguments.AllItems[0], null, null); if (argument != null) { argument.Name = annotationName; annotations.Add(argument); } } else if (!ignoredTraits.Contains(trait.NamedReference)) { var extension = CdmFolder.TraitReferencePersistence.ToData(trait, null, null); extensions.Add(JToken.FromObject(extension, JsonSerializationUtil.JsonSerializer)); } } } if (annotations.Count > 0) { obj.Annotations = annotations; } if (extensions.Count > 0) { obj.Traits = extensions; } }