Exemple #1
0
        private Schema(Type tdoc)
        {
            lock (s_TypeLatch)
            {
                if (s_TypeLatch.Contains(tdoc))
                {
                    throw new DataException(StringConsts.CRUD_TYPED_DOC_RECURSIVE_FIELD_DEFINITION_ERROR.Args(tdoc.FullName));
                }

                s_TypeLatch.Add(tdoc);
                try
                {
                    m_Name = tdoc.AssemblyQualifiedName;


                    var tattrs = tdoc.GetCustomAttributes(typeof(TableAttribute), false).Cast <TableAttribute>();
                    m_TableAttrs = new List <TableAttribute>(tattrs);


                    m_FieldDefs = new OrderedRegistry <FieldDef>();
                    var props = GetFieldMembers(tdoc);
                    var order = 0;
                    foreach (var prop in props)
                    {
                        var fattrs = prop.GetCustomAttributes(typeof(FieldAttribute), false)
                                     .Cast <FieldAttribute>()
                                     .ToArray();

                        //20160318 DKh. Interpret [Field(CloneFromType)]
                        for (var i = 0; i < fattrs.Length; i++)
                        {
                            var attr = fattrs[i];
                            if (attr.CloneFromDocType == null)
                            {
                                continue;
                            }

                            if (fattrs.Length > 1)
                            {
                                throw new DataException(StringConsts.CRUD_TYPED_DOC_SINGLE_CLONED_FIELD_ERROR.Args(tdoc.FullName, prop.Name));
                            }

                            var clonedSchema = Schema.GetForTypedDoc(attr.CloneFromDocType);
                            var clonedDef    = clonedSchema[prop.Name];
                            if (clonedDef == null)
                            {
                                throw new DataException(StringConsts.CRUD_TYPED_DOC_CLONED_FIELD_NOTEXISTS_ERROR.Args(tdoc.FullName, prop.Name));
                            }

                            fattrs = clonedDef.Attrs.ToArray();//replace these attrs from the cloned target
                            break;
                        }

                        var fdef = new FieldDef(prop.Name, order, prop.PropertyType, fattrs, prop);
                        m_FieldDefs.Register(fdef);

                        order++;
                    }
                    s_TypedRegistry.Register(this);
                    m_TypedDocType = tdoc;
                }
                finally
                {
                    s_TypeLatch.Remove(tdoc);
                }
            }    //lock
        }
Exemple #2
0
            /// <summary>
            /// Writes fielddef as JSON. Do not call this method directly, instead call rowset.ToJSON() or use JSONWriter class
            /// </summary>
            void IJsonWritable.WriteAsJson(System.IO.TextWriter wri, int nestingLevel, JsonWritingOptions options)
            {
                var attr = this[null];

                if (attr != null && attr.NonUI)
                {
                    wri.Write("{}");
                    return;//nothing to write for NONUI
                }

                bool   typeIsNullable;
                string tp = JSONMappings.MapCLRTypeToJSON(m_Type, out typeIsNullable);

                var map = new Dictionary <string, object>
                {
                    { "Name", m_Name },
                    { "Order", m_Order },
                    { "Type", tp },
                    { "Nullable", typeIsNullable }
                };

                //20190322 DKh inner schema
                if (typeof(Doc).IsAssignableFrom(this.NonNullableType))
                {
                    map["IsDataDoc"]   = true;
                    map["IsAmorphous"] = typeof(IAmorphousData).IsAssignableFrom(this.NonNullableType);
                    map["IsForm"]      = typeof(Form).IsAssignableFrom(this.NonNullableType);

                    if (typeof(TypedDoc).IsAssignableFrom(this.NonNullableType))
                    {
                        var innerSchema = Schema.GetForTypedDoc(this.NonNullableType);
                        if (innerSchema.Any(fd => typeof(TypedDoc).IsAssignableFrom(fd.Type)))
                        {
                            map["DataDocSchema"] = "@complex";
                        }
                        else
                        {
                            map["DataDocSchema"] = innerSchema;
                        }
                    }
                }

                if (attr != null)
                {
                    map.Add("IsKey", attr.Key);
                    map.Add("IsRequired", attr.Required);
                    map.Add("Visible", attr.Visible);
                    if (attr.Default != null)
                    {
                        map.Add("Default", attr.Default);
                    }
                    if (attr.CharCase != CharCase.AsIs)
                    {
                        map.Add("CharCase", attr.CharCase);
                    }
                    if (attr.Kind != DataKind.Text)
                    {
                        map.Add("Kind", attr.Kind);
                    }
                    if (attr.MinLength != 0)
                    {
                        map.Add("MinLen", attr.MinLength);
                    }
                    if (attr.MaxLength != 0)
                    {
                        map.Add("MaxLen", attr.MaxLength);
                    }
                    if (attr.Min != null)
                    {
                        map.Add("Min", attr.Min);
                    }
                    if (attr.Max != null)
                    {
                        map.Add("Max", attr.Max);
                    }
                    if (attr.ValueList != null)
                    {
                        map.Add("ValueList", attr.ValueList);
                    }
                    if (attr.Description != null)
                    {
                        map.Add("Description", attr.Description);
                    }
                    //metadata content is in the internal format and not dumped
                }

                JsonWriter.WriteMap(wri, map, nestingLevel, options);
            }
Exemple #3
0
        public FieldAttribute(
            Type protoType,
            string protoFieldName,             //Schema:Field
            string targetName    = ANY_TARGET,
            object storeFlag     = null,
            object key           = null,
            object kind          = null,
            object required      = null,
            object visible       = null,
            string valueList     = null,
            object dflt          = null,
            object min           = null,
            object max           = null,
            object minLength     = null,
            object maxLength     = null,
            object charCase      = null,
            string backendName   = null,
            string backendType   = null,
            string description   = null,
            string metadata      = null,
            object nonUI         = null,
            string formatRegExp  = null,
            string formatDescr   = null,
            string displayFormat = null,
            object isArow        = null
            ) : base(targetName, null)
        {
            if (protoType == null || protoFieldName.IsNullOrWhiteSpace())
            {
                throw new DataException(StringConsts.ARGUMENT_ERROR + "FieldAttr.ctor(protoType|protoFieldName=null|empty)");
            }
            try
            {
                var schema          = Schema.GetForTypedDoc(protoType);
                var protoTargetName = targetName;
                var segs            = protoFieldName.Split(':');
                if (segs.Length > 1)
                {
                    protoTargetName = segs[0].Trim();
                    protoFieldName  = segs[1].Trim();
                }
                if (protoTargetName.IsNullOrWhiteSpace())
                {
                    throw new Exception("Wrong target syntax");
                }
                if (protoFieldName.IsNullOrWhiteSpace())
                {
                    throw new Exception("Wrong field syntax");
                }

                var protoFieldDef = schema[protoFieldName];
                if (protoFieldDef == null)
                {
                    throw new Exception("Prototype '{0}' field '{1}' not found".Args(protoType.FullName, protoFieldName));
                }
                var protoAttr = protoFieldDef[protoTargetName];

                try
                {
                    StoreFlag         = storeFlag == null? protoAttr.StoreFlag   : (StoreFlag)storeFlag;
                    BackendName       = backendName == null? protoAttr.BackendName : backendName;
                    BackendType       = backendType == null? protoAttr.BackendType : backendType;
                    Key               = key == null? protoAttr.Key         : (bool)key;
                    Kind              = kind == null? protoAttr.Kind        : (DataKind)kind;
                    Required          = required == null? protoAttr.Required    : (bool)required;
                    Visible           = visible == null? protoAttr.Visible     : (bool)visible;
                    Min               = min == null? protoAttr.Min         : min;
                    Max               = max == null? protoAttr.Max         : max;
                    Default           = dflt == null? protoAttr.Default     : dflt;
                    MinLength         = minLength == null? protoAttr.MinLength   : (int)minLength;
                    MaxLength         = maxLength == null? protoAttr.MaxLength   : (int)maxLength;
                    CharCase          = charCase == null? protoAttr.CharCase    : (CharCase)charCase;
                    ValueList         = valueList == null? protoAttr.ValueList   : valueList;
                    Description       = description == null? protoAttr.Description : description;
                    NonUI             = nonUI == null? protoAttr.NonUI       : (bool)nonUI;
                    FormatRegExp      = formatRegExp == null? protoAttr.FormatRegExp: formatRegExp;
                    FormatDescription = formatDescr == null? protoAttr.FormatDescription: formatDescr;
                    DisplayFormat     = displayFormat == null? protoAttr.DisplayFormat : displayFormat;
                    IsArow            = isArow == null? protoAttr.IsArow        : (bool)isArow;



                    if (metadata.IsNullOrWhiteSpace())
                    {
                        m_MetadataContent = protoAttr.m_MetadataContent;
                    }
                    else
                    if (protoAttr.m_MetadataContent.IsNullOrWhiteSpace())
                    {
                        m_MetadataContent = metadata;
                    }
                    else
                    {
                        var conf1 = ParseMetadataContent(protoAttr.m_MetadataContent);
                        var conf2 = ParseMetadataContent(metadata);

                        var merged = new LaconicConfiguration();
                        merged.CreateFromMerge(conf1, conf2);
                        m_MetadataContent = merged.SaveToString();
                    }
                }
                catch (Exception err)
                {
                    throw new Exception("Invalid assignment of prototype override value: " + err.ToMessageWithType());
                }
            }
            catch (Exception error)
            {
                throw new DataException(StringConsts.CRUD_FIELD_ATTR_PROTOTYPE_CTOR_ERROR.Args(error.Message));
            }
        }
Exemple #4
0
        private Schema(Type tdoc)
        {
            lock (s_TypeLatch)
            {
                if (s_TypeLatch.Contains(tdoc))
                {
                    throw new DataException(StringConsts.CRUD_TYPED_DOC_RECURSIVE_FIELD_DEFINITION_ERROR.Args(tdoc.FullName));
                }

                s_TypeLatch.Add(tdoc);
                try
                {
                    m_Name = tdoc.AssemblyQualifiedName;

                    var tattrs = tdoc.GetCustomAttributes(typeof(SchemaAttribute), false).Cast <SchemaAttribute>();
                    tattrs.ForEach(a => a.StopPropAssignmentTracking());
                    m_SchemaAttrs = new List <SchemaAttribute>(tattrs);

                    //20191026 DKh. Expand resource references in Descriptions
                    m_SchemaAttrs.ForEach(a => { a.ExpandResourceReferencesRelativeTo(tdoc, null); a.Seal(); });

                    m_FieldDefs = new OrderedRegistry <FieldDef>();
                    var props = GetFieldMembers(tdoc);
                    var order = 0;
                    foreach (var prop in props)
                    {
                        var fattrs = prop.GetCustomAttributes(typeof(FieldAttribute), false)
                                     .Cast <FieldAttribute>()
                                     .ToArray();

                        fattrs.ForEach(a => a.StopPropAssignmentTracking());

                        //Interpret [Field(CloneFromType)]
                        for (var i = 0; i < fattrs.Length; i++)
                        {
                            var attr = fattrs[i];

                            if (attr.CloneFromDocType == null)
                            {
                                //20190831 DKh. Expand resource references in Descriptions
                                attr.ExpandResourceReferencesRelativeTo(tdoc, prop.Name);
                                continue;
                            }

                            if (fattrs.Length > 1)
                            {
                                throw new DataException(StringConsts.CRUD_TYPED_DOC_SINGLE_CLONED_FIELD_ERROR.Args(tdoc.FullName, prop.Name));
                            }

                            var clonedSchema = Schema.GetForTypedDoc(attr.CloneFromDocType);
                            var clonedDef    = clonedSchema[prop.Name];
                            if (clonedDef == null)
                            {
                                throw new DataException(StringConsts.CRUD_TYPED_DOC_CLONED_FIELD_NOTEXISTS_ERROR.Args(tdoc.FullName, prop.Name));
                            }

                            fattrs = clonedDef.Attrs.ToArray();//replace these attrs from the cloned target
                            break;
                        }

                        FieldAttribute.FixupInheritedTargets($"{tdoc.Name}.{prop.Name}", fattrs);

                        var fdef = new FieldDef(prop.Name, order, prop.PropertyType, fattrs, prop);
                        m_FieldDefs.Register(fdef);

                        order++;
                    }
                    s_TypedRegistry.Register(this);
                    m_TypedDocType = tdoc;
                }
                finally
                {
                    s_TypeLatch.Remove(tdoc);
                }
            }//lock
        }