public static string? ToCSharpDefaultValueForAssignment()

in src/JetBrains.Space.Generator/CodeGeneration/CSharp/Extensions/ApiFieldExtensions.cs [81:135]


    public static string? ToCSharpDefaultValueForAssignment(this ApiField subject, CodeGenerationContext context)
    {
        if (subject.DefaultValue == null) return null;
            
        if (subject.DefaultValue is ApiDefaultValue.Const.EnumEntry enumEntry)
        {
            var apiEnumRef = subject.Type as ApiFieldType.Enum;
            if (apiEnumRef == null || !context.TryGetEnum(apiEnumRef.EnumRef!.Id, out var apiEnum) || apiEnum == null)
            {
                throw new NotSupportedException("For " + nameof(ApiDefaultValue.Const.EnumEntry) + ", the field type should be of type" + nameof(ApiFieldType.Enum) + ".");
            }

            var typeNameForEnum = apiEnum.ToCSharpClassName();
            var identifierForValue = CSharpIdentifier.ForClassOrNamespace(enumEntry.EntryName);

            return $"{typeNameForEnum}.{identifierForValue}";
        }

        if (subject.DefaultValue is ApiDefaultValue.Collection collection)
        {
            var typeNameForArrayElement = subject.Type.GetArrayElementTypeOrType().ToCSharpType(context);

            var builder = new CSharpBuilder();
            builder.Append($"new List<{typeNameForArrayElement}>()");

            if (collection.Elements.Count > 0)
            {
                throw new NotSupportedException("Default values with populated collections are not supported yet.");
            }
                
            return builder.ToString();
        }

        if (subject.DefaultValue is ApiDefaultValue.Map map)
        {
            var typeNameForMapValue = subject.Type.GetMapValueTypeOrType().ToCSharpType(context);

            var builder = new CSharpBuilder();
            builder.Append($"new Dictionary<string, {typeNameForMapValue}>()");

            if (map.Elements.Count > 0)
            {
                throw new NotSupportedException("Default values with populated maps are not supported yet.");
            }
                
            return builder.ToString();
        }

        if (subject.DefaultValue is ApiDefaultValue.Reference)
        {
            throw new NotSupportedException(nameof(ApiDefaultValue.Reference) + " is not supported yet.");
        }

        return null;
    }