autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Improve the enum modelAsString extension option (for strongly typed languages)

Open matthchr opened this issue 10 years ago • 2 comments

Currently there is behavior that if I specify an enum and choose "modelAsString": true you will avoid generating an enum type and instead generate a static class which contains a bunch of strings.

This leads to a method signature looking like this (in C#):

public void DoStuff(string myEnum)

and a static class which looks something like this:

public static class MyEnum
{
    public static readonly string ValueOne = "value1";
    public static readonly string ValueTwo = "value2";
}

Instead, could we maybe do something like this to improve the discoverability experience for users?

public void DoStuff(MyEnum myEnum)

public class MyEnum
{
    public static readonly MyEnum ValueOne = MyEnum.Custom("value1");
    public static readonly MyEnum ValueTwo = MyEnum.Custom("value2");

    public string Value { get; }

    private MyEnum(string str)
    {
        this.Value = str;
    }

    public static MyEnum Custom(string str)
    {
        return new MyEnum(str);
    }
}

This would give a better client experience for those values which are "almost an enum" but the service renders them as strings. I think this makes sense in languages like C#/Java more than it does for more dynamically typed languages like Python/Node.JS

matthchr avatar Feb 25 '16 02:02 matthchr

:+1:

This is what I originally proposed in issue https://github.com/Azure/autorest/issues/290, before it was closed a bit prematurely :-)

We've implemented this pattern with custom code in the Azure Search SDK. See these files:

  • https://github.com/Azure/azure-sdk-for-net/blob/AutoRest/src/Search/Microsoft.Azure.Search/Customizations/Indexes/Models/DataType.cs
  • https://github.com/Azure/azure-sdk-for-net/blob/AutoRest/src/Search/Microsoft.Azure.Search/Customizations/Indexes/Models/AnalyzerName.cs

It would be awesome if AutoRest could generate those files for us.

bitsforthought avatar Feb 25 '16 03:02 bitsforthought

Howdy!

In our planning for driving towards a stable '1.0' release, I'm marking this issue as 'deferred' :zzz: and we're going to review it during the post-1.0 planning cycle.

It's not to say that we're not going to work on it, or that this isn't not important, but at the moment, we're picking and choosing the stuff we must do before 1.0. :horse_racing: :horse_racing: :horse_racing:

We'll make sure we pick this back up at that point. :tada:

fearthecowboy avatar Jun 15 '16 16:06 fearthecowboy