forked from jenkinsci/configuration-as-code-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScalar.java
More file actions
190 lines (160 loc) · 4.3 KB
/
Scalar.java
File metadata and controls
190 lines (160 loc) · 4.3 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package io.jenkins.plugins.casc.model;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.stream.IntStream;
/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
*/
public final class Scalar implements CNode, CharSequence {
private static final String SECRET_VALUE_STRING = "****";
private String value;
private Format format;
private boolean raw;
private Source source;
private boolean sensitive;
private boolean encrypted;
private boolean printableWhenEmpty = false;
public enum Format {
STRING,
MULTILINESTRING,
BOOLEAN,
NUMBER,
FLOATING
}
public Scalar(String value, Source source) {
this(value);
this.source = source;
}
public Scalar(String value) {
this.value = value;
this.format = value.contains("\n") ? Format.MULTILINESTRING : Format.STRING;
this.raw = false;
}
public Scalar(Enum instance) {
this.value = instance.name();
this.format = Format.STRING;
this.raw = true;
}
public Scalar(Boolean instance) {
this.value = String.valueOf(instance);
this.format = Format.BOOLEAN;
this.raw = true;
}
public Scalar(Number instance) {
this.value = String.valueOf(instance);
if (instance instanceof Float || instance instanceof Double) {
this.format = Format.STRING;
} else {
this.raw = true;
this.format = Format.NUMBER;
}
}
@Override
public Type getType() {
return Type.SCALAR;
}
public Format getFormat() {
return format;
}
public boolean isRaw() {
return raw;
}
@Override
public Scalar asScalar() {
return this;
}
/**
* Gets value of the scalar for export.
* @return Value of the scalar if not {@link #isMasked()},
* {@link #SECRET_VALUE_STRING} otherwise.
* Encrypted sensitive data will be returned as is.
*
*/
public String getValue() {
return isMasked() ? SECRET_VALUE_STRING : value;
}
/**
* Check whether the scalar value should be masked in the output.
* @return {@code true} if the value is masked
* @since 1.25
*/
public boolean isMasked() {
return sensitive && !encrypted;
}
/**
* Sets the sensitive flag.
* It indicates that the scalar represents a sensitive argument (secret or other restricted data).
* @param sensitive value to set
* @return Object instance
* @since 1.25
*/
public Scalar sensitive(boolean sensitive) {
this.sensitive = sensitive;
return this;
}
/**
* Indicates that the data is encrypted and hence safe to be exported.
* @param encrypted Value to set
* @return Object instance
* @since 1.25
*/
public Scalar encrypted(boolean encrypted) {
this.encrypted = encrypted;
return this;
}
@Override
@SuppressWarnings("deprecation")
public boolean isSensitiveData() {
return sensitive;
}
@NonNull
@Override
public String toString() {
return value;
}
@NonNull
@Override
public IntStream chars() {
return value.chars();
}
@NonNull
@Override
public IntStream codePoints() {
return value.codePoints();
}
@Override
public int length() {
return value.length();
}
@Override
public char charAt(int index) {
return value.charAt(index);
}
@NonNull
@Override
public CharSequence subSequence(int start, int end) {
return value.subSequence(start, end);
}
public Source getSource() {
return source;
}
@Override
public boolean isPrintableWhenEmpty() {
return printableWhenEmpty;
}
public void setPrintableWhenEmpty(boolean print) {
this.printableWhenEmpty = print;
}
@Override
public CNode clone() {
return new Scalar(this);
}
private Scalar(Scalar it) {
this.value = it.value;
this.format = it.format;
this.raw = it.raw;
this.source = it.source;
this.sensitive = it.sensitive;
this.encrypted = it.encrypted;
this.printableWhenEmpty = it.printableWhenEmpty;
}
}