Esempio n. 1
0
        public void Should_convert_for_value_conversion()
        {
            var field = Fields.Json(1, "json", Partitioning.Invariant);

            var source =
                new ContentFieldData()
                .AddJsonValue(JsonValue.Object());

            var result = FieldConverters.ForValues(ValueConverters.EncodeJson(TestUtils.DefaultSerializer))(source, field);

            var expected =
                new ContentFieldData()
                .AddValue("iv", "e30=");

            Assert.Equal(expected, result);
        }
Esempio n. 2
0
        public void Should_not_return_value_if_master_is_missing()
        {
            var field = Fields.String(1, "string", Partitioning.Language);

            var source =
                new ContentFieldData()
                .AddValue("de", "DE");

            var expected =
                new ContentFieldData()
                .AddValue("de", "DE");

            var result = FieldConverters.ResolveFallbackLanguages(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 3
0
        public void Should_resolve_invariant_and_use_direct_value()
        {
            var field = Fields.String(1, "string", Partitioning.Invariant);

            var source =
                new ContentFieldData()
                .AddValue("iv", "A");

            var expected =
                new ContentFieldData()
                .AddValue("iv", "A");

            var result = FieldConverters.ResolveInvariant(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 4
0
        public void Should_filter_languages()
        {
            var field = Fields.String(1, "string", Partitioning.Language);

            var source =
                new ContentFieldData()
                .AddValue("en", "EN")
                .AddValue("de", "DE");

            var expected =
                new ContentFieldData()
                .AddValue("de", "DE");

            var result = FieldConverters.FilterLanguages(languagesConfig, new[] { Language.DE })(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 5
0
        public void Should_resolve_languages_and_resolve_master_language_from_invariant()
        {
            var field = Fields.String(1, "string", Partitioning.Language);

            var source =
                new ContentFieldData()
                .AddValue("iv", "A")
                .AddValue("it", "B");

            var expected =
                new ContentFieldData()
                .AddValue("en", "A");

            var result = FieldConverters.ResolveLanguages(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 6
0
        private IEnumerable <FieldConverter> GenerateConverters(Context context, ValueConverter?cleanReferences)
        {
            if (!context.IsFrontendClient)
            {
                yield return(FieldConverters.ExcludeHidden());

                yield return(FieldConverters.ForNestedName2Name(ValueConverters.ExcludeHidden()));
            }

            yield return(FieldConverters.ExcludeChangedTypes());

            yield return(FieldConverters.ForNestedName2Name(ValueConverters.ExcludeChangedTypes()));

            if (cleanReferences != null)
            {
                yield return(FieldConverters.ForValues(cleanReferences));

                yield return(FieldConverters.ForNestedName2Name(cleanReferences));
            }

            yield return(FieldConverters.ResolveInvariant(context.App.LanguagesConfig));

            yield return(FieldConverters.ResolveLanguages(context.App.LanguagesConfig));

            if (!context.IsFrontendClient)
            {
                if (context.ShouldResolveLanguages())
                {
                    yield return(FieldConverters.ResolveFallbackLanguages(context.App.LanguagesConfig));
                }

                var languages = context.Languages();

                if (languages.Any())
                {
                    yield return(FieldConverters.FilterLanguages(context.App.LanguagesConfig, languages));
                }

                var assetUrls = context.AssetUrls();

                if (assetUrls.Any())
                {
                    yield return(FieldConverters.ResolveAssetUrls(assetUrls.ToList(), assetUrlGenerator));
                }
            }
        }
        public void Should_resolve_languages_and_cleanup_old_languages()
        {
            var field = Fields.String(1, "string", Partitioning.Language);

            var source =
                new ContentFieldData()
                .AddLocalized("en", "EN")
                .AddLocalized("it", "IT");

            var expected =
                new ContentFieldData()
                .AddLocalized("en", "EN");

            var result = FieldConverters.ResolveLanguages(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
        public void Should_resolve_invariant_from_first_language()
        {
            var field = Fields.String(1, "string", Partitioning.Invariant);

            var source =
                new ContentFieldData()
                .AddLocalized("de", "DE")
                .AddLocalized("it", "IT");

            var expected =
                new ContentFieldData()
                .AddInvariant("DE");

            var result = FieldConverters.ResolveInvariant(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
        public void Should_return_master_language_if_languages_to_filter_are_invalid()
        {
            var field = Fields.String(1, "string", Partitioning.Language);

            var source =
                new ContentFieldData()
                .AddLocalized("en", "EN")
                .AddLocalized("de", "DE");

            var expected =
                new ContentFieldData()
                .AddLocalized("en", "EN");

            var result = FieldConverters.FilterLanguages(languagesConfig, new[] { Language.CA })(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 10
0
        public void Should_cleanup_deleted_ids()
        {
            var id1 = DomainId.NewGuid();
            var id2 = DomainId.NewGuid();

            var source =
                new NamedContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array(id1, id2)))
                .AddField("assets",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array(id1)))
                .AddField("array",
                          new ContentFieldData()
                          .AddJsonValue(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id1, id2)))));

            var expected =
                new NamedContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array(id2)))
                .AddField("assets",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array()))
                .AddField("array",
                          new ContentFieldData()
                          .AddJsonValue(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id2)))));

            var cleaner = ValueReferencesConverter.CleanReferences(new HashSet <DomainId> {
                id2
            });
            var cleanNested = ValueConverters.ForNested(cleaner);

            var converter = FieldConverters.ForValues(cleaner, cleanNested);

            var actual = source.ConvertName2Name(schema, converter);

            Assert.Equal(expected, actual);
        }
Esempio n. 11
0
        public DataConverter(IJsonSerializer serializer)
        {
            decodeJsonConverters = new[]
            {
                FieldConverters.ForValues(
                    ValueConverters.DecodeJson(serializer)),
                FieldConverters.ForNestedId2Name(
                    ValueConverters.DecodeJson(serializer))
            };

            encodeJsonConverters = new[]
            {
                FieldConverters.ForValues(
                    ValueConverters.EncodeJson(serializer)),
                FieldConverters.ForNestedName2Id(
                    ValueConverters.EncodeJson(serializer))
            };
        }
Esempio n. 12
0
        public ConvertData(IUrlGenerator urlGenerator, IJsonSerializer jsonSerializer,
                           IAssetRepository assetRepository, IContentRepository contentRepository)
        {
            Guard.NotNull(urlGenerator, nameof(urlGenerator));
            Guard.NotNull(jsonSerializer, nameof(jsonSerializer));
            Guard.NotNull(assetRepository, nameof(assetRepository));
            Guard.NotNull(contentRepository, nameof(contentRepository));

            this.urlGenerator      = urlGenerator;
            this.assetRepository   = assetRepository;
            this.contentRepository = contentRepository;

            excludedChangedField = FieldConverters.ExcludeChangedTypes(jsonSerializer);
            excludedChangedValue = FieldConverters.ForValues(ValueConverters.ForNested(ValueConverters.ExcludeChangedTypes(jsonSerializer)));

            excludedHiddenField = FieldConverters.ExcludeHidden;
            excludedHiddenValue = FieldConverters.ForValues(ValueConverters.ForNested(ValueConverters.ExcludeHidden));
        }
Esempio n. 13
0
        public void Should_cleanup_deleted_ids()
        {
            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            var input =
                new IdContentData()
                .AddField(5,
                          new ContentFieldData()
                          .AddValue("iv", JsonValue.Array(id1.ToString(), id2.ToString())));

            var converter = FieldConverters.ForValues(ValueReferencesConverter.CleanReferences(new[] { id2 }));

            var actual = input.ConvertId2Id(schema, converter);

            var cleanedValue = (JsonArray)actual[5]["iv"];

            Assert.Equal(1, cleanedValue.Count);
            Assert.Equal(id1.ToString(), cleanedValue[0].ToString());
        }
        public void Should_convert_name_to_id()
        {
            var input =
                new NamedContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("en", "EN"))
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("iv", 1))
                .AddField("array",
                          new ContentFieldData()
                          .AddValue("iv",
                                    JsonValue.Array(
                                        JsonValue.Object()
                                        .Add("nested1", 100)
                                        .Add("nested2", 200)
                                        .Add("invalid", 300))))
                .AddField("invalid",
                          new ContentFieldData()
                          .AddValue("iv", 2));

            var hideRoot   = FieldConverters.ExcludeHidden;
            var hideNested = FieldConverters.ForValues(ValueConverters.ForNested(ValueConverters.ExcludeHidden));

            var actual = input.ConvertName2IdCloned(schema, hideRoot, hideNested);

            var expected =
                new IdContentData()
                .AddField(1,
                          new ContentFieldData()
                          .AddValue("en", "EN"))
                .AddField(7,
                          new ContentFieldData()
                          .AddValue("iv",
                                    JsonValue.Array(
                                        JsonValue.Object()
                                        .Add("72", 200))));

            Assert.Equal(expected, actual);
        }
Esempio n. 15
0
        public void Should_return_language_from_fallback_if_found()
        {
            var config_1 = languagesConfig.Set(new LanguageConfig(Language.IT));
            var config_2 = config_1.Set(new LanguageConfig(Language.ES, false, Language.IT));

            var source =
                new ContentFieldData()
                .AddValue("en", "EN")
                .AddValue("it", "IT");

            var expected =
                new ContentFieldData()
                .AddValue("en", "EN")
                .AddValue("de", "EN")
                .AddValue("it", "IT")
                .AddValue("es", "IT");

            var result = FieldConverters.ResolveFallbackLanguages(config_2)(source, stringLanguageField);

            Assert.Equal(expected, result);
        }
Esempio n. 16
0
        public void Should_convert_nested_asset_ids_to_urls_for_wildcard_fields()
        {
            var field =
                Fields.Array(1, "array", Partitioning.Invariant,
                             Fields.Assets(1, "assets"));

            var source =
                new ContentFieldData()
                .AddJsonValue(JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("assets", JsonValue.Array("1", "2"))));

            var expected =
                new ContentFieldData()
                .AddJsonValue(JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("assets", JsonValue.Array("url/to/1", "url/to/2"))));

            var result = FieldConverters.ResolveAssetUrls(new HashSet <string>(new[] { "*" }), assetUrlGenerator)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 17
0
        public void Should_convert_name_to_id()
        {
            var input =
                new ContentFieldData()
                .AddValue("iv",
                          new JArray(
                              new JObject(
                                  new JProperty("field1", 100),
                                  new JProperty("field2", 200),
                                  new JProperty("invalid", 300))));

            var actual = FieldConverters.ForNestedName2Id(ValueConverters.ExcludeHidden())(input, arrayField);

            var expected =
                new ContentFieldData()
                .AddValue("iv",
                          new JArray(
                              new JObject(
                                  new JProperty("1", 100))));

            Assert.Equal(expected, actual);
        }
Esempio n. 18
0
        public void Should_convert_id_to_id()
        {
            var input =
                new ContentFieldData()
                .AddValue("iv",
                          JsonValue.Array(
                              JsonValue.Object()
                              .Add("1", 100)
                              .Add("2", 200)
                              .Add("99", 300)));

            var actual = FieldConverters.ForNestedId2Id(ValueConverters.ExcludeHidden())(input, arrayField);

            var expected =
                new ContentFieldData()
                .AddValue("iv",
                          JsonValue.Array(
                              JsonValue.Object()
                              .Add("1", 100)));

            Assert.Equal(expected, actual);
        }
Esempio n. 19
0
        public void Should_convert_name_to_name()
        {
            var input =
                new ContentFieldData()
                .AddValue("iv",
                          JsonValue.Array(
                              JsonValue.Object()
                              .Add("field1", 100)
                              .Add("field2", 200)
                              .Add("invalid", 300)));

            var actual = FieldConverters.ForNestedName2Name(ValueConverters.ExcludeHidden())(input, arrayField);

            var expected =
                new ContentFieldData()
                .AddValue("iv",
                          JsonValue.Array(
                              JsonValue.Object()
                              .Add("field1", 100)));

            Assert.Equal(expected, actual);
        }
Esempio n. 20
0
        public void Should_resolve_invariant_from_master_language(IJsonValue?value)
        {
            var field = Fields.String(1, "string", Partitioning.Invariant);

            var source =
                new ContentFieldData()
                .AddLocalized("de", "DE")
                .AddLocalized("en", "EN");

            if (value != JsonValue.False)
            {
                source[InvariantPartitioning.Key] = value !;
            }

            var expected =
                new ContentFieldData()
                .AddInvariant("EN");

            var result = FieldConverters.ResolveInvariant(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 21
0
        public void Should_resolve_master_language_from_invariant(IJsonValue?value)
        {
            var field = Fields.String(1, "string", Partitioning.Language);

            var source =
                new ContentFieldData()
                .AddLocalized("iv", "A")
                .AddLocalized("it", "B");

            if (value != JsonValue.False)
            {
                source["en"] = value !;
            }

            var expected =
                new ContentFieldData()
                .AddLocalized("en", "A");

            var result = FieldConverters.ResolveLanguages(languagesConfig)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 22
0
        public void Should_convert_nested_asset_ids_to_urls()
        {
            var field =
                Fields.Array(1, "array", Partitioning.Invariant,
                             Fields.Assets(1, "assets"));

            var source =
                new ContentFieldData()
                .AddJsonValue(JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("assets", JsonValue.Array(id1, id2))));

            var expected =
                new ContentFieldData()
                .AddJsonValue(JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("assets", JsonValue.Array($"url/to/{id1}", $"url/to/{id2}"))));

            var result = FieldConverters.ResolveAssetUrls(new HashSet <string>(new[] { "array.assets" }), urlGenerato)(source, field);

            Assert.Equal(expected, result);
        }
Esempio n. 23
0
        public void RegisterPlugins(CSharpConverter converter)
        {
            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }
            ConvertBegin.Clear();
            AfterPreprocessing.Clear();
            GetCSharpNameResolvers.Clear();
            GetCSharpTypeResolvers.Clear();
            GetCSharpContainerResolvers.Clear();
            CompilationConverters.Clear();
            CommentConverters.Clear();
            EnumConverters.Clear();
            EnumItemConverters.Clear();
            ClassConverters.Clear();
            FieldConverters.Clear();
            FunctionConverters.Clear();
            FunctionTypeConverters.Clear();
            ParameterConverters.Clear();
            TypedefConverters.Clear();
            Converting.Clear();
            Converted.Clear();
            ConvertEnd.Clear();
            RegisteredPlugins.Clear();

            for (var index = Options.Plugins.Count - 1; index >= 0; index--)
            {
                var plugin = Options.Plugins[index];
                if (RegisteredPlugins.Contains(plugin))
                {
                    continue;
                }

                RegisteredPlugins.Add(plugin);
                plugin.Register(converter, this);
            }
        }
Esempio n. 24
0
        public void Should_apply_value_conversion_on_all_levels()
        {
            var source =
                new ContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(1, 2)))
                .AddField("assets1",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(1)))
                .AddField("array",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(1, 2)))))
                .AddField("component",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Object()
                              .Add("references",
                                   JsonValue.Array(1, 2))
                              .Add("assets1",
                                   JsonValue.Array(1))
                              .Add("array",
                                   JsonValue.Array(
                                       JsonValue.Object()
                                       .Add("nested", JsonValue.Array(1, 2))))
                              .Add(Component.Discriminator, DomainId.Empty)))
                .AddField("components",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("references",
                                       JsonValue.Array(1, 2))
                                  .Add("assets1",
                                       JsonValue.Array(1))
                                  .Add("array",
                                       JsonValue.Array(
                                           JsonValue.Object()
                                           .Add("nested", JsonValue.Array(1, 2))))
                                  .Add(Component.Discriminator, DomainId.Empty))));

            var expected =
                new ContentData()
                .AddField("references",
                          new ContentFieldData())
                .AddField("assets1",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(1)))
                .AddField("array",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object())))
                .AddField("component",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Object()
                              .Add("assets1",
                                   JsonValue.Array(1))
                              .Add("array",
                                   JsonValue.Array(
                                       JsonValue.Object()))
                              .Add(Component.Discriminator, DomainId.Empty)))
                .AddField("components",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("assets1",
                                       JsonValue.Array(1))
                                  .Add("array",
                                       JsonValue.Array(
                                           JsonValue.Object()))
                                  .Add(Component.Discriminator, DomainId.Empty))));

            var actual =
                source.Convert(schema,
                               FieldConverters.ForValues((data, field, parent) => field.Name != "assets1" ? null : data));

            Assert.Equal(expected, actual);
        }
Esempio n. 25
0
 public static IdContentData ToMongoModel(this NamedContentData result, Schema schema)
 {
     return(result.ToIdModel(schema, FieldConverters.EncodeJson()));
 }
Esempio n. 26
0
 public static NamedContentData FromMongoModel(this IdContentData result, Schema schema, List <Guid> deletedIds)
 {
     return(result.ToNameModel(schema, FieldConverters.DecodeJson(), FieldReferencesConverter.CleanReferences(deletedIds)));
 }
Esempio n. 27
0
        public void Should_cleanup_deleted_ids()
        {
            var id1 = DomainId.NewGuid();
            var id2 = DomainId.NewGuid();

            var source =
                new ContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(id1, id2)))
                .AddField("assets1",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(id1)))
                .AddField("array",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id1, id2)))))
                .AddField("component",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Object()
                              .Add("references",
                                   JsonValue.Array(id1, id2))
                              .Add("assets1",
                                   JsonValue.Array(id1))
                              .Add("array",
                                   JsonValue.Array(
                                       JsonValue.Object()
                                       .Add("nested", JsonValue.Array(id1, id2))))
                              .Add(Component.Discriminator, DomainId.Empty)))
                .AddField("components",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("references",
                                       JsonValue.Array(id1, id2))
                                  .Add("assets1",
                                       JsonValue.Array(id1))
                                  .Add("array",
                                       JsonValue.Array(
                                           JsonValue.Object()
                                           .Add("nested", JsonValue.Array(id1, id2))))
                                  .Add(Component.Discriminator, DomainId.Empty))));

            var expected =
                new ContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(id2)))
                .AddField("assets1",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array()))
                .AddField("array",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id2)))))
                .AddField("component",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Object()
                              .Add("references",
                                   JsonValue.Array(id2))
                              .Add("assets1",
                                   JsonValue.Array())
                              .Add("array",
                                   JsonValue.Array(
                                       JsonValue.Object()
                                       .Add("nested", JsonValue.Array(id2))))
                              .Add(Component.Discriminator, DomainId.Empty)))
                .AddField("components",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("references",
                                       JsonValue.Array(id2))
                                  .Add("assets1",
                                       JsonValue.Array())
                                  .Add("array",
                                       JsonValue.Array(
                                           JsonValue.Object()
                                           .Add("nested", JsonValue.Array(id2))))
                                  .Add(Component.Discriminator, DomainId.Empty))));

            var converter =
                FieldConverters.ForValues(
                    ValueReferencesConverter.CleanReferences(new HashSet <DomainId> {
                id2
            }));

            var actual = source.Convert(schema, converter);

            Assert.Equal(expected, actual);
        }