Ejemplo n.º 1
0
        internal static async Task ProcessAnnotationsFromData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits)
        {
            var multiTraitAnnotations = new List <NameValuePair>();

            if (obj.Annotations != null)
            {
                foreach (var element in obj.Annotations)
                {
                    if (!ShouldAnnotationGoIntoASingleTrait(element.Name))
                    {
                        NameValuePair cdmElement = new NameValuePair()
                        {
                            Name  = element.Name,
                            Value = element.Value
                        };
                        multiTraitAnnotations.Add(cdmElement);
                    }
                    else
                    {
                        var innerTrait = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, ConvertAnnotationToTrait(element.Name));
                        innerTrait.Arguments.Add(await ArgumentPersistence.FromData(ctx, element));
                        traits.Add(innerTrait);
                    }
                }

                if (multiTraitAnnotations.Count > 0)
                {
                    var trait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.modelConversion.otherAnnotations", false);
                    trait.IsFromProperty = false;
                    var annotationsArgument = new CdmArgumentDefinition(ctx, "annotations")
                    {
                        Value = multiTraitAnnotations
                    };
                    trait.Arguments.Add(annotationsArgument);
                    traits.Add(trait);
                }
            }

            if (obj.Traits != null)
            {
                foreach (var trait in obj.Traits)
                {
                    var trToken = JToken.FromObject(trait);

                    if (!(trToken is JValue) && trToken["traitGroupReference"] != null)
                    {
                        traits.Add(CdmFolder.TraitGroupReferencePersistence.FromData(ctx, trToken));
                    }
                    else
                    {
                        traits.Add(CdmFolder.TraitReferencePersistence.FromData(ctx, trToken));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal static async Task ProcessAnnotationsFromData(CdmCorpusContext ctx, MetadataObject obj, CdmTraitCollection traits)
        {
            var multiTraitAnnotations = new List <Annotation>();

            if (obj.Annotations != null)
            {
                foreach (var element in obj.Annotations)
                {
                    if (!ShouldAnnotationGoIntoASingleTrait(element.Name))
                    {
                        multiTraitAnnotations.Add(element);
                    }
                    else
                    {
                        var innerTrait = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, ConvertAnnotationToTrait(element.Name));
                        innerTrait.Arguments.Add(await ArgumentPersistence.FromData(ctx, element));
                        traits.Add(innerTrait);
                    }
                }

                var trait = ctx.Corpus.MakeRef <CdmTraitReference>(CdmObjectType.TraitRef, "is.modelConversion.otherAnnotations", false);
                trait.IsFromProperty = true;
                var annotationsArgument = new CdmArgumentDefinition(ctx, "annotations")
                {
                    Value = multiTraitAnnotations
                };
                trait.Arguments.Add(annotationsArgument);
                traits.Add(trait);
            }

            if (obj.Traits != null)
            {
                foreach (var trait in obj.Traits)
                {
                    var traitInstance = CdmFolder.TraitReferencePersistence.FromData(ctx, JToken.FromObject(trait));
                    traits.Add(traitInstance);
                }
            }
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        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;
            }
        }