forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TargetSourceInfo.cs
89 lines (72 loc) · 2.38 KB
/
TargetSourceInfo.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Newtonsoft.Json;
using System;
using System.IO;
using System.Management.Automation;
namespace PSRule.Data
{
public sealed class TargetSourceInfo
{
private const string PROPERTY_FILE = "file";
private const string PROPERTY_LINE = "line";
private const string PROPERTY_POSITION = "position";
public TargetSourceInfo()
{
// Do nothing
}
internal TargetSourceInfo(InputFileInfo info)
{
File = info.FullName;
}
internal TargetSourceInfo(FileInfo info)
{
File = info.FullName;
}
internal TargetSourceInfo(Uri uri)
{
File = uri.AbsoluteUri;
}
[JsonProperty(PropertyName = PROPERTY_FILE)]
public string File { get; internal set; }
[JsonProperty(PropertyName = PROPERTY_LINE)]
public int? Line { get; internal set; }
[JsonProperty(PropertyName = PROPERTY_POSITION)]
public int? Position { get; internal set; }
public bool Equals(TargetSourceInfo other)
{
return other != null &&
File == other.File &&
Line == other.Line &&
Position == other.Position;
}
public override int GetHashCode()
{
unchecked // Overflow is fine
{
int hash = 17;
hash = hash * 23 + (File != null ? File.GetHashCode() : 0);
hash = hash * 23 + (Line.HasValue ? Line.Value.GetHashCode() : 0);
hash = hash * 23 + (Position.HasValue ? Position.Value.GetHashCode() : 0);
return hash;
}
}
public static TargetSourceInfo Create(object o)
{
if (o is PSObject pso)
return Create(pso);
return null;
}
public static TargetSourceInfo Create(PSObject o)
{
var result = new TargetSourceInfo();
if (o.TryProperty(PROPERTY_FILE, out string file))
result.File = file;
if (o.TryProperty(PROPERTY_LINE, out int line))
result.Line = line;
if (o.TryProperty(PROPERTY_POSITION, out int position))
result.Position = position;
return result;
}
}
}