Skip to content

Commit d188a59

Browse files
committed
Introduce BootEnumValueParser
Signed-off-by: BoykoAlex <[email protected]>
1 parent 7c57b68 commit d188a59

File tree

3 files changed

+73
-27
lines changed

3 files changed

+73
-27
lines changed

headless-services/commons/commons-util/src/main/java/org/springframework/ide/vscode/commons/util/EnumValueParser.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014, 2025 Pivotal, Inc.
2+
* Copyright (c) 2014-2017 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,11 +11,10 @@
1111
package org.springframework.ide.vscode.commons.util;
1212

1313
import java.util.Collection;
14-
import java.util.Set;
14+
import java.util.Collections;
1515
import java.util.TreeSet;
1616
import java.util.concurrent.Callable;
1717
import java.util.function.Supplier;
18-
import java.util.stream.Collectors;
1918

2019
import com.google.common.collect.ImmutableSet;
2120

@@ -31,7 +30,6 @@ public class EnumValueParser implements ValueParser {
3130
private Supplier<PartialCollection<String>> values;
3231
private final boolean longRunning;
3332

34-
private transient Set<String> _canonicalValues;
3533

3634
public EnumValueParser(String typeName, String... values) {
3735
this(typeName, ImmutableSet.copyOf(values));
@@ -72,14 +70,24 @@ public Object parse(String str) throws Exception {
7270
throw errorOnBlank(createBlankTextErrorMessage());
7371
}
7472

75-
Set<String> canonicalValues = getCanonicalValues();
76-
77-
if (canonicalValues == null || canonicalValues.contains(getCanonicalName(str))) {
73+
PartialCollection<String> values = this.values.get();
74+
75+
// If values is not fully known then just assume the str is acceptable.
76+
if (values == null || !values.isComplete() || hasMatchingValue(str, values.getElements())) {
7877
return str;
7978
} else {
80-
throw errorOnParse(createErrorMessage(str, this.values.get().getElements()));
79+
throw errorOnParse(createErrorMessage(str, values.getElements()));
8180
}
8281
}
82+
83+
protected final Collection<String> getAllKnownValues() {
84+
PartialCollection<String> partialCollection = this.values.get();
85+
return partialCollection == null ? Collections.emptyList() : this.values.get().getElements();
86+
}
87+
88+
protected boolean hasMatchingValue(String str, Collection<String> values) {
89+
return values.contains(str);
90+
}
8391

8492
protected String createBlankTextErrorMessage() {
8593
return "'" + typeName + "'" + " cannot be blank.";
@@ -100,21 +108,4 @@ protected Exception errorOnBlank(String message) {
100108
public boolean longRunning() {
101109
return this.longRunning ;
102110
}
103-
104-
private Set<String> getCanonicalValues() {
105-
PartialCollection<String> partialCollection = this.values.get();
106-
if (partialCollection != null) {
107-
_canonicalValues = partialCollection.getElements().stream().map(EnumValueParser::getCanonicalName).collect(Collectors.toSet());
108-
}
109-
return _canonicalValues;
110-
}
111-
112-
static String getCanonicalName(String name) {
113-
StringBuilder canonicalName = new StringBuilder(name.length());
114-
name.chars()
115-
.filter(Character::isLetterOrDigit)
116-
.map(Character::toLowerCase)
117-
.forEach((c) -> canonicalName.append((char) c));
118-
return canonicalName.toString();
119-
}
120111
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/metadata/types/TypeUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.ide.vscode.boot.metadata.hints.StsValueHint;
4545
import org.springframework.ide.vscode.boot.metadata.util.DeprecationUtil;
4646
import org.springframework.ide.vscode.boot.metadata.util.PropertyDocUtils;
47+
import org.springframework.ide.vscode.boot.properties.reconcile.BootEnumValueParser;
4748
import org.springframework.ide.vscode.commons.java.Flags;
4849
import org.springframework.ide.vscode.commons.java.IField;
4950
import org.springframework.ide.vscode.commons.java.IJavaElement;
@@ -55,7 +56,6 @@
5556
import org.springframework.ide.vscode.commons.util.ArrayUtils;
5657
import org.springframework.ide.vscode.commons.util.Assert;
5758
import org.springframework.ide.vscode.commons.util.CollectionUtil;
58-
import org.springframework.ide.vscode.commons.util.EnumValueParser;
5959
import org.springframework.ide.vscode.commons.util.LazyProvider;
6060
import org.springframework.ide.vscode.commons.util.MimeTypes;
6161
import org.springframework.ide.vscode.commons.util.Renderables;
@@ -272,7 +272,7 @@ public ValueParser getValueParser(Type type) {
272272
//Note, technically if 'enumValues is empty array' this means something different
273273
// from when it is null. An empty array means a type that has no values, so
274274
// assigning anything to it is an error.
275-
return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
275+
return new BootEnumValueParser(niceTypeName(type), getBareValues(enumValues));
276276
}
277277
if (isMap(type)) {
278278
//Trying to parse map types from scalars is not possible. Thus we
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Broadcom, Inc.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Broadcom, Inc. - initial API and implementation
10+
*******************************************************************************/
11+
package org.springframework.ide.vscode.boot.properties.reconcile;
12+
13+
import java.util.Arrays;
14+
import java.util.Set;
15+
import java.util.stream.Collectors;
16+
17+
import org.springframework.ide.vscode.commons.util.EnumValueParser;
18+
import org.springframework.ide.vscode.commons.util.StringUtil;
19+
20+
public class BootEnumValueParser extends EnumValueParser {
21+
22+
private Set<String> canonicalValues;
23+
24+
private static String getCanonicalName(String name) {
25+
StringBuilder canonicalName = new StringBuilder(name.length());
26+
name.chars()
27+
.filter(Character::isLetterOrDigit)
28+
.map(Character::toLowerCase)
29+
.forEach((c) -> canonicalName.append((char) c));
30+
return canonicalName.toString();
31+
}
32+
33+
public BootEnumValueParser(String typeName, String[] values) {
34+
super(typeName, values);
35+
this.canonicalValues = Arrays.stream(values).map(BootEnumValueParser::getCanonicalName).collect(Collectors.toSet());
36+
}
37+
38+
@Override
39+
public Object parse(String str) throws Exception {
40+
// IMPORTANT: check the text FIRST before fetching values
41+
// from the hints provider, as the hints provider may be expensive when
42+
// resolving values
43+
if (!StringUtil.hasText(str)) {
44+
throw errorOnBlank(createBlankTextErrorMessage());
45+
}
46+
47+
// If values is not fully known then just assume the str is acceptable.
48+
if (canonicalValues.contains(getCanonicalName(str))) {
49+
return str;
50+
} else {
51+
throw errorOnParse(createErrorMessage(str, getAllKnownValues()));
52+
}
53+
}
54+
55+
}

0 commit comments

Comments
 (0)