autorest icon indicating copy to clipboard operation
autorest copied to clipboard

"Open enum" pattern in generated code

Open bitsforthought opened this issue 11 years ago • 3 comments

It would be really cool if AutoRest supported this pattern for model properties of type "string" that have a well-defined but open set of choices:

/// <summary>
/// Defines the data type of a field in an Azure Search index.
/// </summary>
public partial class DataType
{
    /// <summary>
    /// Indicates that a field contains a string.
    /// </summary>
    public static readonly DataType String = new DataType("Edm.String");

    /// <summary>
    /// Indicates that a field contains a 32-bit signed integer.
    /// </summary>
    public static readonly DataType Int32 = new DataType("Edm.Int32");

    /// <summary>
    /// Indicates that a field contains an IEEE double-precision floating point number.
    /// </summary>
    public static readonly DataType Double = new DataType("Edm.Double");

    /// <summary>
    /// Indicates that a field contains a Boolean value (true or false).
    /// </summary>
    public static readonly DataType Boolean = new DataType("Edm.Boolean");

    // Etc...

    private string _name;

    private DataType(string typeName)
    {
        _name = typeName;
    }

    /// <summary>
    /// Defines implicit conversion from DataType to string.
    /// </summary>
    /// <param name="dataType">DataType to convert.</param>
    /// <returns>The name of the DataType as a string.</returns>
    public static implicit operator string(DataType dataType)
    {
        return dataType.ToString();
    }

    /// <summary>
    /// Returns the name of the DataType in a form that can be used in an Azure Search index definition.
    /// </summary>
    /// <returns>The name of the DataType as a string.</returns>
    public override string ToString()
    {
        return _name;
    }
}

bitsforthought avatar Aug 06 '15 04:08 bitsforthought

@brjohnstmsft We currently support documenting these values in the comments for a parameter or property if the enum array is used for values. Adding constants would be a useful enhancement

markcowl avatar Aug 18 '15 19:08 markcowl

Implemented in https://github.com/Azure/autorest/pull/393

stankovski avatar Oct 23 '15 18:10 stankovski

Seems like a useful enhancement, but what I'm proposing goes one step further by using strongly-typed objects instead of string constants. The benefit (at least for C#) is better Intellisense support. For example, in the Azure Search .NET SDK, we use a DataType class instead of string constants, so when you start typing this:

var field = new Field("myField",

Intellisense tells you that the next thing you need is a DataType, not a string.

bitsforthought avatar Oct 23 '15 19:10 bitsforthought