liveBook · Manning

Reference id

As explained in Building User-Friendly DSLs

A reference ID is an identifier stored in a serialized reference object to identify the referenced AST object.

A reference ID is a unique identifier stored in a serialized reference object. It replaces a JavaScript object reference so an AST reference can survive JSON serialization. section 7.2

Implement reference IDs by:

  • Assigning every AST object an id property containing a unique string. section 7.2
  • Replacing ref: targetObject with refId: targetObject.id during serialization. section 7.2
  • Generating ten-character IDs with nanoid; collisions are unlikely enough for the example, though real ASTs should use longer IDs. section 7.2

Serialized reference

An AST object may have id "id1". A second object referring to it serializes its reference as { "refId": "id1" }, rather than embedding the JavaScript object. section 7.2

{ 
    id: "<unique-ID>",       #1
    concept: "<concept>", 
    settings: { 
        // ... 
    } 
}

Use the AST object shape to see where the identifier is stored before references are converted to refId values. section 7.2

How Building User-Friendly DSLs covers it

How deep it goes
Explains storage, generation, reference replacement, and serialization, but does not detail deserialization and reference resolution.
Examples
Uses rental AST objects, nanoid-generated strings, and serialized refId properties as concrete examples.
What it assumes
Assumes AST objects, JavaScript object references, and JSON.stringify; uses existing reference-recognition logic.
What you can do after
Build it from scratch by assigning AST IDs and converting reference objects into JSON-safe refId records.
How it connects
Connects ASTs with object references, JSON serialization, and unique identifier generation.

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.