public string GenerateEnumDefinition(ApiEnum apiEnum)
        {
            var indent  = new Indent();
            var builder = new StringBuilder();

            var typeNameForEnum = apiEnum.ToCSharpClassName();

            if (apiEnum.Deprecation != null)
            {
                builder.AppendLine(apiEnum.Deprecation.ToCSharpDeprecation());
            }

            builder.AppendLine($"{indent}[JsonConverter(typeof(EnumStringConverter))]");
            builder.AppendLine($"{indent}public enum {typeNameForEnum}");
            builder.AppendLine($"{indent}{{");

            indent.Increment();

            foreach (var value in apiEnum.Values)
            {
                var identifierForValue = CSharpIdentifier.ForClassOrNamespace(value);
                builder.AppendLine($"{indent}[EnumMember(Value = \"{value}\")]");
                builder.AppendLine($"{indent}{identifierForValue},");
                builder.AppendLine($"{indent}");
            }

            indent.Decrement();

            builder.AppendLine($"{indent}}}");
            return(builder.ToString());
        }
Ejemplo n.º 2
0
        public static string ToCSharpFactoryMethodName(this ApiDto subject, ApiDto parent)
        {
            var classNameForParent  = CSharpIdentifier.ForClassOrNamespace(parent.Name);
            var classNameForSubject = CSharpIdentifier.ForClassOrNamespace(subject.Name);
            var factoryMethodName   = CSharpIdentifier.ForClassOrNamespace(
                classNameForSubject.Replace(classNameForParent, string.Empty));

            return(factoryMethodName);
        }
        public static string?ToCSharpRequestBodyClassName(this ApiEndpoint subject, string endpointPath)
        {
            if (subject.RequestBody == null ||
                subject.RequestBody.Kind != ApiFieldType.Object.ObjectKind.REQUEST_BODY)
            {
                return(null);
            }

            return(CSharpIdentifier.ForClassOrNamespace(endpointPath)
                   + subject.Method.ToHttpMethod().ToLowerInvariant().ToUppercaseFirst()
                   + "Request");
        }
        public static string ToCSharpPropertyName(this ApiField subject, string?containingType)
        {
            var propertyName = CSharpIdentifier.ForClassOrNamespace(subject.Name);

            return(subject.Type switch
            {
                ApiFieldType.Primitive primitive when primitive.ToCSharpPrimitiveType() == CSharpType.Bool &&
                !propertyName.StartsWith("Is") &&
                !propertyName.StartsWith("Can")
                => $"Is{propertyName}",

                // Resolve CS0542 - Member names cannot be the same as their enclosing type by adding prefix/suffix
                ApiFieldType.Array _ when string.Equals(propertyName, containingType, StringComparison.OrdinalIgnoreCase)
                => $"{propertyName}Items",

                _ => propertyName
            });
        public override IClassNameConvertible Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Null)
            {
                return(null);
            }

            var readerAtStart = reader;

            using var jsonDocument = JsonDocument.ParseValue(ref reader);
            var jsonObject = jsonDocument.RootElement;

            var className = jsonObject.GetStringValue("className");

            if (!string.IsNullOrEmpty(className))
            {
                if (!TypeMap.TryGetValue(className, out var targetType))
                {
                    targetType = Type.GetType(SpaceDotNetClientNamespace + "." + CSharpIdentifier.ForClassOrNamespace(className) + ", " + SpaceDotNetClientAssemblyName);
                    if (targetType != null)
                    {
                        TypeMap[className] = targetType;
                    }
                }
                if (targetType != null && typeof(IClassNameConvertible).IsAssignableFrom(targetType))
                {
                    var value = JsonSerializer.Deserialize(ref readerAtStart, targetType, options) as IClassNameConvertible;

                    PropagatePropertyAccessPathHelper.SetAccessPathForValue(targetType.Name, true, value);

                    return(value);
                }
            }

            return(null);
        }
Ejemplo n.º 6
0
 public static string ToCSharpClassName(this ApiDto subject)
 => CSharpIdentifier.ForClassOrNamespace(subject.Name);
Ejemplo n.º 7
0
        private string GenerateMenuIds(Node node)
        {
            var indent  = new Indent();
            var builder = new StringBuilder();

            var lastDotIndexInPrefix = node.Prefix.LastIndexOf(".", StringComparison.Ordinal);
            var commonPrefix         = lastDotIndexInPrefix >= 0
                ? CSharpIdentifier.ForClassOrNamespace(node.Prefix.Substring(lastDotIndexInPrefix + 1))
                : CSharpIdentifier.ForClassOrNamespace(node.Prefix);

            if (node.Children.Count <= 1)
            {
                var prefix = node.Children.Count > 0
                    ? commonPrefix
                    : "Root";

                var expectedPayload = string.Empty;
                if (node.Context != null && _context.TryGetDto(node.Context.Id, out var expectedPayloadDto) && expectedPayloadDto != null)
                {
                    expectedPayload = expectedPayloadDto.ToCSharpClassName();
                }

                builder.AppendLine($"{indent}/// <summary>");
                builder.AppendLine($"{indent}/// Represents the \"{node.Prefix}\" menu.");
                if (!string.IsNullOrEmpty(expectedPayload) && expectedPayload != CSharpType.Object.Value)
                {
                    builder.AppendLine($"{indent}///");
                    builder.AppendLine($"{indent}/// Expected webhook payload: <see cref=\"{expectedPayload}\"/>.");
                }
                builder.AppendLine($"{indent}/// </summary>");
                builder.AppendLine($"{indent}public static readonly string {prefix} = \"{node.Prefix}\";");
                builder.AppendLine($"{indent}");
            }
            else
            {
                if (!string.IsNullOrEmpty(node.Prefix))
                {
                    builder.AppendLine($"{indent}public static class {commonPrefix}");
                    builder.AppendLine($"{indent}{{");

                    indent.Increment();
                }

                foreach (var nodeChild in node.Children)
                {
                    builder.Append(
                        indent.Wrap(
                            GenerateMenuIds(nodeChild)));
                }

                if (!string.IsNullOrEmpty(node.Prefix))
                {
                    indent.Decrement();

                    builder.AppendLine($"{indent}}}");
                    builder.AppendLine($"{indent}");
                }
            }

            return(builder.ToString());
        }
Ejemplo n.º 8
0
 public static string ToCSharpClassNameShort(this ApiUrlParameterOption subject)
 => CSharpIdentifier.ForClassOrNamespace(subject.OptionName.Split('.', StringSplitOptions.RemoveEmptyEntries).Last());
Ejemplo n.º 9
0
 public static string ToCSharpClassName(this ApiUrlParameterOption subject)
 => CSharpIdentifier.ForClassOrNamespace(subject.OptionName);
 public static string ToCSharpMethodName(this ApiEndpoint subject) =>
 CSharpIdentifier.ForClassOrNamespace(subject.FunctionName ?? subject.DisplayName);
 public static string ToCSharpIdentifierPlural(this ApiResource subject)
 => CSharpIdentifier.ForClassOrNamespace(subject.DisplayPlural);
 public static string ToCSharpIdentifierSingular(this ApiResource subject)
 => CSharpIdentifier.ForClassOrNamespace(subject.DisplaySingular);