private Constant?ParseConstant(RequestParameter parameter)
        {
            if (parameter.ClientDefaultValue != null)
            {
                CSharpType constantTypeReference = _context.TypeFactory.CreateType(parameter.Schema, parameter.IsNullable());
                return(BuilderHelpers.ParseConstant(parameter.ClientDefaultValue, constantTypeReference));
            }

            if (parameter.Schema is ConstantSchema constantSchema)
            {
                return(ParseConstant(constantSchema));
            }

            return(null);
        }
        private List <EnumTypeValue> BuildValues()
        {
            var values = new List <EnumTypeValue>();

            foreach (var c in _choices)
            {
                var name          = BuilderHelpers.DisambiguateName(Type, c.CSharpName());
                var memberMapping = _typeMapping?.GetForMember(name);
                values.Add(new EnumTypeValue(
                               BuilderHelpers.CreateMemberDeclaration(name, Type, "public", memberMapping?.ExistingMember, _context.TypeFactory),
                               CreateDescription(c),
                               BuilderHelpers.ParseConstant(c.Value, BaseType)));
            }

            return(values);
        }
 protected Constant ParseConstant(ConstantSchema constant) =>
 BuilderHelpers.ParseConstant(constant.Value.Value, _context.TypeFactory.CreateType(constant.ValueType, constant.Value.Value == null));
Beispiel #4
0
        private ObjectTypeConstructor BuildInitializationConstructor()
        {
            List <Parameter> defaultCtorParameters = new List <Parameter>();
            List <ObjectPropertyInitializer> defaultCtorInitializers = new List <ObjectPropertyInitializer>();

            ObjectTypeConstructor?baseCtor = null;

            if (Inherits != null && !Inherits.IsFrameworkType && Inherits.Implementation is ObjectType objectType)
            {
                baseCtor = objectType.Constructors.First();
                defaultCtorParameters.AddRange(baseCtor.Parameters);
            }

            foreach (var property in Properties)
            {
                // Only required properties that are not discriminators go into default ctor
                if (property == Discriminator?.Property)
                {
                    continue;
                }

                ReferenceOrConstant?initializationValue;

                if (property.SchemaProperty?.Schema is ConstantSchema constantSchema)
                {
                    // Turn constants into initializers
                    initializationValue = constantSchema.Value.Value != null?
                                          BuilderHelpers.ParseConstant(constantSchema.Value.Value, property.Declaration.Type) :
                                              Constant.NewInstanceOf(property.Declaration.Type);
                }
                else if (IsStruct || property.SchemaProperty?.Required == true)
                {
                    // For structs all properties become required
                    Constant?defaultParameterValue = null;
                    if (property.SchemaProperty?.ClientDefaultValue is object defaultValueObject)
                    {
                        defaultParameterValue = BuilderHelpers.ParseConstant(defaultValueObject, property.Declaration.Type);
                    }

                    var defaultCtorParameter = new Parameter(
                        property.Declaration.Name.ToVariableName(),
                        property.Description,
                        TypeFactory.GetInputType(property.Declaration.Type),
                        defaultParameterValue,
                        true
                        );

                    defaultCtorParameters.Add(defaultCtorParameter);
                    initializationValue = defaultCtorParameter;
                }
                else
                {
                    initializationValue = GetPropertyDefaultValue(property);
                }

                if (initializationValue != null)
                {
                    defaultCtorInitializers.Add(new ObjectPropertyInitializer(property, initializationValue.Value));
                }
            }

            if (Discriminator != null)
            {
                defaultCtorInitializers.Add(new ObjectPropertyInitializer(Discriminator.Property, BuilderHelpers.StringConstant(Discriminator.Value)));
            }

            return(new ObjectTypeConstructor(
                       BuilderHelpers.CreateMemberDeclaration(
                           Type.Name,
                           Type,
                           // inputs have public ctor by default
                           _objectSchema.IsInput ? "public" : "internal",
                           null,
                           _typeFactory),
                       defaultCtorParameters.ToArray(),
                       defaultCtorInitializers.ToArray(),
                       baseCtor));
        }