-
-
Notifications
You must be signed in to change notification settings - Fork 723
/
ResultValue.cs
106 lines (93 loc) · 3.19 KB
/
ResultValue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using HotChocolate.Properties;
#nullable enable
namespace HotChocolate.Execution;
/// <summary>
/// Represents an entry in a <see cref="IResultMap"/>
/// </summary>
public readonly struct ResultValue : IEquatable<ResultValue>
{
/// <summary>
/// Creates a new result value.
/// </summary>
/// <param name="name">The name of the entry.</param>
/// <param name="value">The value of the entry.</param>
/// <param name="isNullable">
/// Specifies if the <paramref name="value"/> is allowed to be null.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="name"/> is <c>null</c> or <see cref="string.Empty"/>.
/// </exception>
public ResultValue(string name, object? value, bool isNullable = true)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(
AbstractionResources.ResultValue_NameIsNullOrEmpty,
nameof(name));
}
Name = name;
Value = value;
IsNullable = isNullable;
}
/// <summary>
/// Gets the name of this <see cref="IResultMap"/> entry.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the value of this <see cref="IResultMap"/> entry.
/// </summary>
public object? Value { get; }
/// <summary>
/// Specifies if <see cref="Value"/> is allowed to be empty.
/// </summary>
public bool IsNullable { get; }
/// <summary>
/// Specifies if this entry is fully initialized.
/// </summary>
public bool IsInitialized => Name is not null;
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="obj" /> and this instance are the same type
/// and represent the same value; otherwise, <see langword="false" />.
/// </returns>
public override bool Equals(object? obj)
=> obj is ResultValue value && Equals(value);
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the
/// <paramref name="other" /> parameter; otherwise, <see langword="false" />.
/// </returns>
public bool Equals(ResultValue other)
{
if (IsInitialized != other.IsInitialized)
{
return false;
}
if (IsInitialized == false)
{
return true;
}
return string.Equals(Name, other.Name, StringComparison.Ordinal) &&
Equals(Value, other.Value);
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer that is the hash code for this instance.
/// </returns>
public override int GetHashCode()
{
unchecked
{
var hash = (Name?.GetHashCode() ?? 0) * 3;
hash ^= (Value?.GetHashCode() ?? 0) * 7;
return hash;
}
}
}