Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use source generator instead of in-memory collection #179

Open
Marvin-Brouwer opened this issue Aug 13, 2022 Discussed in #69 · 2 comments
Open

Use source generator instead of in-memory collection #179

Marvin-Brouwer opened this issue Aug 13, 2022 Discussed in #69 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@Marvin-Brouwer
Copy link
Owner

Discussed in #69

Originally posted by Marvin-Brouwer February 9, 2022
We'd like to research the possibility of generating the JSON/XML/whatever mappings on build instead of reflecting this on startup.
For example, change this:

    public sealed class ProjectProfile : OpenAirSerializerProfile
    {
        public override void Configure()
        {
            For<Project>(
                attributeNamingStrategy: Names.Use.SnakeCase,
                tagNamingStrategy: Names.Use.PascalCase
            )
                .Child(project => project.Name)
                .Child(project => project.LastUpdate,
                    namingStrategy: Names.Equal("updated"),
                    converter: Converters.Use.OpenAirDate)
                .Child(project => project.Active,
                    converter: Converters.Use.StringBitBoolean)
                .Child(project => project.CustomDate,
                    namingStrategy: Names.Use.CustomFieldNames,
                    converter: Converters.Use.SimpleDate)
                .Child(project => project.RateCardId);
        }
    }

Into this:

public sealed class GeneratedSerializer {
    
    public string? Serialize(object? objectToSerialize) {
         if(object is null) return null)

         if(objectToSerialize is FullNamespace.Project)) return ProjectSerializer.Serialize(objectToSerialize);
         // ...
         
         throw new ClassMapNotFoundException(objectToSerialize.GetType());
    }
}
public static class ProjectSerializer {

    public static string? Serialize(Project objectToSerialize, ISerializerContext context) {
   
         const projectName = Names.Use.PascalCase(objectToSerialize.GetType(), context);
         var dataConatiner = new XObject(projectName,
                new XmlBuilder.Element(
                       Names.Use.SnakeCase(objectToSerialize.GetType().GetProperty(nameof(objectToSerialize.Name)), context),
                       Converters.Use.Convertible(objectToSerialize.Name, context)),
                new XmlBuilder.Element(
                       Names.Equal("updated")(nameof(objectToSerialize.LastUpdate), context),
                       Converters.Use.OpenAirDate(objectToSerialize.LastUpdate)),
                new XmlBuilder.Element(
                       Names.Use.SnakeCase(objectToSerialize.GetType().GetProperty(nameof(objectToSerialize.Active)), context),
                       Converters.Use.StringBitBoolean(objectToSerialize.Active, context)),
                new XmlBuilder.Element(
                       Names.Use.CustomFieldNames(objectToSerialize.GetType().GetProperty(nameof(objectToSerialize.CustomDate)), context),
                       Converters.Use.SimpleDate(objectToSerialize.CustomDate, context)),
                new XmlBuilder.Element(
                       Names.Use.SnakeCase(objectToSerialize.GetType().GetProperty(nameof(objectToSerialize.RateCardId)), context),
                       Converters.Use.Convertible(objectToSerialize.RateCardId.ToString(), context))
         );

        return dataContainer.ToString();
    }

}

Or something similar to that.

The question is, do we even want this?
Do we want to support this as an option or is it always on?


Additional notes:

These might be interesting libraries:

@Marvin-Brouwer Marvin-Brouwer added enhancement New feature or request help wanted Extra attention is needed question Further information is requested labels Aug 13, 2022
@Marvin-Brouwer
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant