Exemplo n.º 1
0
        public static IContentSchema AddField(this IContentSchema schema, string name, Type fieldType, ContentFieldOptions options, int?sortkey = null)
        {
            schema.Fields.Add(name, new ContentSchemaField(ContentFieldManager.Default.GetContentFieldName(fieldType), options)
            {
                SortKey = sortkey ?? 0, Options = options
            });

            return(schema);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Заменить повторяющиеся объекты <see cref="ContentSchema"/> на ссылки <see cref="ContentSchemaJsonRef"/>
        /// </summary>
        private IContentSchema DeduplicateContentSchema(
            IContentSchema schema, SchemaContext context, HashSet <ContentSchema> visitedSchemas)
        {
            if (schema is ContentSchema contentSchema)
            {
                if (visitedSchemas.Contains(contentSchema))
                {
                    return(GetSchemaRef(contentSchema, context));
                }

                visitedSchemas.Add(contentSchema);

                foreach (FieldSchema fieldSchema in contentSchema.Fields.Values)
                {
                    if (fieldSchema is RelationFieldSchema relationSchema)
                    {
                        relationSchema.RelatedContent = DeduplicateContentSchema(
                            relationSchema.RelatedContent, context, visitedSchemas);
                    }
                    else if (fieldSchema is ExtensionFieldSchema extensionSchema)
                    {
                        foreach (var pair in extensionSchema.ExtensionContents.ToArray())
                        {
                            extensionSchema.ExtensionContents[pair.Key] = DeduplicateContentSchema(
                                pair.Value, context, visitedSchemas);
                        }
                    }
                }

                if (context.RepeatedSchemas.Contains(contentSchema))
                {
                    return(GetSchemaRef(contentSchema, context));
                }
            }

            return(schema);
        }
Exemplo n.º 3
0
        public static void FromBsonValue(this BsonValue bsonValue, string fieldName, IContentElement contentItem, IContentSchema schema)
        {
            if (bsonValue == BsonNull.Value)
            {
                return;
            }

            if (contentItem.Fields.TryGetValue(fieldName, out ContentField? contentField))
            {
                if (schema.Fields.TryGetValue(fieldName, out ContentSchemaField? schemaField))
                {
                    IFieldSerializer fieldSerializer = MongoFieldManager.Default.GetByType(contentField.GetType());

                    fieldSerializer.Read(schemaField, contentField, bsonValue);
                }
            }
        }
Exemplo n.º 4
0
        public static void FromRestValue(this JToken bsonValue, string fieldName, IContentElement contentItem, IContentSchema schema)
        {
            if (contentItem.Fields.TryGetValue(fieldName, out ContentField contentField))
            {
                switch (bsonValue.Type)
                {
                case JTokenType.Null:
                    break;

                case JTokenType.String:
                    if (contentField is SingleValueContentField <string> stringField)
                    {
                        stringField.Value = bsonValue.Value <string>();
                    }
                    else if (contentField is SingleValueContentField <Guid?> guidField)
                    {
                        guidField.Value = Guid.Parse(bsonValue.Value <string>());
                    }
                    break;

                case JTokenType.Integer:
                    if (contentField is SingleValueContentField <short?> shortField)
                    {
                        shortField.Value = bsonValue.Value <short?>();
                    }
                    else if (contentField is SingleValueContentField <int?> intField)
                    {
                        intField.Value = bsonValue.Value <int?>();
                    }
                    else if (contentField is SingleValueContentField <long?> longField)
                    {
                        longField.Value = bsonValue.Value <long?>();
                    }
                    break;

                case JTokenType.Float:
                    ((SingleValueContentField <double?>)contentField).Value = bsonValue.Value <double>();
                    break;

                case JTokenType.Boolean:
                    ((SingleValueContentField <bool?>)contentField).Value = bsonValue.Value <bool>();
                    break;

                //case JTokenType.Guid:
                //    ((SingleContentField<Guid?>)contentField).Value = bsonValue.Value<Guid>();
                //    break;

                case JTokenType.Array:
                    if (contentField is ArrayField arrayField)
                    {
                        if (schema.Fields.TryGetValue(fieldName, out ContentSchemaField definition))
                        {
                            ArrayFieldOptions arrayOptions = definition.Options as ArrayFieldOptions;

                            foreach (JObject item in bsonValue)
                            {
                                ArrayFieldItem arrayFieldItem = arrayOptions.CreateArrayField();

                                foreach (var subitem in item)
                                {
                                    FromRestValue(subitem.Value, subitem.Key, arrayFieldItem, arrayOptions);
                                }

                                arrayField.Items.Add(arrayFieldItem);
                            }
                        }
                    }
                    break;

                case JTokenType.Object:
                    if (contentField is ReferenceField referenceField)
                    {
                        if (bsonValue.HasValues)
                        {
                            RestContentItem restContentItem = bsonValue.ToObject <RestContentItem>(NewtonJsonExtensions.CreateSerializer());
                            referenceField.ContentItem = restContentItem.ToModel();
                        }
                    }
                    else if (contentField is EmbedField embedField)
                    {
                        RestContentEmbedded restContentItem = bsonValue.ToObject <RestContentEmbedded>(NewtonJsonExtensions.CreateSerializer());
                        embedField.ContentEmbedded = restContentItem.ToModel();
                    }
                    else if (contentField is AssetField assetField)
                    {
                        if (bsonValue.HasValues)
                        {
                            RestAsset restAsset = bsonValue.ToObject <RestAsset>();
                            assetField.Asset = restAsset.ToModel();
                        }
                    }
                    else
                    {
                        ContentField c = (ContentField)bsonValue.ToObject(contentField.GetType(), NewtonJsonExtensions.CreateSerializer());

                        contentItem.Fields[fieldName] = c;
                    }
                    break;
                }
            }
        }
        public static TContentItem AddArrayFieldItem <TContentItem>(this TContentItem contentItem, string name, IContentSchema schema, Action <ArrayFieldItem> action)
            where TContentItem : IContentElement
        {
            ArrayFieldOptions options = schema.GetArrayFieldOptions(name);
            ArrayFieldItem    item    = options.CreateArrayField();

            action(item);

            return(AddArrayFieldItem(contentItem, name, item));
        }
Exemplo n.º 6
0
 public static ArrayFieldOptions GetArrayFieldOptions(this IContentSchema schema, string name)
 {
     return(schema.Fields[name].GetArrayFieldOptions());
 }