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

How to get Class from Action.ReturnType or Action.Parameters #81

Open
gregveres opened this issue Jun 22, 2023 · 3 comments
Open

How to get Class from Action.ReturnType or Action.Parameters #81

gregveres opened this issue Jun 22, 2023 · 3 comments
Labels

Comments

@gregveres
Copy link
Contributor

both of these give me a Type, which makes sense because the return type or the parameter might be a built-in such as a string.
But I have a situation where I need to specify a different name in the TS file than the class name. This special name is specified in an attribute that is on the class. If this attribute exists, then I need to output the special name rather than the type name. If the attribute doesn't exist, or the type isn't a class, then I just go ahead and output the type name.

But I can't see any way to:

  1. determine if the type is really a class rather than a built in type.
  2. once I determine that the type is a class, how do I convert it to the class so that I can then fetch the attributes

Any pointers would be very much appreciated.

@gregveres
Copy link
Contributor Author

Ok, I think I have figured it out.

IType is a symbol and as such, I can assume that it has an attributes array and do a filter on it.

I have created a function like this:

{{- func typeName(type)
   	useType = Type.Unwrap(type)
	if (useType.ArrayType != null) 
	    useType = useType.ArrayType
	end
        typeAttrs = useType.Attributes | Array.Filter @AttrIsTypescriptType
        if (typeAttrs.size > 0)
             ret typeAttrs[0] | Custom.ExtractTypescriptType;
        end
	ret useType | Type.ToTypeScriptType
	end
}}
{{- func AttrIsTypescriptType(attr)
	ret attr.Name | String.Contains "TypescriptType"
	end
}}

first, I nwrap the type and extract the actual type if the type is an array type.
Then, I search the list of attributes for the symbol to find the TypescriptType attribute and if it exists, I use a custom c# function to extract the attributes parameter, which is it's name.
If the symbol doesn't have the TypescriptType attribute, I just return the Type.ToTypescriptType.

This seems to work well. I now just need to figure out how to do this on the parameter array.

@NeVeSpl
Copy link
Owner

NeVeSpl commented Jun 25, 2023

It looks like you are getting there, but I really do not know why you prefer to do it in scriban instead of C#.

@gregveres
Copy link
Contributor Author

Hmm, that is a good point. I hadn't thought of doing it in C#. In my mind, the NTypewriter scripts are scriban and I was just locked into "do it in scriban". I will have to re-evaluate it. I think I could really simplify the scripts, and introduce better reuse if I did more in C#.

Thanks for the suggestion!

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

No branches or pull requests

2 participants