Example #1
0
 //--- Class Methods ---
 public static DeserializerBuilder WithCloudFormationFunctions(this DeserializerBuilder builder)
 {
     foreach (var tag in CloudFormationFunctionNodeDeserializer.SupportedTags)
     {
         builder = builder.WithTagMapping(tag, typeof(CloudFormationListFunction));
     }
     return(builder
            .WithTagMapping("!Transform", typeof(CloudFormationMapFunction)));
 }
Example #2
0
        /// <summary>
        /// Generates a deserializer suitable for deserialzing <see cref="RuleSet"/> for use with this class (see also <see cref="LoadRules(string)"/>)
        /// </summary>
        /// <returns></returns>
        public static IDeserializer GetDeserializer()
        {
            var builder = new DeserializerBuilder();

            builder.WithTagMapping("!SocketRule", typeof(SocketRule));
            builder.WithTagMapping("!WhiteListRule", typeof(WhiteListRule));
            builder.WithTagMapping("!IsIdentifiableRule", typeof(IsIdentifiableRule));

            return(builder.Build());
        }
Example #3
0
        protected static Item ParseYamlMarkdown(string yamlmd)
        {
            var builder = new DeserializerBuilder();

            foreach (var mapping in ClassMapping)
            {
                builder = builder.WithTagMapping($"!{mapping.Key}", mapping.Value);
            }
            var yamlDeserializer = builder
                                   .WithNamingConvention(PascalCaseNamingConvention.Instance)
                                   .Build();

            var parser = new Parser(new System.IO.StringReader(yamlmd));

            parser.Consume <StreamStart>();
            parser.Consume <DocumentStart>();
            var post = yamlDeserializer.Deserialize(parser);

            parser.Consume <DocumentEnd>();
            //parser.MoveNext();
            var item = post as Item;

            item.Markdown = yamlmd.Substring(parser.Current.End.Index + 1);
            return(post as Item);
        }
        public static IDeserializer GetDeserializer(string filePath = null)
        {
            // if (_desrializer != null) {
            //     return _desrializer;
            // }
            var builder = new DeserializerBuilder();

            builder = builder
                      .IgnoreFields()
                      .IgnoreUnmatchedProperties()
                      .WithNamingConvention(CamelCaseNamingConvention.Instance);
            if (RegisteredTags?.Count > 0)
            {
                foreach (var t in RegisteredTags)
                {
                    var type   = t.GetType();
                    var tagKey = type.GetCustomAttributes(typeof(YamlTagAttribute), true)
                                 .FirstOrDefault() is YamlTagAttribute att
                        ? att.TagName
                        : null;

                    builder = builder.WithTagMapping($"!{tagKey}", type);
                }

                builder = builder
                          .WithTypeConverter(
                    new FileTypeConverter(RegisteredTags, filePath),
                    s => s.OnTop());
            }

            // _desrializer = builder.Build();
            // return _desrializer;
            return(builder.Build());
        }
Example #5
0
        public void NotSpecifyingObjectFactoryUsesDefault()
        {
            var text = "!empty {}";

            DeserializerBuilder.WithTagMapping("!empty", typeof(EmptyBase));
            var result = Deserializer.Deserialize(UsingReaderFor(text));

            result.Should().BeOfType <EmptyBase>();
        }
Example #6
0
        public void ObjectFactoryIsInvoked()
        {
            AssumingDeserializerWith(new LambdaObjectFactory(t => new EmptyDerived()));
            var text = "!empty {}";

            DeserializerBuilder.WithTagMapping("!empty", typeof(EmptyBase));
            var result = Deserializer.Deserialize(UsingReaderFor(text));

            result.Should().BeOfType <EmptyDerived>();
        }
Example #7
0
        static void Main(string[] args)
        {
            // Build YAML Parser
            var listOfAbilities = (
                from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                from assemblyType in domainAssembly.GetTypes()
                where assemblyType.IsSubclassOf(typeof(Ability)) && !assemblyType.IsAbstract
                select assemblyType
                ).ToArray();
            var listOfRaces = (
                from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                from assemblyType in domainAssembly.GetTypes()
                where assemblyType.IsSubclassOf(typeof(Race)) && !assemblyType.IsAbstract
                select assemblyType
                ).ToArray();
            var deserializerBuilder = new DeserializerBuilder()
                                      .WithNamingConvention(CamelCaseNamingConvention.Instance);

            foreach (var type in listOfAbilities.Concat(listOfRaces))
            {
                deserializerBuilder.WithTagMapping("!" + type.Name, type);
            }
            var deserializer = deserializerBuilder.Build();

            // Load Parties
            List <Party> parties = new List <Party>();

            foreach (var arg in args)
            {
                string input = System.IO.File.ReadAllText(arg);
                parties.Add(deserializer.Deserialize <Party>(input));
            }

            if (!IsValid(parties))
            {
                Console.WriteLine("One or more parties are not valid. Exiting.");
                return;
            }


            // Pick combat type
            if (parties.Count == 2)
            {
                Duel(parties);
            }
            else if (parties.Count > 2)
            {
                Brawl(parties);
            }
            else
            {
                Console.WriteLine("You must run this with at least 2 parties");
            }
        }
Example #8
0
        public void DeserializeCustomTags()
        {
            var stream = Yaml.StreamFrom("tags.yaml");

            DeserializerBuilder.WithTagMapping("tag:yaml.org,2002:point", typeof(Point));
            var result = Deserializer.Deserialize(stream);

            result.Should().BeOfType <Point>().And
            .Subject.As <Point>()
            .ShouldBeEquivalentTo(new { X = 10, Y = 20 }, o => o.ExcludingMissingMembers());
        }
        /// <summary>
        /// Converts the input RAML stream to an Open API Specification document.
        /// </summary>
        /// <param name="inputPath">The path to the RAML file.</param>
        public OpenApiDocument ConvertToOpenApiDocument(string inputPath)
        {
            var builder = new DeserializerBuilder();

            var includeNodeDeserializer = new YamlIncludeNodeDeserializer(new YamlIncludeNodeDeserializerOptions
            {
                DirectoryName = Path.GetDirectoryName(inputPath),
                Deserializer  = builder.Build()
            });

            _deserializer = builder
                            .WithTagMapping(Constants.IncludeTag, typeof(IncludeRef))
                            .WithNodeDeserializer(includeNodeDeserializer, s => s.OnTop())
                            .Build();

            var result = _deserializer.Deserialize <Dictionary <object, object> >(File.ReadAllText(inputPath));

            // Step 1 - Get all types and schemas
            var types = result.GetAsDictionary("types");

            if (types != null)
            {
                foreach (var type in types.Where(x => !_types.ContainsKey(x.Key)))
                {
                    _types.Add(type.Key, type.Value);
                }
            }
            var schemas = result.GetAsDictionary("schemas");

            if (schemas != null)
            {
                foreach (var schema in schemas.Where(x => !_types.ContainsKey(x.Key)))
                {
                    _types.Add(schema.Key, schema.Value);
                }
            }

            // Step 2 - Get Info, Servers and Components
            _doc = new OpenApiDocument
            {
                Info       = MapInfo(result),
                Servers    = MapServers(result),
                Components = MapComponents(_types)
            };

            // Step 3 - Get Paths
            _doc.Paths = MapPaths(result);

            // Check if valid
            var text = _doc.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);

            return(_doc);
        }
Example #10
0
 public static T Parse <T>(string readText, string debugFilename, ErrorHandler handle_error = null, List <Tuple <string, Type> > tagMappings = null)
 {
     try
     {
         if (handle_error == null)
         {
             handle_error = LogError;
         }
         readText = readText.Replace("\t", "    ");
         Action <string> unmatchedLogFn = delegate(string error)
         {
             handle_error(new Error
             {
                 file = new FileHandle
                 {
                     full_path = debugFilename
                 },
                 text     = readText,
                 message  = error,
                 severity = Error.Severity.Recoverable
             }, false);
         };
         DeserializerBuilder deserializerBuilder = new DeserializerBuilder();
         deserializerBuilder.IgnoreUnmatchedProperties(unmatchedLogFn);
         if (tagMappings != null)
         {
             foreach (Tuple <string, Type> tagMapping in tagMappings)
             {
                 deserializerBuilder = deserializerBuilder.WithTagMapping(tagMapping.first, tagMapping.second);
             }
         }
         Deserializer deserializer = deserializerBuilder.Build();
         StringReader input        = new StringReader(readText);
         return(deserializer.Deserialize <T>(input));
     }
     catch (Exception ex)
     {
         handle_error(new Error
         {
             file = new FileHandle
             {
                 full_path = debugFilename
             },
             text            = readText,
             message         = ex.Message,
             inner_exception = ex.InnerException,
             severity        = Error.Severity.Fatal
         }, false);
     }
     return(default(T));
 }
Example #11
0
        internal Deserializer CreateDefaultDeserializer()
        {
            var mappings = this.moduleFinder.GetAllModulesForModulesNamespace();

            var builder = new DeserializerBuilder();

            foreach (var mapping in mappings)
            {
                System.Diagnostics.Debug.WriteLine($"Added custom tag for yaml mapping: {mapping.Name} - {mapping.Type.Name}");
                builder = builder.WithTagMapping(mapping.Name, mapping.Type);
            }

            return(builder.Build());
        }
Example #12
0
        public static IDeserializer Build(string directoryName)
        {
            var builder = new DeserializerBuilder();

            var includeNodeDeserializerOptions = new YamlIncludeNodeDeserializerOptions
            {
                DirectoryName = directoryName
            };

            var includeNodeDeserializer = new YamlIncludeNodeDeserializer(includeNodeDeserializerOptions);

            return(builder
                   .WithTagMapping(string.Empty, typeof(IncludeRef))
                   .WithTagMapping(Constants.IncludeTag, typeof(IncludeRef))
                   .WithNodeDeserializer(includeNodeDeserializer, s => s.OnTop())
                   .Build());
        }
Example #13
0
        public SequencePersistence()
        {
            var deserializerBuilder = new DeserializerBuilder();

            deserializerBuilder
            .WithTagMapping("tag:yaml.org,2002:abort", typeof(AbortCondition))
            .WithTagMapping("tag:yaml.org,2002:output", typeof(OutputEvent))
            .WithNamingConvention(new CamelCaseNamingConvention());
            deserializer = deserializerBuilder.Build();

            var serializerBuilder = new SerializerBuilder();

            serializerBuilder
            .WithTagMapping("tag:yaml.org,2002:abort", typeof(AbortCondition))
            .WithTagMapping("tag:yaml.org,2002:output", typeof(OutputEvent))
            .WithNamingConvention(new CamelCaseNamingConvention());
            serializer = serializerBuilder.Build();
        }
Example #14
0
        public static PhotonViewPrefabMaster Load(TextReader reader)
        {
            var views = Assembly.GetExecutingAssembly().GetTypes()
                        .Where(typeof(PhotonViewComponent).IsAssignableFrom);

            var builder = new DeserializerBuilder();

            foreach (var view in views)
            {
                builder.WithTagMapping($"!{view.Name}", view);
            }
            var deserializer = builder.Build();

            var photonViews = deserializer.Deserialize <PhotonView[]>(reader)
                              .ToDictionary(e => e.Name);

            return(new PhotonViewPrefabMaster(photonViews));
        }
Example #15
0
        public YamlFormatter(IEnumerable <Type> knownTypes) : base(knownTypes)
        {
            SerializerBuilder serializerBuilder = new SerializerBuilder()
                                                  .WithNamingConvention(PascalCaseNamingConvention.Instance)
                                                  .EnsureRoundtrip()
                                                  .IgnoreFields();
            DeserializerBuilder deserializerBuilder = new DeserializerBuilder()
                                                      .WithNamingConvention(PascalCaseNamingConvention.Instance)
                                                      .IgnoreUnmatchedProperties()
                                                      .IgnoreFields();

            foreach (Type type in KnownTypes)
            {
                serializerBuilder   = serializerBuilder.WithTagMapping($"!{type.FullName}", type);
                deserializerBuilder = deserializerBuilder.WithTagMapping($"!{type.FullName}", type);
            }
            _serializer   = serializerBuilder.Build();
            _deserializer = deserializerBuilder.Build();
        }
Example #16
0
        public ConfigService()
        {
            config = new Config
            {
                Channels = new System.Collections.ObjectModel.ObservableCollection <Channel>()
            };

            var deserializerBuilder = new DeserializerBuilder();

            deserializerBuilder
            .WithTagMapping("tag:yaml.org,2002:input", typeof(InputChannel))
            .WithNamingConvention(new CamelCaseNamingConvention());
            deserializer = deserializerBuilder.Build();

            var serializerBuilder = new SerializerBuilder();

            serializerBuilder
            .WithTagMapping("tag:yaml.org,2002:input", typeof(InputChannel))
            .WithNamingConvention(new CamelCaseNamingConvention());
            serializer = serializerBuilder.Build();
        }
Example #17
0
        public static MapConfig ReadFromString(string yamlString, IEnumerable <Type> customCommands)
        {
            var commandMapping = new Dictionary <string, Type>()
            {
                { CommandUtils.GetCommandYamlName(typeof(ReplaceCommandSet)), typeof(ReplaceCommandSet) },
                { CommandUtils.GetCommandYamlName(typeof(SetFlowCommand)), typeof(SetFlowCommand) },
                { CommandUtils.GetCommandYamlName(typeof(LookupCommand)), typeof(LookupCommand) },
                { CommandUtils.GetCommandYamlName(typeof(TypeConvertCommand)), typeof(TypeConvertCommand) },
                { CommandUtils.GetCommandYamlName(typeof(SetCommand)), typeof(SetCommand) },
                { CommandUtils.GetCommandYamlName(typeof(ConcatCommand)), typeof(ConcatCommand) },
                { CommandUtils.GetCommandYamlName(typeof(GetValueCommand)), typeof(GetValueCommand) },
                { CommandUtils.GetCommandYamlName(typeof(GetNotEmptyValueCommand)), typeof(GetNotEmptyValueCommand) },
                { CommandUtils.GetCommandYamlName(typeof(IfCommand)), typeof(IfCommand) },
                { CommandUtils.GetCommandYamlName(typeof(TraceCommand)), typeof(TraceCommand) },
                { CommandUtils.GetCommandYamlName(typeof(CommandSet <CommandBase>)), typeof(CommandSet <CommandBase>) },
                { CommandUtils.GetCommandYamlName(typeof(GetTargetCommand)), typeof(GetTargetCommand) },
                { CommandUtils.GetCommandYamlName(typeof(MessageCommand)), typeof(MessageCommand) },

                { "csv", typeof(CsvDataSource) },
                { "composite-source", typeof(CompositeDataSource) },
                { "excel", typeof(ExcelDataSource) },
                { "sql", typeof(SqlDataSource) },
            };

            var builder = new DeserializerBuilder();

            customCommands
            .Select(type => new KeyValuePair <string, Type>(type.Name, type))
            .Union(commandMapping)
            .ToList()
            .ForEach(type => builder = builder.WithTagMapping("!" + type.Key, type.Value));

            var deserializer = builder.Build();

            var mapConfig = deserializer.Deserialize <MapConfig>(yamlString);

            mapConfig.Initialize();

            return(mapConfig);
        }