/
PSRuleMemberInfo.cs
99 lines (80 loc) · 2.3 KB
/
PSRuleMemberInfo.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Newtonsoft.Json;
using PSRule.Data;
using System.Collections.Generic;
using System.Management.Automation;
namespace PSRule.Runtime
{
internal sealed class PSRuleTargetInfo : PSMemberInfo
{
internal const string PropertyName = "_PSRule";
public PSRuleTargetInfo()
{
SetMemberName(PropertyName);
if (Source == null)
Source = new List<TargetSourceInfo>();
}
private PSRuleTargetInfo(PSRuleTargetInfo targetInfo)
: this()
{
if (targetInfo == null)
return;
Source = targetInfo.Source;
}
public string Path
{
get
{
if (Source.Count == 0)
return null;
return Source[0].File;
}
}
[JsonProperty(PropertyName = "source")]
public List<TargetSourceInfo> Source { get; internal set; }
[JsonIgnore]
public override PSMemberTypes MemberType => PSMemberTypes.PropertySet;
[JsonIgnore]
public override string TypeNameOfValue => typeof(PSRuleTargetInfo).FullName;
[JsonIgnore]
public override object Value
{
get => this;
set { }
}
public override PSMemberInfo Copy()
{
return new PSRuleTargetInfo(this);
}
internal void Combine(PSRuleTargetInfo targetInfo)
{
if (targetInfo == null)
return;
Source.AddUnique(targetInfo?.Source);
}
internal void WithSource(TargetSourceInfo source)
{
if (source == null)
return;
if (Source.Count == 0)
{
Source.Add(source);
return;
}
if (Source[0].File == null)
Source[0].File = source.File;
}
internal void WithSource(int lineNumber, int linePosition)
{
if (Source.Count > 0)
return;
var s = new TargetSourceInfo
{
Line = lineNumber,
Position = linePosition
};
Source.Add(s);
}
}
}