示例#1
0
        /// <inheritdoc />
        public override CdmObject Copy(ResolveOptions resOpt = null, CdmObject host = null)
        {
            if (resOpt == null)
            {
                resOpt = new ResolveOptions(this, this.Ctx.Corpus.DefaultResolutionDirectives);
            }

            CdmDataTypeDefinition copy;

            if (host == null)
            {
                copy = new CdmDataTypeDefinition(this.Ctx, this.DataTypeName, null);
            }
            else
            {
                copy              = host as CdmDataTypeDefinition;
                copy.Ctx          = this.Ctx;
                copy.DataTypeName = this.DataTypeName;
            }

            copy.ExtendsDataType = (CdmDataTypeReference)this.ExtendsDataType?.Copy(resOpt);

            this.CopyDef(resOpt, copy);
            return(copy);
        }
示例#2
0
        public override CdmObject Copy(ResolveOptions resOpt = null)
        {
            if (resOpt == null)
            {
                resOpt = new ResolveOptions(this);
            }

            CdmDataTypeDefinition copy = new CdmDataTypeDefinition(this.Ctx, this.DataTypeName, null)
            {
                ExtendsDataType = (CdmDataTypeReference)this.ExtendsDataType?.Copy(resOpt)
            };

            this.CopyDef(resOpt, copy);
            return(copy);
        }
示例#3
0
        /// <summary>
        /// Checks if the trait argumnet value matchs the data type defined on the trait parameter
        /// </summary>
        /// <param name="resOpt"></param>
        /// <param name="wrtDoc"></param>
        /// <param name="argumentValue"></param>
        /// <returns></returns>
        internal dynamic ConstTypeCheck(ResolveOptions resOpt, CdmDocumentDefinition wrtDoc, dynamic argumentValue)
        {
            ResolveContext ctx         = this.Ctx as ResolveContext;
            dynamic        replacement = argumentValue;

            // if parameter type is entity, then the value should be an entity or ref to one
            // same is true of 'dataType' dataType
            if (this.DataTypeRef == null)
            {
                return(replacement);
            }

            CdmDataTypeDefinition dt = this.DataTypeRef.FetchObjectDefinition <CdmDataTypeDefinition>(resOpt);

            if (dt == null)
            {
                Logger.Error(ctx, Tag, nameof(ConstTypeCheck), this.AtCorpusPath, CdmLogCode.ErrUnrecognizedDataType, this.Name);
                return(null);
            }

            // compare with passed in value or default for parameter
            dynamic pValue = argumentValue;

            if (pValue == null)
            {
                pValue      = this.DefaultValue;
                replacement = pValue;
            }
            if (pValue != null)
            {
                if (dt.IsDerivedFrom("cdmObject", resOpt))
                {
                    List <CdmObjectType> expectedTypes = new List <CdmObjectType>();
                    string expected = null;
                    if (dt.IsDerivedFrom("entity", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.ConstantEntityDef);
                        expectedTypes.Add(CdmObjectType.EntityRef);
                        expectedTypes.Add(CdmObjectType.EntityDef);
                        expectedTypes.Add(CdmObjectType.ProjectionDef);
                        expected = "entity";
                    }
                    else if (dt.IsDerivedFrom("attribute", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.AttributeRef);
                        expectedTypes.Add(CdmObjectType.TypeAttributeDef);
                        expectedTypes.Add(CdmObjectType.EntityAttributeDef);
                        expected = "attribute";
                    }
                    else if (dt.IsDerivedFrom("dataType", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.DataTypeRef);
                        expectedTypes.Add(CdmObjectType.DataTypeDef);
                        expected = "dataType";
                    }
                    else if (dt.IsDerivedFrom("purpose", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.PurposeRef);
                        expectedTypes.Add(CdmObjectType.PurposeDef);
                        expected = "purpose";
                    }
                    else if (dt.IsDerivedFrom("traitGroup", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.TraitGroupRef);
                        expectedTypes.Add(CdmObjectType.TraitGroupDef);
                        expected = "traitGroup";
                    }
                    else if (dt.IsDerivedFrom("trait", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.TraitRef);
                        expectedTypes.Add(CdmObjectType.TraitDef);
                        expected = "trait";
                    }
                    else if (dt.IsDerivedFrom("attributeGroup", resOpt))
                    {
                        expectedTypes.Add(CdmObjectType.AttributeGroupRef);
                        expectedTypes.Add(CdmObjectType.AttributeGroupDef);
                        expected = "attributeGroup";
                    }

                    if (expectedTypes.Count == 0)
                    {
                        Logger.Error(ctx, Tag, nameof(ConstTypeCheck), wrtDoc.FolderPath + wrtDoc.Name, CdmLogCode.ErrUnexpectedDataType, this.Name);
                    }

                    // if a string constant, resolve to an object ref.
                    CdmObjectType foundType  = CdmObjectType.Error;
                    Type          pValueType = pValue.GetType();

                    if (typeof(CdmObject).IsAssignableFrom(pValueType))
                    {
                        foundType = (pValue as CdmObject).ObjectType;
                    }
                    string foundDesc = ctx.RelativePath;
                    if (!(pValue is CdmObject))
                    {
                        // pValue is a string or JValue
                        pValue = (string)pValue;
                        if (pValue == "this.attribute" && expected == "attribute")
                        {
                            // will get sorted out later when resolving traits
                            foundType = CdmObjectType.AttributeRef;
                        }
                        else
                        {
                            foundDesc = pValue;
                            int seekResAtt = CdmObjectReferenceBase.offsetAttributePromise(pValue);
                            if (seekResAtt >= 0)
                            {
                                // get an object there that will get resolved later after resolved attributes
                                replacement = new CdmAttributeReference(ctx, pValue, true);
                                (replacement as CdmAttributeReference).Ctx        = ctx;
                                (replacement as CdmAttributeReference).InDocument = wrtDoc;
                                foundType = CdmObjectType.AttributeRef;
                            }
                            else
                            {
                                CdmObjectBase lu = ctx.Corpus.ResolveSymbolReference(resOpt, wrtDoc, pValue, CdmObjectType.Error, retry: true);
                                if (lu != null)
                                {
                                    if (expected == "attribute")
                                    {
                                        replacement = new CdmAttributeReference(ctx, pValue, true);
                                        (replacement as CdmAttributeReference).Ctx        = ctx;
                                        (replacement as CdmAttributeReference).InDocument = wrtDoc;
                                        foundType = CdmObjectType.AttributeRef;
                                    }
                                    else
                                    {
                                        replacement = lu;
                                        foundType   = (replacement as CdmObject).ObjectType;
                                    }
                                }
                            }
                        }
                    }
                    if (expectedTypes.IndexOf(foundType) == -1)
                    {
                        Logger.Error(ctx, Tag, nameof(ConstTypeCheck), wrtDoc.AtCorpusPath, CdmLogCode.ErrResolutionFailure, this.Name, expected, foundDesc, expected);
                    }
                    else
                    {
                        Logger.Info(ctx, Tag, nameof(ConstTypeCheck), wrtDoc.AtCorpusPath, $"resolved '{foundDesc}'");
                    }
                }
            }

            return(replacement);
        }