-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Added ContentType Extensible Enum #20768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
50c15c8
204a23d
08a9b35
ff431f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> | ||
| { | ||
| 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"); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
| 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))); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes