Code Monkey home page Code Monkey logo

Comments (8)

vmandic avatar vmandic commented on August 25, 2024 1

Thanks for the idea @yegorpetrov, the proposed solution is OK but the c.Order(orderFunc(c.Type)) call does not work as expected (I am using the latest tool version of 1.4.6), i.e. the reflected/translated code is not in the order it should be...

I made a bit more verbose version of the hierarchy enumeration with a Type dict like so:

    public static void Configure(ConfigurationBuilder builder)
    {
      var types = typeof(Class1).Assembly.GetTypes().ToList();

      var orderedTypes = new Dictionary<Type, List<Type>>();
      types.ForEach(t => orderedTypes.Add(t, EnumerateHierarchy(t, e => e.BaseType).Reverse().ToList()));
      builder.ExportAsClasses(types, c => c.Order(orderedTypes[c.Type].IndexOf(c.Type)));
    }

    static IEnumerable<T> EnumerateHierarchy<T>(T item, Func<T, T> selector) where T : class
    {
      do
      {
        yield return item;
        item = selector(item);
      }
      while (item != default(T));
    }

... I tested this separately on three class files (Class1, Class2, Class3) from three different Namespaces like so:

namespace ReinforcedTypings.DifferentNamespaces.Ns1
{
  public class Class1
  {
    public int AnIntegerPropNs1 { get; set; }
    public string AStringPropNs1 { get; set; }
  }
}

namespace ReinforcedTypings.DifferentNamespaces.Ns2
{
  public class Class2 : Class1
  {
    public int AnIntegerPropNs2 { get; set; }
    public string AStringPropNs2 { get; set; }
  }
}

namespace ReinforcedTypings.DifferentNamespaces.Ns3
{
  public class Class3
  {
    public int AnIntegerPropNs3 { get; set; }
    public Class1 AClass1PropNs3 { get; set; }
  }
}

And the tested order I get is:

  • Class 3 with order number 1
  • Class 2 with order number 2
  • Class 1 with order number 1

So either way the Class1 should appear before Class2 which extends it in TypeScript so we shouldn't get the error of having a class used before its declaration, but unfortunately that is not the case.

The output:

image

from reinforced.typings.

pavel-b-novikov avatar pavel-b-novikov commented on August 25, 2024

Why dont you use builder.ExportAs...().Order(10) method or .ExportAsClasses(...,x=>x.Order(...))?
Please ask questions on StackOverflow

from reinforced.typings.

yegorpetrov avatar yegorpetrov commented on August 25, 2024

The problem I see here is that the output is erroneous by default. And it can be cumbersome to figure out and maintain the proper order in a situation with numerous interdependent classes. I kinda expected this framework to order types properly by itself.

Is this out of the scope of the project? Were there attempts to sort it out automatically?

from reinforced.typings.

pavel-b-novikov avatar pavel-b-novikov commented on August 25, 2024

It is out of scope of project because

  • this case is complex to detect and resolve when exporting to multiple files/namespaces plus inheritance of classes in different namespaces plus how it should work with TS modules?
  • this case is extremely rare and easily fixable by pulling all the base classes to the top using .Order
  • so according to previous 2 points, ratio of time spent implementing this feature/collected benefits is extremely low

So for now I'm not going to implement that. If you want to implement this feature - feel free to fork & PR. Otherwise please use .Order

from reinforced.typings.

yegorpetrov avatar yegorpetrov commented on August 25, 2024

If someone else comes upon this issue, here's one way to "pull all the base classes to the top" in a more or less universal manner:

IEnumerable<T> EnumerateHierarchy<T>(T item, Func<T, T> selector)
{
    do yield return item;
    while ((item = selector(item)) != default(T));
}
...
IEnumerable<Type> dtoClasses...
Func<Type, int> orderFunc = dtoClasses.SelectMany(
    t => EnumerateHierarchy(t, e => e.BaseType).Reverse()).ToList().IndexOf;
cfg.ExportAsClasses(dtoClasses, c => c.Order(orderFunc(c.Type))...

May be quite far from perfect, it's only tested to cover my specific case.

from reinforced.typings.

pavel-b-novikov avatar pavel-b-novikov commented on August 25, 2024

Thank you, Egor! But it would be much better if you post such kind of question to stackoverflow - more audience coverage there.

I have marked your solution with "faq" label, but still not sure that someone can find it quickly.

from reinforced.typings.

yegorpetrov avatar yegorpetrov commented on August 25, 2024

@vmandic I think that's because you have separate namespaces. You can't expect the lower level order to override the higher level one automatically.

from reinforced.typings.

pavel-b-novikov avatar pavel-b-novikov commented on August 25, 2024

RT. Connecting people.

from reinforced.typings.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.