private static IEnumerable <PathSegment> GetPathSegments(string httpRequestUri, Dictionary <string, PathSegment> parameters, bool isRaw = false) { PathSegment TextSegment(string text) { return(new PathSegment(BuilderHelpers.StringConstant(text), false, SerializationFormat.Default, isRaw)); } foreach ((string text, bool isLiteral) in StringExtensions.GetPathParts(httpRequestUri)) { yield return(isLiteral ? TextSegment(text) : parameters[text]); } }
private ReferenceOrConstant?GetPropertyDefaultValue(ObjectTypeProperty property) { if (property == Discriminator?.Property) { if (Discriminator.Value != null) { return(BuilderHelpers.StringConstant(Discriminator.Value)); } } else { var initializeWithType = property.InitializeWithType; return(initializeWithType != null?Constant.NewInstanceOf(initializeWithType) : (ReferenceOrConstant?)null); } return(null); }
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)); }
private ObjectTypeConstructor BuildSerializationConstructor() { bool ownsDiscriminatorProperty = false; List <Parameter> serializationConstructorParameters = new List <Parameter>(); List <ObjectPropertyInitializer> serializationInitializers = new List <ObjectPropertyInitializer>(); ObjectTypeConstructor? baseSerializationCtor = null; if (Inherits != null && !Inherits.IsFrameworkType && Inherits.Implementation is ObjectType objectType) { baseSerializationCtor = objectType.Constructors.Last(); serializationConstructorParameters.AddRange(baseSerializationCtor.Parameters); } foreach (var property in Properties) { var deserializationParameter = new Parameter( property.Declaration.Name.ToVariableName(), property.Description, property.Declaration.Type, null, false ); ownsDiscriminatorProperty |= property == Discriminator?.Property; serializationConstructorParameters.Add(deserializationParameter); serializationInitializers.Add(new ObjectPropertyInitializer(property, deserializationParameter, GetPropertyDefaultValue(property))); } if (Discriminator != null) { // Add discriminator initializer to constructor at every level of hierarchy if (!ownsDiscriminatorProperty && baseSerializationCtor != null) { var discriminatorParameter = baseSerializationCtor.FindParameterByInitializedProperty(Discriminator.Property); Debug.Assert(discriminatorParameter != null); serializationInitializers.Add(new ObjectPropertyInitializer(Discriminator.Property, discriminatorParameter, BuilderHelpers.StringConstant(Discriminator.Value))); } } return(new ObjectTypeConstructor( BuilderHelpers.CreateMemberDeclaration(Type.Name, Type, "internal", null, _typeFactory), serializationConstructorParameters.ToArray(), serializationInitializers.ToArray(), baseSerializationCtor )); }