liveBook · Manning

Dynamic data structure

As explained in API Design Patterns

A dynamic data structure permits fields or keys to be present or absent rather than requiring a fixed structure for every value.

A dynamic data structure permits keys or fields to be present, explicitly null, or absent. JSON objects and maps use this flexibility, but APIs must define how each state behaves. section 5.1.1 section 8.3.6

Design dynamic structures deliberately:

  • Distinguish a value, explicit null, and a missing key; treating them alike creates inconsistent requests and responses. section 5.1.1
  • Use field masks for partial updates: an omitted field is not selected, while a null field is selected and set to null. section 8.3.5
  • Remove keys with an explicit field-mask operation rather than inventing an undefined value that JSON cannot represent. section 8.3.6

Removing a map key

To remove the test key from a ChatRoom settings map, send PATCH /chatRooms/1?fieldMask=settings.test with an empty JSON body. The mask identifies the key for removal without encoding undefined. section 8.3.6

fruit1 = { name: "Apple", color: "red" };
fruit2 = { name: "Apple", color: "" };
fruit3 = { name: "Apple", color: null };
fruit4 = { name: "Apple" };

Compare a concrete value, an empty string, explicit null, and a missing color in otherwise similar Fruit objects. section 5.1.1

How API Design Patterns covers it

How deep it goes
Explains three field states and deletion mechanics, stopping short of a complete dynamic-structure implementation.
Examples
Uses Fruit color objects, ChatRoom settings maps, PATCH requests, and field-mask examples.
What it assumes
Assumes familiarity with JSON objects, maps, null values, and partial-update field masks.
What you can do after
Operate partial-update APIs that distinguish omission, null assignment, and dynamic-key removal.
How it connects
Connects dynamic structures with JSON, maps, null values, field masks, partial updates, and static data structures.

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.