liveBook · Manning

Map

As explained in API Design Patterns

A map is a collection of key-value pairs in which keys identify associated values and may be dynamically defined.

A dictionary, or map, stores key-value pairs. Keys identify associated values and can be defined dynamically. Unlike a custom data type with a predefined schema, a map suits arbitrary data whose structure is not known in advance. section 5.7

Choose between a map and a schema-based structure deliberately:

  • Use a custom data type to group known, related fields while preserving explicit names and value types. section 5.7
  • Use a map for arbitrary, dynamic data with no expected structure, such as user-defined attributes or measurements. section 5.7
  • Bound the number of entries, key lengths, and value lengths; otherwise, a map can grow like an unbounded list. section 5.7.1
  • Avoid using a total-character limit as the default solution, because it can encourage moving increasingly large data into the map instead of a better structure. section 5.7.1

Ingredient measurements

A grocery item can keep known fields such as name and calories while storing variable ingredient measurements in ingredientAmounts. Keys such as fat, sodium, and sugar map to values such as 9 g, 150 mg, and 1 g.

Maps can also appear inside resources. Partial retrievals and updates can address nested map entries through field masks: separate path components with dots, quote nonliteral keys with backticks, and treat map keys as strings. section 8.3.2

Table 8.3 A resource representation with corresponding field mask entries for each field (view table figure)

ChatRoom resource

Field mask entry

{
"*"
  "id": 1,
"id"
  "title": "Chat Room",
"title"
  "description": "Cool chat!"
"description"
  "loggingConfig": {
"loggingConfig.*"
    "maxSizeMb": 10,
"loggingConfig.maxSizeMb"
    "maxMessageCount": 1000
"loggingConfig.maxMessageCount"
  },
 
  "settings": {
"settings.*"
    "1234": "abcd",
"settings.`1234`"
    "`hello`": null,
"settings.```hello```"
    "*": "star",
"settings.`*`"
    "test.value": true
"settings.`test.value`"
    "test": {
"settings.test.*"
      "value": false
"settings.test.value"
    }
 
  }
 
}
 

Use the field-mask table to compare nested interface paths, wildcard map selections, quoted numeric keys, escaped backticks, and keys containing dots. section 8.3.2

How API Design Patterns covers it

How deep it goes
Defines maps, contrasts schemas, explains bounds, and specifies nested field-mask syntax without detailing storage internals.
Examples
Uses ChatRoom configuration, grocery ingredient amounts, JSON objects, and nested settings field-mask paths.
What it assumes
Assumes familiarity with resources, interfaces, JSON-style objects, and field masks while contrasting schema-defined and dynamic structures.
What you can do after
Build it from scratch by choosing maps for dynamic fields, bounding their contents, and addressing nested entries with valid field masks.
How it connects
Connects maps with custom data types, resources, JSON, lists, nested interfaces, and field masks.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.