-
Notifications
You must be signed in to change notification settings - Fork 111
[OSJC-205] Access raw JSON as object map #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...java/com/openshift/internal/restclient/capability/resources/PropertyAccessCapability.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2016 Red Hat, Inc. | ||
| * Distributed under license by Red Hat, Inc. All rights reserved. | ||
| * This program is made available under the terms of the | ||
| * Eclipse Public License v1.0 which accompanies this distribution, | ||
| * and is available at http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Red Hat, Inc. - initial API and implementation | ||
| ******************************************************************************/ | ||
| package com.openshift.internal.restclient.capability.resources; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static com.openshift.internal.util.JBossDmrExtentions.*; | ||
|
|
||
| import org.jboss.dmr.ModelNode; | ||
|
|
||
| import com.openshift.internal.restclient.model.KubernetesResource; | ||
| import com.openshift.restclient.capability.resources.IPropertyAccessCapability; | ||
| import com.openshift.restclient.model.IResource; | ||
|
|
||
| public class PropertyAccessCapability implements IPropertyAccessCapability { | ||
|
|
||
| private KubernetesResource resource; | ||
|
|
||
| public PropertyAccessCapability(IResource resource) { | ||
| if(resource instanceof KubernetesResource) { | ||
| this.resource = (KubernetesResource) resource; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String asString(String path) { | ||
| ModelNode node = get(resource.getNode(), null, path); | ||
| if(!node.isDefined()) { | ||
| throw new UnresolvablePathException(); | ||
| } | ||
| return node.asString(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> asMap(String path) { | ||
| return asMap(get(resource.getNode(), null, path)); | ||
| } | ||
|
|
||
| private Map<String, Object> asMap(ModelNode node) { | ||
| if(!node.isDefined()) { | ||
| throw new UnresolvablePathException(); | ||
| } | ||
| Map<String, Object> result = new HashMap<>(); | ||
| for (String key : node.keys()) { | ||
| ModelNode value = node.get(key); | ||
| switch(value.getType()) { | ||
| case OBJECT: | ||
| result.put(key, asMap(value)); | ||
| break; | ||
| case LIST: | ||
| result.put(key, asList(value)); | ||
| case STRING: | ||
| result.put(key, value.asString()); | ||
| break; | ||
| case INT: | ||
| result.put(key, value.asInt()); | ||
| break; | ||
| case BIG_INTEGER: | ||
| result.put(key, value.asBigInteger()); | ||
| break; | ||
| case BIG_DECIMAL: | ||
| result.put(key, value.asBigDecimal()); | ||
| break; | ||
| case LONG: | ||
| result.put(key, value.asBigDecimal()); | ||
| break; | ||
| case BOOLEAN: | ||
| result.put(key, value.asBoolean()); | ||
| break; | ||
| default: | ||
| result.put(key, value.asString()); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private List<Object> asList(ModelNode node) { | ||
| List<Object> list = new ArrayList<>(); | ||
| for (ModelNode entry : node.asList()) { | ||
| switch(entry.getType()) { | ||
| case OBJECT: | ||
| list.add(asMap(entry)); | ||
| break; | ||
| case LIST: | ||
| list.add(asList(entry)); | ||
| case STRING: | ||
| list.add(entry.asString()); | ||
| break; | ||
| case INT: | ||
| list.add(entry.asInt()); | ||
| break; | ||
| case BIG_INTEGER: | ||
| list.add(entry.asBigInteger()); | ||
| break; | ||
| case BIG_DECIMAL: | ||
| list.add(entry.asBigDecimal()); | ||
| break; | ||
| case LONG: | ||
| list.add(entry.asBigDecimal()); | ||
| break; | ||
| case BOOLEAN: | ||
| list.add(entry.asBoolean()); | ||
| break; | ||
| default: | ||
| list.add(entry.asString()); | ||
| } | ||
|
|
||
| } | ||
| return list; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSupported() { | ||
| return resource != null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return this.getClass().getSimpleName(); | ||
| } | ||
|
|
||
| } |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/openshift/restclient/capability/resources/IPropertyAccessCapability.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2016 Red Hat, Inc. | ||
| * Distributed under license by Red Hat, Inc. All rights reserved. | ||
| * This program is made available under the terms of the | ||
| * Eclipse Public License v1.0 which accompanies this distribution, | ||
| * and is available at http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Red Hat, Inc. - initial API and implementation | ||
| ******************************************************************************/ | ||
| package com.openshift.restclient.capability.resources; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import com.openshift.restclient.capability.ICapability; | ||
|
|
||
| /** | ||
| * A mechanism to access the underlying content of a json | ||
| * structure | ||
| * | ||
| * Relies on a dot delimited path to indentify the root of the entity | ||
| * being returned (e.g. 'metadata.labels'). This does not provide | ||
| * a mechanism to retrieve properties across an array kind. | ||
| * | ||
| * @author jeff.cantrill | ||
| * | ||
| */ | ||
| public interface IPropertyAccessCapability extends ICapability { | ||
|
|
||
| /** | ||
| * | ||
| * @param path | ||
| * @return | ||
| * | ||
| * @throws @{@link UnresolvablePathException} when the path | ||
| * does not resolve to an existing node | ||
| */ | ||
| Map<String, Object> asMap(String path); | ||
|
|
||
| /** | ||
| * Return the string value to the path | ||
| * @param path | ||
| * @return | ||
| * @throws @{@link UnresolvablePathException} when the path | ||
| * does not resolve to an existing node | ||
| */ | ||
| String asString(String path); | ||
|
|
||
| /** | ||
| * The exception thrown when a path given to the capability is | ||
| * unresolvable | ||
| * | ||
| * @author jeff.cantrill | ||
| * | ||
| */ | ||
| public static class UnresolvablePathException extends RuntimeException{ | ||
|
|
||
| /** | ||
| * | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice javadoc
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sarcasm? |
||
| */ | ||
| private static final long serialVersionUID = 2422016683166925224L; | ||
|
|
||
|
|
||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
.../com/openshift/internal/restclient/capability/resources/PropertyAccessCapabilityTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2016 Red Hat, Inc. | ||
| * Distributed under license by Red Hat, Inc. All rights reserved. | ||
| * This program is made available under the terms of the | ||
| * Eclipse Public License v1.0 which accompanies this distribution, | ||
| * and is available at http://www.eclipse.org/legal/epl-v10.html | ||
| * | ||
| * Contributors: | ||
| * Red Hat, Inc. - initial API and implementation | ||
| ******************************************************************************/ | ||
| package com.openshift.internal.restclient.capability.resources; | ||
|
|
||
| import static org.junit.Assert.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.jboss.dmr.ModelNode; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import com.openshift.internal.restclient.model.BuildConfig; | ||
| import com.openshift.restclient.IClient; | ||
| import com.openshift.restclient.capability.resources.IPropertyAccessCapability; | ||
| import com.openshift.restclient.capability.resources.IPropertyAccessCapability.UnresolvablePathException; | ||
| import com.openshift.restclient.utils.Samples; | ||
|
|
||
| public class PropertyAccessCapabilityTest { | ||
|
|
||
| private ModelNode node; | ||
| private IPropertyAccessCapability cap; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| IClient client = mock(IClient.class); | ||
| node = ModelNode.fromJSONString(Samples.V1_BUILD_CONFIG.getContentAsString()); | ||
| node.get(new String[] {"spec", "strategy","sourceStrategy", "xyz"}).set(1986); | ||
| BuildConfig config = new BuildConfig(node, client, new HashMap<>()); | ||
| cap = new PropertyAccessCapability(config); | ||
| } | ||
| @Test(expected=UnresolvablePathException.class) | ||
| public void testAsMapWhenPathIsNotFound() { | ||
| cap.asMap("foo.strategy.sourceStrategy.from"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAsString() { | ||
| assertEquals("1986", cap.asString("spec.strategy.sourceStrategy.xyz")); | ||
| } | ||
|
|
||
| @Test(expected=UnresolvablePathException.class) | ||
| public void testAsWhenPathIsNotFound() { | ||
| cap.asString("spec.strategy.sourceStrategy.xyzzz"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAsMap() { | ||
| Map<String, Object> from = new HashMap<>(); | ||
| from.put("kind","ImageStreamTag"); | ||
| from.put("name","ruby-20-centos7:latest"); | ||
| Map<String, Object> exp = new HashMap<>(); | ||
| exp.put("from", from); | ||
| exp.put("incremental", true); | ||
| exp.put("scripts", "aLocation"); | ||
| exp.put("xyz", 1986); | ||
|
|
||
| // @TODO Why doesnt this validate? | ||
| // Map<String, Object> envEntry = new HashMap<>(); | ||
| // envEntry.put("name", "foo"); | ||
| // envEntry.put("value", "bar"); | ||
| // List<Object> env = new ArrayList<>(); | ||
| // env.add(envEntry); | ||
| // | ||
| // exp.put("env", env); | ||
| Map<String, Object> act = cap.asMap("spec.strategy.sourceStrategy.from"); | ||
| assertEquals(from, act); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and how bad is it arrays are not supported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont see it as a big deal since you are able to retrieve the entire JSON as a map of string to Objects. But the bigger question is what does it mean to retrieve an entry across an array? Do we allow you to do something like "foo.[3].bar" where you retrieve bar value of the third entry? I think this is probably good enough but if we can come up with a could API, I'm willing to adjust it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also believe that we should not push this too far since we'd start to get in the area of json parsers which I believe we dont want to do.
But then I'm not sure how bad it is that we dont support arrays, looking at the json openshift returns this seems important?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree with @adietish that we want this API as minimal as possible. It's a last resort to access something that isn't exposed with a proper getter.