Example #1
0
        private static void SyncComponent(Component component, IComponentInfo schema)
        {
            component.Schema = schema;

            // Delete existing properties that don't have a corresponding definition.
            // and are not system properties (starting with $) or hidden ones (starting with _)
            component.Properties
                .Where(p => !p.Name.StartsWith("$") && !p.Name.StartsWith("_") && !schema.Properties.Any(info => info.Name == p.Name))
                .ToArray()
                .ForEach(p => p.Delete());

            // Initialize all the new properties. Existing ones are not modified.
            foreach (var propertySchema in schema.Properties)
            {
                var property = component.Properties.FirstOrDefault(x => x.Name == propertySchema.Name);
                if (property != null)
                {
                    property.Schema = propertySchema;
                }
                else
                {
                    property = component.CreateProperty(propertySchema.Name);
                    property.Schema = propertySchema;
                }

                // NOTE: unlike original NuPattern, we don't eagerly evaluate default values,
                // neither do we persist them.
            }
        }
        private void DeserializeComponent(Component component, JObject json)
        {
            var lineInfo = json as IJsonLineInfo;
            if (lineInfo != null && lineInfo.HasLineInfo())
                component.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);

            foreach (var jprop in json.Properties().Where(x =>
                !x.Name.StartsWith("$") && x.HasValues && x.Value.Type != JTokenType.Object))
            {
                var jvalue = jprop.Value as JValue;
                if (jvalue != null)
                {
                    var value = jvalue.Value;
                    var str = value as string;
                    Guid guid;
                    // See if it's a Guid.
                    if (str != null && str.StartsWith("{") && str.EndsWith("}") &&
                        Guid.TryParse(str, out guid))
                    {
                        value = guid;
                    }

                    var property = component.CreateProperty(jprop.Name);
                    property.Value = value;

                    lineInfo = jvalue as IJsonLineInfo;
                    if (lineInfo != null && lineInfo.HasLineInfo())
                        property.SetLineInfo(lineInfo.LineNumber, lineInfo.LinePosition);
                }
            }
        }