public static CodeWriter WriteReferenceOrConstant(this CodeWriter writer, ReferenceOrConstant value) { if (value.IsConstant) { writer.WriteConstant(value.Constant); } else { var parts = value.Reference.Name.Split("."); bool first = true; foreach (var part in parts) { if (first) { first = false; } else { writer.AppendRaw("."); } writer.Identifier(part); } } return(writer); }
public static CodeWriter AppendEnumToString(this CodeWriter writer, EnumType enumType) { if (!enumType.IsExtendable) { writer.UseNamespace(enumType.Type.Namespace); } return(writer.AppendRaw(enumType.IsExtendable ? ".ToString()" : ".ToSerialString()")); }
public static void WriteParameter(this CodeWriter writer, Parameter clientParameter, bool includeDefaultValue = true) { writer.Append($"{clientParameter.Type} {clientParameter.Name:D}"); if (includeDefaultValue && clientParameter.DefaultValue != null) { if (CanBeInitializedInline(clientParameter)) { writer.Append($" = {clientParameter.DefaultValue.Value.Value:L}"); } else { // initialize with null and set the default later writer.Append($" = null"); } } writer.AppendRaw(","); }
private static void DeserializeFrameworkTypeValue(CodeWriter writer, CodeWriterDelegate element, Type frameworkType, SerializationFormat serializationFormat) { bool includeFormat = false; writer.Append($"{element}."); if (frameworkType == typeof(JsonElement)) { writer.AppendRaw("Clone"); } if (frameworkType == typeof(object)) { writer.AppendRaw("GetObject"); } if (frameworkType == typeof(bool)) { writer.AppendRaw("GetBoolean"); } if (frameworkType == typeof(char)) { writer.AppendRaw("GetChar"); } if (frameworkType == typeof(short)) { writer.AppendRaw("GetInt16"); } if (frameworkType == typeof(int)) { writer.AppendRaw("GetInt32"); } if (frameworkType == typeof(long)) { writer.AppendRaw("GetInt64"); } if (frameworkType == typeof(float)) { writer.AppendRaw("GetSingle"); } if (frameworkType == typeof(double)) { writer.AppendRaw("GetDouble"); } if (frameworkType == typeof(decimal)) { writer.AppendRaw("GetDecimal"); } if (frameworkType == typeof(string)) { writer.AppendRaw("GetString"); } if (frameworkType == typeof(Guid)) { writer.AppendRaw("GetGuid"); } if (frameworkType == typeof(byte[])) { writer.AppendRaw("GetBytesFromBase64"); includeFormat = true; } if (frameworkType == typeof(DateTimeOffset)) { writer.AppendRaw("GetDateTimeOffset"); includeFormat = true; } if (frameworkType == typeof(TimeSpan)) { writer.AppendRaw("GetTimeSpan"); includeFormat = true; } writer.AppendRaw("("); if (includeFormat && serializationFormat.ToFormatSpecifier() is string formatString) { writer.Literal(formatString); } writer.AppendRaw(")"); }
public static void ToSerializeCall(this CodeWriter writer, JsonSerialization serialization, CodeWriterDelegate name, CodeWriterDelegate?writerName = null) { writerName ??= w => w.AppendRaw("writer"); switch (serialization) { case JsonArraySerialization array: writer.Line($"{writerName}.WriteStartArray();"); var collectionItemVariable = new CodeWriterDeclaration("item"); using (writer.Scope($"foreach (var {collectionItemVariable:D} in {name})")) { writer.ToSerializeCall( array.ValueSerialization, w => w.Append(collectionItemVariable), writerName); } writer.Line($"{writerName}.WriteEndArray();"); return; case JsonDictionarySerialization dictionary: writer.Line($"{writerName}.WriteStartObject();"); var dictionaryItemVariable = new CodeWriterDeclaration("item"); using (writer.Scope($"foreach (var {dictionaryItemVariable:D} in {name})")) { writer.Line($"{writerName}.WritePropertyName({dictionaryItemVariable}.Key);"); writer.ToSerializeCall( dictionary.ValueSerialization, w => w.Append($"{dictionaryItemVariable}.Value"), writerName); } writer.Line($"{writerName}.WriteEndObject();"); return; case JsonObjectSerialization obj: writer.Line($"{writerName}.WriteStartObject();"); foreach (JsonPropertySerialization property in obj.Properties) { bool hasNullableType = property.Property?.Declaration.Type.IsNullable == true; bool emptyAsUndefined = property.Property?.EmptyAsUndefined == true; CodeWriter.CodeWriterScope?scope = default; if (hasNullableType || emptyAsUndefined) { var propertyName = property.Property !.Declaration.Name; writer.Append($"if ("); if (hasNullableType) { writer.Append($"{propertyName} != null"); } if (emptyAsUndefined) { if (hasNullableType) { writer.Append($" && "); } writer.UseNamespace(typeof(Enumerable).Namespace !); writer.Append($"{propertyName}.Any()"); } writer.Append($")"); scope = writer.Scope(); } using (scope) { writer.Line($"{writerName}.WritePropertyName({property.Name:L});"); writer.ToSerializeCall( property.ValueSerialization, w => w.Append($"{property.Property?.Declaration.Name}")); } } if (obj.AdditionalProperties != null) { var itemVariable = new CodeWriterDeclaration("item"); using (writer.Scope($"foreach (var {itemVariable:D} in {obj.AdditionalProperties.Property.Declaration.Name})")) { writer.Line($"{writerName}.WritePropertyName({itemVariable}.Key);"); writer.ToSerializeCall( obj.AdditionalProperties.ValueSerialization, w => w.Append($"{itemVariable}.Value"), writerName); } } writer.Line($"{writerName}.WriteEndObject();"); return; case JsonValueSerialization valueSerialization: writer.UseNamespace(typeof(Utf8JsonWriterExtensions).Namespace !); if (valueSerialization.Type.IsFrameworkType) { var frameworkType = valueSerialization.Type.FrameworkType; if (frameworkType == typeof(JsonElement)) { writer.Line($"{name}.WriteTo({writerName});"); return; } bool writeFormat = false; writer.Append($"{writerName}."); if (frameworkType == typeof(decimal) || frameworkType == typeof(double) || frameworkType == typeof(float) || frameworkType == typeof(long) || frameworkType == typeof(int) || frameworkType == typeof(short)) { writer.AppendRaw("WriteNumberValue"); } else if (frameworkType == typeof(object)) { writer.AppendRaw("WriteObjectValue"); } else if (frameworkType == typeof(string) || frameworkType == typeof(char) || frameworkType == typeof(Guid)) { writer.AppendRaw("WriteStringValue"); } else if (frameworkType == typeof(bool)) { writer.AppendRaw("WriteBooleanValue"); } else if (frameworkType == typeof(byte[])) { writer.AppendRaw("WriteBase64StringValue"); writeFormat = true; } else if (frameworkType == typeof(DateTimeOffset) || frameworkType == typeof(DateTime) || frameworkType == typeof(TimeSpan)) { if (valueSerialization.Format == SerializationFormat.DateTime_Unix) { writer.AppendRaw("WriteNumberValue"); } else { writer.AppendRaw("WriteStringValue"); } writeFormat = true; } writer.Append($"({name}") .AppendNullableValue(valueSerialization.Type); if (writeFormat && valueSerialization.Format.ToFormatSpecifier() is string formatString) { writer.Append($", {formatString:L}"); } writer.LineRaw(");"); return; } switch (valueSerialization.Type.Implementation) { case ObjectType _: writer.Line($"{writerName}.WriteObjectValue({name});"); return; case EnumType clientEnum: writer.Append($"{writerName}.WriteStringValue({name}") .AppendNullableValue(valueSerialization.Type) .AppendEnumToString(clientEnum) .Line($");"); return; } throw new NotSupportedException(); default: throw new NotSupportedException(); } }
private void WriteObjectSchema(CodeWriter writer, ObjectType schema) { using (writer.Namespace(schema.Declaration.Namespace)) { List <CSharpType> implementsTypes = new List <CSharpType>(); if (schema.Inherits != null) { implementsTypes.Add(schema.Inherits); } if (schema.ImplementsDictionaryType != null) { implementsTypes.Add(schema.ImplementsDictionaryType); } writer.WriteXmlDocumentationSummary(schema.Description); if (schema.IsStruct) { writer.Append($"{schema.Declaration.Accessibility} readonly partial struct {schema.Declaration.Name}"); } else { writer.Append($"{schema.Declaration.Accessibility} partial class {schema.Declaration.Name}"); } if (implementsTypes.Any()) { writer.AppendRaw(" : "); foreach (var type in implementsTypes) { writer.Append($"{type} ,"); } writer.RemoveTrailingComma(); } writer.Line(); using (writer.Scope()) { WriteConstructor(writer, schema); foreach (var property in schema.Properties) { writer.WriteXmlDocumentationSummary(property.Description); CSharpType propertyType = property.Declaration.Type; writer.Append($"{property.Declaration.Accessibility} {propertyType} {property.Declaration.Name:D}"); writer.AppendRaw(property.IsReadOnly ? "{ get; }" : "{ get; set; }"); writer.Line(); } if (schema.AdditionalPropertiesProperty is ObjectTypeProperty additionalPropertiesProperty) { var dictionaryType = schema.ImplementsDictionaryType; Debug.Assert(dictionaryType != null); var keyType = typeof(string); var isReadonly = dictionaryType.FrameworkType == typeof(IReadOnlyDictionary <,>); var itemType = dictionaryType.Arguments[1]; var keyValuePairType = new CSharpType(typeof(KeyValuePair <,>), keyType, itemType); var iEnumeratorKeyValuePairType = new CSharpType(typeof(IEnumerator <>), keyValuePairType); var collectionType = isReadonly ? typeof(IReadOnlyCollection <>) : typeof(ICollection <>); var iCollectionKeyValuePairType = new CSharpType(collectionType, keyValuePairType); var keyValueCollectionType = isReadonly ? typeof(IEnumerable <>) : typeof(ICollection <>); var iCollectionKeyType = new CSharpType(keyValueCollectionType, keyType); var iCollectionItemType = new CSharpType(keyValueCollectionType, itemType); var iEnumerator = typeof(IEnumerator); var iEnumerable = typeof(IEnumerable); string additionalProperties = additionalPropertiesProperty.Declaration.Name; writer .WriteXmlDocumentationInheritDoc() .Line($"public {iEnumeratorKeyValuePairType} GetEnumerator() => {additionalProperties}.GetEnumerator();") .WriteXmlDocumentationInheritDoc() .Line($"{iEnumerator} {iEnumerable}.GetEnumerator() => {additionalProperties}.GetEnumerator();") .WriteXmlDocumentationInheritDoc() .Line($"public bool TryGetValue(string key, out {itemType} value) => {additionalProperties}.TryGetValue(key, out value);") .WriteXmlDocumentationInheritDoc() .Line($"public bool ContainsKey({keyType} key) => {additionalProperties}.ContainsKey(key);") .WriteXmlDocumentationInheritDoc() .Line($"public {iCollectionKeyType} Keys => {additionalProperties}.Keys;") .WriteXmlDocumentationInheritDoc() .Line($"public {iCollectionItemType} Values => {additionalProperties}.Values;") .WriteXmlDocumentationInheritDoc() .Line($"int {iCollectionKeyValuePairType}.Count => {additionalProperties}.Count;"); if (!isReadonly) { writer .WriteXmlDocumentationInheritDoc() .Line($"public void Add({keyType} key, {itemType} value) => {additionalProperties}.Add(key, value);") .WriteXmlDocumentationInheritDoc() .Line($"public bool Remove({keyType} key) => {additionalProperties}.Remove(key);") .WriteXmlDocumentationInheritDoc() .Line($"bool {iCollectionKeyValuePairType}.IsReadOnly => {additionalProperties}.IsReadOnly;") .WriteXmlDocumentationInheritDoc() .Line($"void {iCollectionKeyValuePairType}.Add({keyValuePairType} value) => {additionalProperties}.Add(value);") .WriteXmlDocumentationInheritDoc() .Line($"bool {iCollectionKeyValuePairType}.Remove({keyValuePairType} value) => {additionalProperties}.Remove(value);") .WriteXmlDocumentationInheritDoc() .Line($"bool {iCollectionKeyValuePairType}.Contains({keyValuePairType} value) => {additionalProperties}.Contains(value);") .WriteXmlDocumentationInheritDoc() .Line($"void {iCollectionKeyValuePairType}.CopyTo({keyValuePairType}[] destination, int offset) => {additionalProperties}.CopyTo(destination, offset);") .WriteXmlDocumentationInheritDoc() .Line($"void {iCollectionKeyValuePairType}.Clear() => {additionalProperties}.Clear();"); } using (writer .WriteXmlDocumentationInheritDoc() .Scope($"public {itemType} this[{keyType} key]")) { writer.Line($"get => {additionalProperties}[key];"); if (!isReadonly) { writer.Line($"set => {additionalProperties}[key] = value;"); } } } } } }