Пример #1
0
        public String GetSpecialProperties(ElementMetadata meta)
        {
            StringBuilder sb = new StringBuilder();

            if (!String.IsNullOrEmpty(meta.Id))
            {
                sb.Append($"$id: '{meta.Id}',");
            }
            if (!String.IsNullOrEmpty(meta.Name))
            {
                sb.Append($"$name: '{meta.Name}',");
            }
            if (!String.IsNullOrEmpty(meta.RowNumber))
            {
                sb.Append($"$rowNo: '{meta.RowNumber}',");
            }
            if (!String.IsNullOrEmpty(meta.HasChildren))
            {
                sb.Append($"$hasChildren: '{meta.HasChildren}',");
            }
            if (sb.Length == 0)
            {
                return(null);
            }
            sb.RemoveTailComma();
            return(",\n" + sb.ToString());
        }
        void ProcessComplexMetadata(FieldInfo fieldInfo, ElementMetadata elem, DataType dt)
        {
            // create metadata for nested type
            var innerElem = GetOrCreateMetadata(fieldInfo.TypeName);
            var fna       = fieldInfo.PropertyName.Split('.');

            if (fna.Length != 2)
            {
                throw new DataLoaderException($"Invalid complex name {fieldInfo.PropertyName}");
            }
            elem.AddField(new FieldInfo($"{fna[0]}!{fieldInfo.TypeName}"), DataType.Undefined);
            innerElem.AddField(new FieldInfo(fieldInfo, fna[1]), dt);
        }
        ElementMetadata GetOrCreateMetadata(String typeName)
        {
            if (_metadata == null)
            {
                _metadata = new Dictionary <String, ElementMetadata>();
            }
            ElementMetadata elemMeta;

            if (_metadata.TryGetValue(typeName, out elemMeta))
            {
                return(elemMeta);
            }
            elemMeta = new ElementMetadata();
            _metadata.Add(typeName, elemMeta);
            return(elemMeta);
        }
Пример #4
0
        public StringBuilder GetProperties(ElementMetadata meta)
        {
            var sb = new StringBuilder();

            foreach (var fd in meta.Fields)
            {
                var fm = fd.Value;
                sb.AppendLine()
                .Append(fd.Key)
                .Append(':');
                // TODO: special data type
                if (fm.ItemType == FieldType.Array)
                {
                    sb.Append(fm.RefObject + "Array");
                }
                else if (fm.ItemType == FieldType.Tree)
                {
                    sb.Append(fm.RefObject + "Array");
                }
                else if (fm.ItemType == FieldType.Object)
                {
                    sb.Append(fm.RefObject);
                }
                else if (fm.DataType == DataType.Undefined)
                {
                    throw new DataLoaderException($"Invalid data type for {meta.Name}.{fd.Key}");
                }
                else
                {
                    sb.Append(fm.DataType);
                }
                sb.Append(",");
            }
            if (sb.Length == 0)
            {
                return(sb);
            }
            sb.RemoveTailComma();
            return(sb);
        }
Пример #5
0
        public StringBuilder GetOneConstructor(String name, ElementMetadata ctor)
        {
            var    sb      = new StringBuilder();
            String arrItem = ctor.IsArrayType ? "true" : "false";

            sb.AppendLine($"function {name}(source, path, parent) {{")
            .AppendLine("\tcmn.createObject(this, source, path, parent);")
            .AppendLine("}")
            // metadata
            .Append($"cmn.defineObject({name}, {{props: {{")
            .Append(GetProperties(ctor))
            .Append("}")
            .Append(GetSpecialProperties(ctor))
            .AppendLine($"}}, {arrItem});");

            if (ctor.IsArrayType)
            {
                sb.AppendLine($"function {name}Array(source, path, parent) {{")
                .AppendLine($"return cmn.createArray(source, path, {name}, {name}Array, parent);")
                .AppendLine("}");
            }
            return(sb);
        }