Skip to content

Commit fbbffdb

Browse files
committed
Improve documentation for MMDB_get_entry_data_list and map/array access
This commit addresses two long-standing documentation issues: - Issue #145: Clarifies that the `start` parameter of MMDB_get_entry_data_list() is an MMDB_entry_s value that typically comes from the entry member of MMDB_lookup_result_s. - Issue #255: Adds comprehensive documentation section on accessing elements in maps and arrays using MMDB_get_value(). Includes examples of: * Accessing map values by key * Accessing array elements by numeric index * Using negative array indices * Navigating nested structures * When to use MMDB_get_entry_data_list() for iteration All code examples have been tested with the test database to ensure correctness. Fixes #145 Fixes #255
1 parent 700da00 commit fbbffdb

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

doc/libmaxminddb.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,53 @@ If you want to get all of the entry data at once you can call
611611
For each of the three functions, the return value is a status code as
612612
defined above.
613613
614+
### Accessing Elements in Maps and Arrays
615+
616+
When `MMDB_get_value()` returns an `MMDB_entry_data_s` with a type of
617+
`MMDB_DATA_TYPE_MAP` or `MMDB_DATA_TYPE_ARRAY`, you can access individual
618+
elements by extending the lookup path.
619+
620+
For maps, you can directly access values by their keys:
621+
622+
```c
623+
MMDB_entry_data_s entry_data;
624+
// Get the "en" value from the "names" map
625+
int status = MMDB_get_value(&result.entry, &entry_data,
626+
"names", "en", NULL);
627+
```
628+
629+
For arrays, you can access elements by their numeric index (as a string):
630+
631+
```c
632+
MMDB_entry_data_s entry_data;
633+
// Get the first element from the "cities" array
634+
int status = MMDB_get_value(&result.entry, &entry_data,
635+
"cities", "0", NULL);
636+
```
637+
638+
You can also use negative indices to access elements from the end of the array:
639+
640+
```c
641+
MMDB_entry_data_s entry_data;
642+
// Get the last element from the "cities" array
643+
int status = MMDB_get_value(&result.entry, &entry_data,
644+
"cities", "-1", NULL);
645+
```
646+
647+
The lookup path can navigate through multiple nested levels of maps and arrays:
648+
649+
```c
650+
MMDB_entry_data_s entry_data;
651+
// Navigate: top map -> "location" key -> "coordinates" array -> first element
652+
int status = MMDB_get_value(&result.entry, &entry_data,
653+
"location", "coordinates", "0", NULL);
654+
```
655+
656+
If you need to iterate over all elements in a map or array without knowing
657+
the keys or size in advance, use `MMDB_get_entry_data_list()` which returns
658+
all data in a depth-first traversal. See the `MMDB_get_entry_data_list()`
659+
documentation below for details.
660+
614661
## `MMDB_get_entry_data_list()`
615662

616663
```c
@@ -623,6 +670,10 @@ This function allows you to get all of the data for a complex data structure
623670
at once, rather than looking up each piece using repeated calls to
624671
`MMDB_get_value()`.
625672
673+
The `start` parameter is an `MMDB_entry_s` value. In most cases, this will
674+
come from the `entry` member of the `MMDB_lookup_result_s` structure returned
675+
by `MMDB_lookup_string()` or `MMDB_lookup_sockaddr()`.
676+
626677
```c
627678
MMDB_lookup_result_s result =
628679
MMDB_lookup_sockaddr(&mmdb, address->ai_addr, &mmdb_error);

0 commit comments

Comments
 (0)