Esempio n. 1
0
        private GeneratorExecutionContext GenerateDecoders(GeneratorData message, FrameParameters param, GeneratorExecutionContext context)
        {
            var strBuilder    = new StringBuilder();
            var fieldsBuilder = new StringBuilder();
            var getMethod     = new StringBuilder();

            AppendDecoderTemplate(strBuilder, message, param.Namespace, message.Name);
            getMethod.AppendLine("            var reader = new MessageFrameReader(frame);");
            foreach (var fields in message.Params)
            {
                var fieldName = fields.Name;
                var msgType   = MessageTypeToString(fields.Type, fields.Signed);
                fieldsBuilder.AppendLine($"        public {msgType} {fields.Name} {{ get; set; }}");
                var readMethod = fields.Signed ? "GetSigned" : "GetUnsigned";
                getMethod.AppendLine($"            var {fieldName.ToLower()} = ({msgType}) reader.{readMethod}(MessageType.{fields.Type}, DataOrder.{fields.Order ?? DataOrder.Big}, DataTransformation.{fields.Transform ?? DataTransformation.None});");
            }

            var varToFieldMap = message.Params.Select(t => $"                {t.Name} = {t.Name.ToLower()},").ToList();

            getMethod.AppendLine($"            return new {message.Name} {{");
            foreach (var varToF in varToFieldMap)
            {
                getMethod.AppendLine(varToF);
            }
            getMethod.AppendLine("            };");
            strBuilder.Replace("{Fields}", fieldsBuilder.ToString());
            strBuilder.Replace("{GetMethod}", getMethod.ToString());
            context.AddSource($"{message.Name}.SourceGenerated.cs", SourceText.From(strBuilder.ToString(), Encoding.UTF8));
            return(context);
        }
Esempio n. 2
0
        private GeneratorExecutionContext GenerateEncoders(GeneratorData message, FrameParameters param, GeneratorExecutionContext context)
        {
            var strBuilder    = new StringBuilder();
            var fieldsBuilder = new StringBuilder();
            var getMethod     = new StringBuilder();

            AppendEncoderTemplate(strBuilder, param.Namespace, message.Name);

            getMethod.AppendLine($"            var bldr = new MessageFrameBuilder(alloc, {message.Id}, FrameType.{message.FrameType});");
            foreach (var fields in message.Params)
            {
                getMethod.AppendLine($"            bldr.Put(MessageType.{fields.Type}, DataOrder.{fields?.Order ?? DataOrder.Big}, DataTransformation.{fields?.Transform ?? DataTransformation.None}, {fields.Name});");
                fieldsBuilder.AppendLine($"        public {MessageTypeToString(fields.Type, fields.Signed)} {fields.Name} {{ get; set; }}");
            }
            getMethod.Append("            return bldr.ToMessageFrame();");
            strBuilder.Replace("{Fields}", fieldsBuilder.ToString());
            strBuilder.Replace("{GetMethod}", getMethod.ToString());
            context.AddSource($"{message.Name}.SourceGenerated.cs", SourceText.From(strBuilder.ToString(), Encoding.UTF8));
            return(context);
        }