From adb27f7862403bc8880b5d4e84c479facf19a30d Mon Sep 17 00:00:00 2001 From: Kris De Volder Date: Fri, 20 Jan 2017 15:41:38 -0800 Subject: [PATCH] Cleanup and simplify uses of DynamicSchemaContext --- .../yaml/completion/YTypeAssistContext.java | 7 +- .../SchemaBasedYamlASTReconciler.java | 2 +- .../commons/yaml/schema/YTypeFactory.java | 70 ++++--------------- .../vscode/commons/yaml/schema/YTypeUtil.java | 6 +- 4 files changed, 19 insertions(+), 66 deletions(-) diff --git a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/YTypeAssistContext.java b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/YTypeAssistContext.java index e07c817ee..deeb5115d 100644 --- a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/YTypeAssistContext.java +++ b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/completion/YTypeAssistContext.java @@ -80,7 +80,7 @@ public class YTypeAssistContext extends AbstractYamlAssistContext { int queryOffset = offset - query.length(); SNode contextNode = getContextNode(); DynamicSchemaContext dynamicCtxt = getSchemaContext(); - List properties = typeUtil.getProperties(type, dynamicCtxt); + List properties = typeUtil.getProperties(type); if (CollectionUtil.hasElements(properties)) { ArrayList proposals = new ArrayList<>(properties.size()); Set definedProps = dynamicCtxt.getDefinedProperties(); @@ -180,12 +180,11 @@ public class YTypeAssistContext extends AbstractYamlAssistContext { @Override public YamlAssistContext traverse(YamlPathSegment s) throws Exception { if (s.getType()==YamlPathSegmentType.VAL_AT_KEY) { - DynamicSchemaContext dynamicCtxt = getSchemaContext(); if (typeUtil.isSequencable(type) || typeUtil.isMap(type)) { return contextWith(s, typeUtil.getDomainType(type)); } String key = s.toPropString(); - Map subproperties = typeUtil.getPropertiesMap(type, dynamicCtxt); + Map subproperties = typeUtil.getPropertiesMap(type); if (subproperties!=null) { return contextWith(s, getType(subproperties.get(key))); } @@ -265,6 +264,6 @@ public class YTypeAssistContext extends AbstractYamlAssistContext { } private YTypedProperty getProperty(String name) { - return typeUtil.getPropertiesMap(getType(), getSchemaContext()).get(name); + return typeUtil.getPropertiesMap(getType()).get(name); } } diff --git a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/reconcile/SchemaBasedYamlASTReconciler.java b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/reconcile/SchemaBasedYamlASTReconciler.java index 17b5c497e..8bdbbcf5b 100644 --- a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/reconcile/SchemaBasedYamlASTReconciler.java +++ b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/reconcile/SchemaBasedYamlASTReconciler.java @@ -118,7 +118,7 @@ public class SchemaBasedYamlASTReconciler implements YamlASTReconciler { reconcile(doc, valueAt(path, key), entry.getValueNode(), typeUtil.getDomainType(type)); } } else if (typeUtil.isBean(type)) { - Map beanProperties = typeUtil.getPropertiesMap(type, schemaContext); + Map beanProperties = typeUtil.getPropertiesMap(type); checkRequiredProperties(map, type, beanProperties); for (NodeTuple entry : map.getValue()) { Node keyNode = entry.getKeyNode(); diff --git a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeFactory.java b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeFactory.java index 3e594122d..be524f4b6 100644 --- a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeFactory.java +++ b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeFactory.java @@ -89,13 +89,13 @@ public class YTypeFactory { } @Override - public Map getPropertiesMap(YType type, DynamicSchemaContext dc) { - return ((AbstractType)type).getPropertiesMap(dc); + public Map getPropertiesMap(YType type) { + return ((AbstractType)type).getPropertiesMap(); } @Override - public List getProperties(YType type, DynamicSchemaContext dc) { - return ((AbstractType)type).getProperties(dc); + public List getProperties(YType type) { + return ((AbstractType)type).getProperties(); } @Override @@ -207,11 +207,11 @@ public class YTypeFactory { return ImmutableList.of(); } - public List getProperties(DynamicSchemaContext dc) { + public List getProperties() { return Collections.unmodifiableList(propertyList); } - public Map getPropertiesMap(DynamicSchemaContext dc) { + public Map getPropertiesMap() { if (cachedPropertyMap==null) { cachedPropertyMap = new LinkedHashMap<>(); for (YTypedProperty p : propertyList) { @@ -412,25 +412,6 @@ public class YTypeFactory { return true; } - /** - * Deprecated. This method disregards {@link DynamicSchemaContext}. This may be - * alright for Schemas which don't rely on it but it will result in incorrect/inaccurate - * behavior for Schemas that do. - */ - @Deprecated - public List getProperties() { - return getProperties(DynamicSchemaContext.NULL); - } - - /** - * Deprecated. This method disregards {@link DynamicSchemaContext}. This may be - * alright for Schemas which don't rely on it but it will result in incorrect/inaccurate - * behavior for Schemas that do. - */ - @Deprecated - public Map getPropertiesMap() { - return getPropertiesMap(DynamicSchemaContext.NULL); - } } public static class YAtomicType extends AbstractType { @@ -477,7 +458,7 @@ public class YTypeFactory { private String findPrimary(AbstractType t, List types) { //Note: passing null dynamic context below is okay, assuming the properties in YBeanType // do not care about dynamic context. - for (YTypedProperty p : t.getProperties(DynamicSchemaContext.NULL)) { + for (YTypedProperty p : t.getProperties()) { String name = p.getName(); if (isUniqueFor(name, t, types)) { return name; @@ -491,7 +472,7 @@ public class YTypeFactory { if (other!=t) { //Note: passing null dynamic context below is okay, assuming the properties in YBeanType // do not care about dynamic context. - if (other.getPropertiesMap(DynamicSchemaContext.NULL).containsKey(name)) { + if (other.getPropertiesMap().containsKey(name)) { return false; } } @@ -508,38 +489,11 @@ public class YTypeFactory { } @Override - public Map getPropertiesMap(DynamicSchemaContext dc) { - return asMap(getProperties(dc)); - } - - private Map asMap(List properties) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - for (YTypedProperty p : properties) { - builder.put(p.getName(), p); - } - return builder.build(); - } - - @Override - public List getProperties(DynamicSchemaContext dc) { - Set existingProps = dc.getDefinedProperties(); - if (!existingProps.isEmpty()) { - Builder builder = ImmutableList.builder(); - for (Entry entry : typesByPrimary().entrySet()) { - String primaryName = entry.getKey(); - if (existingProps.contains(primaryName)) { - builder.addAll(entry.getValue().getProperties(dc)); - break; - } - } - //Add 'shared' properties too: - builder.addAll(super.getProperties(dc)); - return builder.build(); - } + public List getProperties() { //Reaching here means we couldn't guess the type from existing props. //We'll just return the primary properties, these are good to give as hints //then, since at least one of them should be added. - return getPrimaryProps(dc); + return getPrimaryProps(); } private synchronized Map typesByPrimary() { @@ -559,11 +513,11 @@ public class YTypeFactory { return typesByPrimary; } - private List getPrimaryProps(DynamicSchemaContext dc) { + private List getPrimaryProps() { if (primaryProps==null) { Builder builder = ImmutableList.builder(); for (Entry entry : typesByPrimary().entrySet()) { - builder.add(entry.getValue().getPropertiesMap(dc).get(entry.getKey())); + builder.add(entry.getValue().getPropertiesMap().get(entry.getKey())); } primaryProps = builder.build(); } diff --git a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeUtil.java b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeUtil.java index b0348789d..ef4e518bd 100644 --- a/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeUtil.java +++ b/vscode-extensions/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/schema/YTypeUtil.java @@ -35,13 +35,13 @@ public interface YTypeUtil { ValueParser getValueParser(YType type, DynamicSchemaContext dc); //TODO: only one of these two should be enough? - List getProperties(YType type, DynamicSchemaContext dc); - Map getPropertiesMap(YType yType, DynamicSchemaContext dc); + List getProperties(YType type); + Map getPropertiesMap(YType yType); /** * Given a {@link DynamicSchemaContext} attempt to get a more specific type, as * may be inferred by stuff present in the context. If not enough information is - * present in the context to narrow the type, then the type itself + * present in the context to narrow the type, then the type itself * should be returned. */ YType inferMoreSpecificType(YType type, DynamicSchemaContext dc);