Beispiel #1
0
        private IEnumerable <CodeGenClass> GenerateModels(ApiDefinition definition)
        {
            foreach (ApiModel model in definition.Models)
            {
                if (string.IsNullOrWhiteSpace(model.Name))
                {
                    throw new ArgumentException("Invalid model name");
                }

                var modelClass = new CodeGenClass(
                    model.Name,
                    Scope.Public,
                    ClassType.Normal);

                modelClass.Comment = new CodeGenComment(model.Description);

                foreach (ApiType prop in model.Props)
                {
                    if (string.IsNullOrWhiteSpace(prop.Name))
                    {
                        throw new ArgumentException("Invalid property name");
                    }
                    if (string.IsNullOrWhiteSpace(prop.Type))
                    {
                        throw new ArgumentException("Invalid property type");
                    }

                    var propProp = new CodeGenProperty(
                        LowerToUpperCamel(prop.Name),
                        ResolveModelType(prop.Type),
                        Scope.Public,
                        true);

                    propProp.Comment = new CodeGenComment(prop.Description);

                    // TODO required

                    modelClass.Properties.Add(propProp);
                }

                yield return(modelClass);
            }
        }
        public CodeGenClass Create(DecoratorClassInformation factoryInformation)
        {
            IEnumerable <CodeGenGeneric> genericTypes = factoryInformation.GenericTypes?.Select(
                parameter => FactoryHelpers.GenerateMethodParameter(parameter, factoryInformation.TypeConstraints));

            string[] derivedFrom = !string.IsNullOrEmpty(factoryInformation.DerviedFrom)
                ? new[] { factoryInformation.DerviedFrom }
                : null;

            var generatedDecoratorClass = new CodeGenClass(
                factoryInformation.ParentInformation.DecoratorName,
                Scope.Public,
                ClassType.Normal,
                genericTypes,
                derivedFrom);

            generatedDecoratorClass.Comment = new CodeGenComment($@"Generated {factoryInformation.Template.Label} decorator for the {factoryInformation.DecoratedType } type. Auto-generated on {DateTimeOffset.Now}");

            var constructorParams      = new List <string>(new[] { $"{factoryInformation.DecoratedType} decorated" });
            var constructorBodyBuilder = new StringBuilder();

            constructorBodyBuilder.AppendLine("_Decorated = decorated ?? throw new ArgumentNullException(nameof(decorated));");

            if (factoryInformation.Template.ConstructorParams != null)
            {
                foreach (ConstructorParam constructorParam in factoryInformation.Template.ConstructorParams)
                {
                    constructorParams.Add($"{constructorParam.Type} {constructorParam.Name}");
                    constructorBodyBuilder.AppendLine($"_{constructorParam.Name} = {constructorParam.Name};");

                    generatedDecoratorClass.Variables.Add(new CodeGenVariable(
                                                              $"_{constructorParam.Name}",
                                                              constructorParam.Type,
                                                              Scope.Private,
                                                              readOnly: true));
                }
            }

            // Constructor
            generatedDecoratorClass.Constructors.Add(new CodeGenConstructor(
                                                         factoryInformation.ParentInformation.DecoratorName,
                                                         Scope.Public,
                                                         constructorParams,
                                                         constructorBodyBuilder.ToString()));

            // Decorated variable
            generatedDecoratorClass.Variables.Add(new CodeGenVariable(
                                                      "_Decorated",
                                                      factoryInformation.DecoratedType,
                                                      Scope.Private,
                                                      readOnly: true));

            foreach (DecoratorMethodInformation methodInformation in factoryInformation.MethodInformation)
            {
                CodeGenMethod method = _MethodFactory.Create(methodInformation);
                generatedDecoratorClass.Methods.Add(method);
            }

            foreach (DecoratorPropertyInformation propertyInformation in factoryInformation.PropertyInformation)
            {
                CodeGenProperty property = _PropertyFactory.Create(propertyInformation);
                generatedDecoratorClass.Properties.Add(property);
            }

            return(generatedDecoratorClass);
        }