Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into IM-182-Create-a-fo…
Browse files Browse the repository at this point in the history
…rm-for-updating-user
  • Loading branch information
kristinaBc3 committed Feb 28, 2024
2 parents fbd7428 + b3e3792 commit 7bd85ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.apache.commons.lang3.StringUtils;
import org.integratedmodelling.klab.Version;
import org.integratedmodelling.klab.api.provenance.IArtifact.Type;
import org.integratedmodelling.klab.exceptions.KlabResourceAccessException;
import org.integratedmodelling.klab.utils.DOIReader;

Expand Down Expand Up @@ -94,4 +95,14 @@ public static String readLicense(JSONObject collection) {
return null;
}

public static Type inferValueType(String key) {
if (StringUtils.isNumeric(key)) {
return Type.NUMBER;
} else if ("true".equalsIgnoreCase(key) || "false".equalsIgnoreCase(key)) {
return Type.BOOLEAN;
}
// As we are reading a JSON, text is our safest default option
return Type.TEXT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.integratedmodelling.kim.api.IParameters;
import org.integratedmodelling.klab.api.data.IGeometry;
import org.integratedmodelling.klab.api.data.IResource;
Expand Down Expand Up @@ -81,7 +82,9 @@ private Type readRasterDataType(JSONObject asset) {
if (!asset.has("raster:bands")) {
return null;
}
if (asset.getJSONArray("raster:bands").isEmpty() || asset.getJSONArray("raster:bands").getJSONObject(0).has("data_type")) {
if (asset.getJSONArray("raster:bands").isEmpty()
|| !asset.getJSONArray("raster:bands").getJSONObject(0).has("data_type")) {
// We could assume that most rasters are numeric. When in doubt, we could set the default to Number
return null;
}
String type = asset.getJSONArray("raster:bands").getJSONObject(0).getString("data_type");
Expand All @@ -94,18 +97,6 @@ private Type readRasterDataType(JSONObject asset) {
return null;
}

private Type getCodelistType(Object value) {
if (value instanceof Number) {
return Type.NUMBER;
} else if (value instanceof String) {
return Type.TEXT;
} else if (value instanceof Boolean) {
return Type.BOOLEAN;
}
// As we are reading a JSON, text is our safest default option
return Type.TEXT;
}

private CodelistReference populateCodelist(String assetId, Map<String, Object> vals) {
CodelistReference codelist = new CodelistReference();
codelist.setId(assetId.toUpperCase());
Expand All @@ -118,7 +109,7 @@ private CodelistReference populateCodelist(String assetId, Map<String, Object> v
direct.getMappings().add(new Pair<>(code.getKey(), (String)code.getValue()));
codelist.getCodeDescriptions().put(code.getKey(), (String)code.getValue());
});
Type type = getCodelistType(vals.entrySet().stream().findFirst().get());
Type type = STACUtils.inferValueType(vals.entrySet().stream().findFirst().get().getKey());
codelist.setType(type);
codelist.setDirectMapping(direct);
codelist.setInverseMapping(inverse);
Expand Down

0 comments on commit 7bd85ce

Please sign in to comment.