Skip to content
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

Extract option supports simple array #502

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Zowe Common C Changelog

## `3.1.0`
- Enhancement: `configmgr extract` option supports simple array (#502)
- Bugfix: removed "ByteOutputStream" debug message, which was part of the `zwe` command output (#491)
- Bugfix: HEAPPOOLS and HEAPPOOLS64 no longer need to be set to OFF for configmgr (#497)
- Enhancement: module registry (#405)
Expand Down
65 changes: 47 additions & 18 deletions c/configmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,29 +1161,57 @@ int cfgGetBooleanC(ConfigManager *mgr, const char *configName, bool *result, int
}
}

static void extractText(ConfigManager *mgr, const char *configName, JsonPointer *jp, FILE *out){
static int printPrimitiveDataType(Json *value, FILE *out) {
if (jsonIsString(value)){
fprintf(out,"%s",jsonAsString(value));
} else if (jsonIsInt64(value)){
fprintf(out,"%lld",INT64_LL(jsonAsInt64(value)));
} else if (jsonIsDouble(value)){
fprintf(out,"%f",jsonAsDouble(value));
} else if (jsonIsBoolean(value)){
fprintf(out,"%s",jsonAsBoolean(value) ? "true" : "false");
} else if (jsonIsNull(value)){
fprintf(out,"null");
} else {
fprintf(out,"error: unhandled type");
return ZCFG_EXTRACT_ERROR;
}
return ZCFG_SUCCESS;
}

static int extractText(ConfigManager *mgr, const char *configName, JsonPointer *jp, FILE *out){
Json *value = NULL;
int status = cfgGetAnyJ(mgr,configName,&value,jp);
int ret = 0;
if (status){
fprintf(out,"error not found, reason=%d",status);
} else {
if (jsonIsObject(value) ||
jsonIsArray(value)){
fprintf(out,"error: cannot access whole objects or arrays");
} else if (jsonIsString(value)){
fprintf(out,"%s",jsonAsString(value));
} else if (jsonIsInt64(value)){
fprintf(out,"%lld",INT64_LL(jsonAsInt64(value)));
} else if (jsonIsDouble(value)){
fprintf(out,"%f",jsonAsDouble(value));
} else if (jsonIsBoolean(value)){
fprintf(out,"%s",jsonAsBoolean(value) ? "true" : "false");
} else if (jsonIsNull(value)){
fprintf(out,"null");
} else {
fprintf(out,"error: unhandled type");
return ZCFG_EXTRACT_ERROR;
}

if (jsonIsObject(value)) {
fprintf(out,"error: cannot access whole objects");
return ZCFG_EXTRACT_ERROR;
}

if (jsonIsArray(value)) {
JsonArray *array = jsonAsArray(value);
for (int i = 0; i < jsonArrayGetCount(array); i++) {
Json *arrayItem = jsonArrayGetItem(array, i);
if (jsonIsObject(arrayItem) || jsonIsArray(arrayItem)) {
fprintf(out,"error: cannot access objects or arrays in arrays");
return ZCFG_EXTRACT_ERROR;
} else {
ret = printPrimitiveDataType(arrayItem, out);
fprintf(out, "\n");
if (ret) {
return ret;
}
}
}
} else {
ret = printPrimitiveDataType(value, out);
}
return ret;
}

#define MAX_PATH_NAME 1024
Expand Down Expand Up @@ -1831,9 +1859,10 @@ static int simpleMain(int argc, char **argv){
printJsonPointer(mgr->traceOut,jp);
fflush(mgr->traceOut);
}
extractText(mgr,configName,jp,stdout);
int extractResult = extractText(mgr,configName,jp,stdout);
printf("\n");
fflush(stdout);
return extractResult;
}
} else if (!strcmp(command, "env")) {
if (argx >= argc){
Expand Down
1 change: 1 addition & 0 deletions h/configmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef struct ConfigManager_tag{
#define ZCFG_IO_ERROR 13
#define ZCFG_VALIDATION_INTERNAL_ERROR 14
#define ZCFG_JQ_PARSE_ERROR 15
#define ZCFG_EXTRACT_ERROR 16
/* Normal way to tell people the program succeded but their config is bad */
#define ZCFG_CONFIG_FAILED_VALIDATION 99

Expand Down
11 changes: 11 additions & 0 deletions tests/configmgr/extract/extract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://json-schema.org/draft/2019-09",
"$id": "https://zowe.org/schema/base",
"type": "object",
"properties": {
"test": {
"type": "object",
"additionalProperties": false
}
}
}
33 changes: 33 additions & 0 deletions tests/configmgr/extract/extract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
test:
number1: 0
number2: "${{ 9 * 13 }}"
number3: 123456789
boolean1: false
boolean2: "${{ 1 === 1 }}"
null1: ~
null2: null
null3:
string1: 'Hello, World!'
string2: "${{ 'a'.repeat(16) }}"
array1:
- 'apple'
- 'banana'
- 'kiwi'
array2: [ 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 ]
array3:
- 'string'
- 0
- true
- "${{ 1 + 1 }}"
- "${{ [ 1, 2, 3 ].toString() }}"
- ~
array4: []
array5: [ ~, ~, ~ ]
error_expected1:
- 'First item'
- [ 0, 1 ]
- [ 0, 1, 2 ]
error_expected2:
NESTED:
OBJECTS:
SUPPORTED: false
49 changes: 49 additions & 0 deletions tests/configmgr/extract/index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

#######################################################################
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License v2.0 which
# accompanies this distribution, and is available at
# https://www.eclipse.org/legal/epl-v20.html
#
# SPDX-License-Identifier: EPL-2.0
#
# Copyright Contributors to the Zowe Project.
#######################################################################

if [ `uname` != "OS/390" ]; then
echo "Error: this test must run on a z/OS system."
exit 1
fi

if [ "${1}" = "--help" ]; then
echo "Test the configmgr with 'extract' option"
echo " no parm: tries to run configmgr from current 'zowe-common-c/bin'"
echo " path: path to configmgr"
echo " --help: this help"
exit 0
fi

configmgr_path="${1}"
if [ -z "${configmgr_path}" ]; then
configmgr_path="../../../bin/configmgr"
fi

if [ ! -f "${configmgr_path}" ]; then
echo "Error: configmgr not found in '${configmgr_path}'"
exit 4
fi

schema="./extract.json"
yaml="./extract.yaml"
fileYaml="FILE(${yaml})"

# To simplify this test, any property starting with lowercase will be tested, the rest is ignored
test_set=$(cat "${yaml}" | grep -E '^[\ ]+[a-z]+' | awk -F: '{ print $1 }');

for item in $test_set; do

result=$(_CEE_RUNOPTS="XPLINK(ON)" "${configmgr_path}" -s "${schema}" -p "${fileYaml}" extract "/test/${item}");
echo "rc=${?}: Item ${item} -> $result"

done
Loading