microsoft / typespec

https://typespec.io/
MIT License
3.96k stars 182 forks source link

Add method in `TypeFactory` to get a `ModelTypeProvider` or `EnumTypeProvider` #3468

Closed ArcturusZhang closed 1 month ago

ArcturusZhang commented 2 months ago

We need to add a method such as GetModel or GetEnum to construct a modeltypeprovider or enumtypeprovider, so that plugin writer to replace our default implementation.

public abstract class TypeFactory
{
    public abstract ModelTypeProvider GetModel(InputModelType model);
    public abstract EnumTypeProvider GetEnum(InputEnumType enum);
}

public class ModelTypeProvider : TypeProvider {}
public abstract class EnumTypeProvider : TypeProvider {}
ArcturusZhang commented 1 month ago

So we decided to make the APIs simple and lightweighted, since it is quite simple to do something like:

public class MyTypeFactory
{
    public override CSharpType CreateCSharpType(InputType inputType) => inputType switch {
        InputModelType model => new MyModel(model),
        _ => base.CreateCSharpType(inputType)
    };
}

to override the behavior of how to get the model, to make it simple, we do not need a method to specifically get a TypeProvider from a InputModelType.