public void Execute(GeneratorExecutionContext context)
 {
     try
     {
         DangerousExecute(context);
     }
     catch (Exception e)
     {
         DiagnosticHelper.ReportDiagnostic(context, DiagnosticSeverity.Error, $"Source generation error: {e.Message} {e.StackTrace}");
     }
 }
Beispiel #2
0
        public override bool ModifyCollectionPrefixSerialization(MethodBuildingContext context)
        {
            if (!context.MethodsRegistry.TryGetWriteMethod(context.Property.CloneWithType(Type), out Method writeMethod))
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "There is no write method for this type, attribute will be ignored.", syntax);
                return(false);
            }

            context.CodeBuilder.Line($"{context.StreamName}.{writeMethod}(({Type}){context.DataName}.{context.Property.Length});");
            return(true);
        }
Beispiel #3
0
        public override bool ModifyCollectionPrefixDeserialization(MethodBuildingContext context)
        {
            if (Length < 0)
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "Length must be a non-negative number. Attribute will be ignored.", syntax);
                return(false);
            }

            context.CodeBuilder.Line($"{context.DataName} = {context.Property.NewCollection(Length.ToString())}");
            return(true);
        }
Beispiel #4
0
        public override bool ModifyCollectionPrefixSerialization(MethodBuildingContext context)
        {
            if (Length < 0)
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "Length must be a non-negative number. Attribute will be ignored.", syntax);
                return(false);
            }

            // Doesn't write length prefix
            return(true);
        }
Beispiel #5
0
        public override bool ModifyCollectionPrefixDeserialization(MethodBuildingContext context)
        {
            if (!context.MethodsRegistry.TryGetReadMethod(context.Property.CloneWithType(Type), out Method readMethod))
            {
                DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "There is no read method for this type, attribute will be ignored.", syntax);
                return(false);
            }

            string getLength = $"{context.StreamName}.{readMethod}()";

            context.CodeBuilder.Line($"{context.DataName} = {context.Property.NewCollection(getLength)};");
            return(true);
        }
    public override bool ModifyDeserialization(MethodBuildingContext context)
    {
        if (!context.MethodsRegistry.TryGetReadMethod(context.Property.CloneWithType(Type), out Method readMethod))
        {
            DiagnosticHelper.ReportDiagnostic(context.GeneratorContext, DiagnosticSeverity.Warning, "There is no read method for this type, attribute will be ignored.", syntax);
            return(false);
        }

        if (context.Property.IsGeneric)
        {
            context.CodeBuilder.Line($"var temp = {context.StreamName}.{readMethod}();");
            context.CodeBuilder.Line($"{context.DataName} = Unsafe.As<{readMethod.Type}, {context.Property.Type}>(ref temp);");
        }
        else
        {
            context.CodeBuilder.Line($"{context.DataName} = ({context.Property.Type}){context.StreamName}.{readMethod}();");
        }
        return(true);
    }