Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ public enum ResponseStatusOption
}
namespace Azure.Core
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct ContentType : System.IEquatable<Azure.Core.ContentType>, System.IEquatable<string>
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public ContentType(string contentType) { throw null; }
public static Azure.Core.ContentType ApplicationJson { get { throw null; } }
public static Azure.Core.ContentType ApplicationOctetStream { get { throw null; } }
public static Azure.Core.ContentType TextPlain { get { throw null; } }
public bool Equals(Azure.Core.ContentType other) { throw null; }
public override bool Equals(object? obj) { throw null; }
public bool Equals(string other) { throw null; }
public override int GetHashCode() { throw null; }
public static bool operator ==(Azure.Core.ContentType left, Azure.Core.ContentType right) { throw null; }
public static implicit operator Azure.Core.ContentType (string contentType) { throw null; }
public static bool operator !=(Azure.Core.ContentType left, Azure.Core.ContentType right) { throw null; }
public override string ToString() { throw null; }
}
[System.Diagnostics.DebuggerDisplayAttribute("Content: {_body}")]
public partial class DynamicContent : Azure.Core.RequestContent
{
Expand Down
87 changes: 87 additions & 0 deletions sdk/core/Azure.Core.Experimental/src/ContentType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.Core
{
/// <summary>
/// Represents content type.
/// </summary>
public readonly struct ContentType : IEquatable<ContentType>, IEquatable<string>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pakrym / @chamons - could code gen make use of this type for produces/consumes instead of generating your own?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

{
private readonly string _contentType;

/// <summary>
/// application/json
/// </summary>
public static ContentType ApplicationJson { get; } = new ContentType("application/json");

/// <summary>
/// application/json
/// </summary>
public static ContentType ApplicationOctetStream { get; } = new ContentType("application/octet-stream");

/// <summary>
/// application/json
/// </summary>
public static ContentType TextPlain { get; } = new ContentType("text/plain");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scrolling through https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types, it might also be worth adding XML because it's still used by a few services like Storage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we ever have untyped (RequestContent, Stream, etc.) APIs that send XML?

/// <summary>
/// Creates an instance of <see cref="ContentType"/>.
/// </summary>
/// <param name="contentType">The content type string.</param>
public ContentType(string contentType)
{
Argument.AssertNotNull(contentType, nameof(contentType));

_contentType = contentType;
}

/// <summary>
/// Creates an instance of <see cref="ContentType"/>.
/// </summary>
/// <param name="contentType">The content type string.</param>
public static implicit operator ContentType(string contentType) => new ContentType(contentType);

/// <inheritdoc />
public bool Equals(ContentType other)
{
return string.Equals(_contentType, other._contentType, StringComparison.Ordinal);
}

/// <inheritdoc />
public bool Equals(string other)
=> string.Equals(_contentType, _contentType, StringComparison.Ordinal);

/// <inheritdoc />
public override bool Equals(object? obj)
=> (obj is ContentType other && Equals(other)) ||
(obj is string str && str.Equals(_contentType, StringComparison.Ordinal));

/// <inheritdoc />
public override int GetHashCode()
=> _contentType?.GetHashCode() ?? 0;

/// <summary>
/// Compares equality of two <see cref="ContentType"/> instances.
/// </summary>
/// <param name="left">The method to compare.</param>
/// <param name="right">The method to compare against.</param>
/// <returns><c>true</c> if <see cref="ContentType"/> values are equal for <paramref name="left"/> and <paramref name="right"/>, otherwise <c>false</c>.</returns>
public static bool operator ==(ContentType left, ContentType right)
=> left.Equals(right);

/// <summary>
/// Compares inequality of two <see cref="ContentType"/> instances.
/// </summary>
/// <param name="left">The method to compare.</param>
/// <param name="right">The method to compare against.</param>
/// <returns><c>true</c> if <see cref="ContentType"/> values are equal for <paramref name="left"/> and <paramref name="right"/>, otherwise <c>false</c>.</returns>
public static bool operator !=(ContentType left, ContentType right)
=> !left.Equals(right);

/// <inheritdoc />
public override string ToString() => _contentType == null?"":_contentType;
}
}
43 changes: 43 additions & 0 deletions sdk/core/Azure.Core.Experimental/tests/ContentTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using NUnit.Framework;

namespace Azure.Core.Tests
{
public class ContentTypeTests
{
[Test]
public void Basics()
{
ContentType contentType = default;
Assert.AreEqual("", contentType.ToString());
Assert.IsTrue(contentType.Equals(null));
Assert.IsTrue(contentType.Equals(new ContentType()));

string aj = "application/json";
contentType = ContentType.ApplicationJson;
Assert.AreEqual(aj, contentType.ToString());
Assert.IsTrue(contentType.Equals(aj));
Assert.IsTrue(contentType.Equals(new ContentType(aj)));
Assert.IsTrue(contentType.Equals((object)aj));
Assert.IsTrue(contentType.Equals((object)new ContentType(aj)));

string aos = "application/octet-stream";
contentType = ContentType.ApplicationOctetStream;
Assert.AreEqual(aos, contentType.ToString());
Assert.IsTrue(contentType.Equals(aos));
Assert.IsTrue(contentType.Equals(new ContentType(aos)));
Assert.IsTrue(contentType.Equals((object)aos));
Assert.IsTrue(contentType.Equals((object)new ContentType(aos)));

string pt = "text/plain";
contentType = ContentType.TextPlain;
Assert.AreEqual(pt, contentType.ToString());
Assert.IsTrue(contentType.Equals(pt));
Assert.IsTrue(contentType.Equals(new ContentType(pt)));
Assert.IsTrue(contentType.Equals((object)pt));
Assert.IsTrue(contentType.Equals((object)new ContentType(pt)));
}
}
}