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

C# Collection type as any[] #131

Closed
maleet opened this issue Sep 2, 2016 · 2 comments
Closed

C# Collection type as any[] #131

maleet opened this issue Sep 2, 2016 · 2 comments

Comments

@maleet
Copy link

maleet commented Sep 2, 2016

I got an old project with lots of custom collections like:

public class MessagePackEntity
    {
        private MessageEntityCollection infoMessages;
        public MessageEntityCollection InfoMessages
        {
            get
            {
                if (infoMessages == null)
                {
                    infoMessages = new MessageEntityCollection();
                }

                return infoMessages;
            }
            set
            {
                infoMessages = value;
            }
        }
        ....
}

public class MessageEntityCollection : List<MessageEntity>
{
 ....
}

result on rendering:

export class MessagePackEntity {

    // InfoMessages
    public infoMessages: any[];
    ...
}

in tst file

$Classes([TypeWriter])[
export class $Name {
    $Properties[
    // $Name
    public $name: $Type;]
}]

Would like to have

export class MessagePackEntity {

    // InfoMessages
    public infoMessages: messageEntity[];
    ...
}

Is it possible?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Sep 20, 2016

Yes it is. If this is an external module, you need to generate a model type for the collection element and import it.
This works well for me:

helpers

IEnumerable<Type> PropertyTypes(Class @class) =>
        @class.Properties
            .Select(x => x.Type)
            .Select(x => x.Unwrap()) // the magic
            .Where(x => !x.IsPrimitive || x.IsDate || x.IsEnum)
            .GroupBy(x => x.Name).Select(x => x.First());

string PropertyTypeName(Property property) => 
    property.Type.Name == "Date" ? "Moment | string" : property.Type.Name;

string ImportTypeName(Type type) => type.Name == "Date" ? "{ Moment }" : type.Name;

string FileName(Class @class) => "./" + FileNameFromTypeName(@class.name);
string FileName(Interface @interface) => "./" + FileNameFromTypeName(@interface.name);
string FileName(Property property) => "./" + FileNameFromTypeName(property.Type.name);
string FileName(Type type) => type.IsDate? "moment" :  "./" + FileNameFromTypeName(type.name);
string FileName(string type) => "./" + FileNameFromTypeName(type);

template

$Classes[
$PropertyTypes[
import $ImportTypeName from '$FileName';
]

export interface $Name {
    $Properties[$name?: $PropertyTypeName;
    ]
}
export default $Name;
]

@frhagn
Copy link
Owner

frhagn commented Oct 18, 2016

Resolved in 1.8.0

@frhagn frhagn closed this as completed Oct 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants