reinforced / Reinforced.Typings

Converts C# classes to TypeScript interfaces (and many more) within project build. 0-dependency, minimal, gluten-free
MIT License
506 stars 82 forks source link

Improve Generators #202

Open tonyhallett opened 3 years ago

tonyhallett commented 3 years ago

It would be useful to allow ClassAndInterfaceGeneratorBase to supply code generators for members. I have a solution that I will provide through nuget shortly but for now the idea is a) Override GenerateMembers b) Implement GeneratorFor

protected override void GenerateMembers<T>(Type element, TypeResolver resolver, ITypeMember typeMember, IEnumerable<T> members)
        {
            foreach (var m in members)
            {
                var defaultGenerator = Generators.GeneratorFor(m);
                var generator = GeneratorFor(m, defaultGenerator.Context, resolver);
                if (generator == null)
                {
                    generator = defaultGenerator;
                }

                var member = generator.Generate(m, resolver);
                if (member != null) typeMember.Members.Add(member);
            }
        }

Also, the code below could be improved https://github.com/reinforced/Reinforced.Typings/blob/ef433b532a47f1fb91bc0312d351835398ac5c13/Reinforced.Typings/GeneratorManager.cs#L54

We can specify the code generator for a member directly on a member with attribute or fluent. Note that there is a mechanism in place to supply for all methods ( only ) through the TsClassAttribute only, not from the fluent API.


        public ITsCodeGenerator<T> GeneratorFor<T>(T member)
            where T : MemberInfo
        {
            var attr = _context.CurrentBlueprint.ForMember<TsTypedMemberAttributeBase>(member);
            var fromAttr = GetFromAttribute<T>(attr);
            if (fromAttr != null) return fromAttr;
            if (member is MethodInfo)
            {
                var classAttr = _context.CurrentBlueprint.Attr<TsClassAttribute>();
                if (classAttr != null && classAttr.DefaultMethodCodeGenerator != null)
                {
                    return LazilyInstantiateGenerator<T>(classAttr.DefaultMethodCodeGenerator);
                }
            }
            var gen = (ITsCodeGenerator<T>)_defaultGenerators[member.MemberType];
            gen.Context = _context;
            return gen;
        }
tonyhallett commented 3 years ago

It would be useful to allow ClassAndInterfaceGeneratorBase to supply code generators for members.

It is possible with

var attr = exportContext.CurrentBlueprint.ForMember...... attr.CodeGeneratorType = generatorType;