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

v.info: add json output for columns #4590

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion vector/v.info/local_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void parse_args(int, char **, char **, char **, int *, int *, int *,
void format_double(double, char *);
void print_region(struct Map_info *, enum OutputFormat, JSON_Object *);
void print_topo(struct Map_info *, enum OutputFormat, JSON_Object *);
void print_columns(struct Map_info *, const char *, const char *);
void print_columns(struct Map_info *, const char *, const char *,
enum OutputFormat);
void print_info(struct Map_info *);
void print_shell(struct Map_info *, const char *, enum OutputFormat,
JSON_Object *);
2 changes: 1 addition & 1 deletion vector/v.info/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int main(int argc, char *argv[])
}
}
else if (col_flag) {
print_columns(&Map, input_opt, field_opt);
print_columns(&Map, input_opt, field_opt, format);
}
Vect_close(&Map);

Expand Down
55 changes: 50 additions & 5 deletions vector/v.info/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void print_topo(struct Map_info *Map, enum OutputFormat format,
}

void print_columns(struct Map_info *Map, const char *input_opt,
const char *field_opt)
const char *field_opt, enum OutputFormat format)
{
int num_dblinks, col, ncols;

Expand Down Expand Up @@ -215,13 +215,58 @@ void print_columns(struct Map_info *Map, const char *input_opt,
if (db_describe_table(driver, &table_name, &table) != DB_OK)
G_fatal_error(_("Unable to describe table <%s>"), fi->table);

JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_object(root_value);

JSON_Value *columns_value = json_value_init_array();
JSON_Array *columns_array = json_array(columns_value);
NishantBansal2003 marked this conversation as resolved.
Show resolved Hide resolved

if (format == JSON) {
json_object_set_value(root_object, "columns", columns_value);
}

ncols = db_get_table_number_of_columns(table);
for (col = 0; col < ncols; col++)
fprintf(stdout, "%s|%s\n",
db_sqltype_name(
db_get_column_sqltype(db_get_table_column(table, col))),
for (col = 0; col < ncols; col++) {
if (format == JSON) {
nilason marked this conversation as resolved.
Show resolved Hide resolved

JSON_Value *column_value = json_value_init_object();
JSON_Object *column_object = json_object(column_value);

json_object_set_string(
column_object, "name",
db_get_column_name(db_get_table_column(table, col)));

int sql_type =
db_get_column_sqltype(db_get_table_column(table, col));
json_object_set_string(column_object, "sql_type",
db_sqltype_name(sql_type));

int c_type = db_sqltype_to_Ctype(sql_type);
json_object_set_boolean(
column_object, "is_number",
(c_type == DB_C_TYPE_INT || c_type == DB_C_TYPE_DOUBLE));

json_array_append_value(columns_array, column_value);
}
else {
fprintf(stdout, "%s|%s\n",
db_sqltype_name(
db_get_column_sqltype(db_get_table_column(table, col))),
db_get_column_name(db_get_table_column(table, col)));
}
}

if (format == JSON) {
char *serialized_string = NULL;
serialized_string = json_serialize_to_string_pretty(root_value);
if (serialized_string == NULL) {
G_fatal_error(_("Failed to initialize pretty JSON string."));
nilason marked this conversation as resolved.
Show resolved Hide resolved
}
puts(serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
}

db_close_database(driver);
db_shutdown_driver(driver);
}
Expand Down
21 changes: 21 additions & 0 deletions vector/v.info/testsuite/test_vinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,27 @@ def test_json(self):
result.pop(field)
self.assertDictEqual(expected, result)

def test_json_column(self):
module = SimpleModule(
"v.info", map=self.test_vinfo_with_db_3d, format="json", flags="c"
)
self.runModule(module)

expected_json = {
"columns": [
{"is_number": True, "name": "cat", "sql_type": "INTEGER"},
{
"is_number": True,
"name": "elevation",
"sql_type": "DOUBLE PRECISION",
},
]
}

result = json.loads(module.outputs.stdout)

self.assertDictEqual(expected_json, result)

def test_database_table(self):
"""Test the database table column and type of the two vector maps with attribute data"""
self.assertModuleKeyValue(
Expand Down
Loading