コード例 #1
0
        private static T Use <T>(JSchema schema1, JSchema schema2, string propertyName,
                                 Func <JSchema, T> propertyAccess, SchemaComparisionReport report, Func <T, bool> hasValueCheck)
        {
            var val1 = propertyAccess(schema1);
            var val2 = propertyAccess(schema2);

            var hasValue1 = hasValueCheck(val1);
            var hasValue2 = hasValueCheck(val2);

            if (hasValue1 && hasValue2 && !val1.Equals(val2))
            {
                report.Push(new ValueConflict(propertyName, schema1, schema2, SchemaComparisionNotificationLevel.Failure)
                {
                    Value1 = val1,
                    Value2 = val2
                });
                return(default(T));
            }

            if (hasValue1)
            {
                return(val1);
            }

            if (hasValue2)
            {
                return(val2);
            }

            return(default(T));
        }
コード例 #2
0
        public async Task<JSchema> GetSchemaFromModuleAsync(ModuleDefinition module)
        {
            IEnumerable<TemplateInfo> templates;
            if (module.Skins != null)
                templates = module.Skins.Values;
            else
                templates = Enumerable.Empty<TemplateInfo>();

            if (module.DefaultTemplate != null)
                templates = templates.Union(new[] {module.DefaultTemplate});

            var enumerator = templates.GetEnumerator();
            if (!enumerator.MoveNext())
                return null;

            var result = await _schemaProvider.GetSchemaFromTemplateAsync(enumerator.Current).ConfigureAwait(false);
            
            var report = new SchemaComparisionReport();
	        while (enumerator.MoveNext())
	        {
				var schema = await _schemaProvider.GetSchemaFromTemplateAsync(enumerator.Current).ConfigureAwait(false);
				result = _schemaCombiner.Apply(result, schema, report);
	        }

            return result;
        }
コード例 #3
0
        public JSchema Apply(JSchema schema, JSchema baseSchema, SchemaComparisionReport report, string propertyName = null)
        {
            if (schema == null)
            {
                return(baseSchema);
            }

            if (baseSchema == null)
            {
                return(schema);
            }

            if (!string.IsNullOrEmpty(baseSchema.Title))
            {
                schema.Title = baseSchema.Title;
            }

            if (!string.IsNullOrEmpty(baseSchema.Format))
            {
                schema.Format = baseSchema.Format;
            }

            if (baseSchema.Type != null)
            {
                if (!CanConvert(schema.Type, baseSchema.Type))
                {
                    report.Push(new TypeChangeFailure(propertyName, schema, baseSchema));
                }
                else
                {
                    schema.Type = baseSchema.Type;
                }
            }

            if (schema.Properties != null)
            {
                foreach (var property in schema.Properties)
                {
                    if (baseSchema.Properties == null || !baseSchema.Properties.ContainsKey(property.Key))
                    {
                        report.Push(new MissingPropertyInfo(property.Key, schema, baseSchema));
                        continue;
                    }

                    var baseProperty = baseSchema.Properties[property.Key];
                    Apply(property.Value, baseProperty, report, property.Key);
                }
            }

            if (schema.Items != null && baseSchema.Items != null)
            {
                for (int i = 0; i < schema.Items.Count && i < baseSchema.Items.Count; i++)
                {
                    Apply(schema.Items[i], baseSchema.Items[i], report);
                }
            }

            return(schema);
        }
コード例 #4
0
ファイル: SchemaComparer.cs プロジェクト: namics/TerrificNet
        public JSchema Apply(JSchema schema, JSchema baseSchema, SchemaComparisionReport report, string propertyName = null)
        {
            if (schema == null)
                return baseSchema;

            if (baseSchema == null)
                return schema;

            if (!string.IsNullOrEmpty(baseSchema.Title))
                schema.Title = baseSchema.Title;

            if (!string.IsNullOrEmpty(baseSchema.Format))
                schema.Format = baseSchema.Format;

            if (baseSchema.Type != null)
            {
                if (!CanConvert(schema.Type, baseSchema.Type))
                    report.Push(new TypeChangeFailure(propertyName, schema, baseSchema));
                else
                    schema.Type = baseSchema.Type;
            }

            if (schema.Properties != null)
            {
                foreach (var property in schema.Properties)
                {
                    if (baseSchema.Properties == null || !baseSchema.Properties.ContainsKey(property.Key))
                    {
                        report.Push(new MissingPropertyInfo(property.Key, schema, baseSchema));
                        continue;
                    }

                    var baseProperty = baseSchema.Properties[property.Key];
                    Apply(property.Value, baseProperty, report, property.Key);
                }
            }

            if (schema.Items != null && baseSchema.Items != null)
            {
                for (int i = 0; i < schema.Items.Count && i < baseSchema.Items.Count; i++)
                {
                    Apply(schema.Items[i], baseSchema.Items[i], report);
                }
            }

            return schema;
        }
コード例 #5
0
        private void Proceed(JSchema otherSchema, SchemaComparisionReport report, IDictionary <string, JSchema> result, KeyValuePair <string, JSchema> propertyEntry)
        {
            if (!result.ContainsKey(propertyEntry.Key))
            {
                JSchema resultSchema;
                JSchema propSchema2;
                if (otherSchema.Properties.TryGetValue(propertyEntry.Key, out propSchema2))
                {
                    resultSchema = Apply(propertyEntry.Value, propSchema2, report, propertyEntry.Key);
                }
                else
                {
                    resultSchema = propertyEntry.Value;
                }

                result.Add(propertyEntry.Key, resultSchema);
            }
        }
コード例 #6
0
        private IEnumerable <JSchema> UseItems(JSchema schema1, JSchema schema2, SchemaComparisionReport report)
        {
            for (int i = 0; i < schema1.Items.Count || i < schema2.Items.Count; i++)
            {
                if (i < schema1.Items.Count && i < schema2.Items.Count)
                {
                    yield return(Apply(schema1.Items[i], schema2.Items[i], report));
                }

                else if (i < schema1.Items.Count)
                {
                    yield return(schema1.Items[i]);
                }

                else if (i < schema2.Items.Count)
                {
                    yield return(schema2.Items[i]);
                }
            }
        }
コード例 #7
0
        public virtual JSchema Apply(JSchema schema1, JSchema schema2, SchemaComparisionReport report,
                                     string propertyName = null)
        {
            if (schema1 == null)
            {
                throw new ArgumentNullException("schema1");
            }

            if (schema2 == null)
            {
                throw new ArgumentNullException("schema2");
            }

            if (report == null)
            {
                report = new SchemaComparisionReport();
            }

            var result = new JSchema
            {
                Title = Use(schema1, schema2, "title", s => s.Title, report),
                Type  = Use(schema1, schema2, "type", s => s.Type, report, v => v.HasValue)
            };

            foreach (var prop in UseProperties(schema1, schema2, report))
            {
                result.Properties.Add(prop);
            }

            foreach (var itemSchema in UseItems(schema1, schema2, report))
            {
                result.Items.Add(itemSchema);
            }

            return(result);
        }
コード例 #8
0
        private IDictionary <string, JSchema> UseProperties(JSchema schema1, JSchema schema2, SchemaComparisionReport report)
        {
            if (schema1.Properties == null)
            {
                return(schema2.Properties);
            }

            if (schema2.Properties == null)
            {
                return(schema2.Properties);
            }

            var result = new Dictionary <string, JSchema>();

            foreach (var propertyEntry in schema1.Properties)
            {
                Proceed(schema2, report, result, propertyEntry);
            }

            foreach (var propertyEntry in schema2.Properties)
            {
                Proceed(schema1, report, result, propertyEntry);
            }

            return(result);
        }
コード例 #9
0
 private static string Use(JSchema schema1, JSchema schema2, string propertyName, Func <JSchema, string> propertyAccess, SchemaComparisionReport report)
 {
     return(Use <string>(schema1, schema2, propertyName, propertyAccess, report, f => !string.IsNullOrEmpty(f)));
 }