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

[Question] Is it possible to output a property's default value? #128

Open
rmarskell opened this issue Oct 21, 2024 · 3 comments
Open

[Question] Is it possible to output a property's default value? #128

rmarskell opened this issue Oct 21, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@rmarskell
Copy link

Hey there, I'm looking to integrate NTypewriter (source generator) into a new project and so far I'm loving the capabilities. A previous project I worked on used TypeGen and while it was good for simplicity, there was no real control over the templates. I've been able to replicate all of the functionality of TypeGen with NTypewriter, except one big thing (default values) and I'm hoping I'm just missing something/not searching the correct terms.

Basically, given the following C# classes...

using System.Collections.Generic;

public class NameCountClass {
  public string Name { get; set; }
  public int Count { get; set; }
}

public static class StaticClass {
  public static string StaticProperty => "Test";

  public static Dictionary<string, NameCountClass> NameCounts = new Dictionary<string, NameCountClass>() {
    { "a", new NameCountClass() { Name = "A name", Count = 123 } },
    { "b", new NameCountClass() { Name = "B name", Count = 321 } }
  };
}

...is it possible to output the default values in a TS class something like this:

export class StaticClass {
  static StaticProperty: string = "Test";
  static NameCounts: { [key: string]: NameCountClass } = {"a":{"Name":"A name", "Count": 123}, "b":{"Name":"B name", "Count": 321}}
}

Basically, is there a way to access the default values for properties to output them?

@NeVeSpl
Copy link
Owner

NeVeSpl commented Oct 21, 2024

Only field constants are supported.

Property's default value, property's getter defined by expression body, object and collection initializers are not currently supported. Mostly because these are C# syntax sugar that do not exist on Roslyn level (symbol/semantic).

But all the objects in NTypewriter code model have ISymbolBase.SourceCode property that gives access to C# syntax that defines a given object, so you can write a custom function that will parse any C# syntax to any TS.

@NeVeSpl NeVeSpl added the enhancement New feature or request label Oct 21, 2024
@rmarskell
Copy link
Author

Thanks for the quick reply. Is there any example code I could look at that utilizes that property as you suggest?

@NeVeSpl
Copy link
Owner

NeVeSpl commented Oct 22, 2024

There is no example, and as far as I know, no one has ever tried to get access to what you call a property's default value.

The type of ISymbolBase.SourceCode is string, and probably some regular expression would be necessary to extract required informations, but all this can be done on the user side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants