⚙️ JSONNET RUNE DIFFICULTY: Archmage Behavioral

The Visitor of Jsonnet

Separating an algorithm from the object structure.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Divination // Deep Inspection
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// JSONNET PATTERN //
local Visitor = {
  visitFile(f):: f.size,
  visitDir(d):: std.foldl(function(acc, c) acc + c.accept(self), d.children, 0)
};

local File(size) = { type: "file", size: size, accept(visitor):: visitor.visitFile(self) };
local Directory(children) = { type: "dir", children: children, accept(visitor):: visitor.visitDir(self) };

local tree = Directory([
  File(10),
  Directory([File(20), File(30)])
]);

{
  total_size: tree.accept(Visitor)
}

The Visitor gracefully walks recursive object trees, applying separate logic decoupled from the nodes themselves, peering into their inner secrets to divine a collective outcome.

Runic Tags: #behavioral #visitor #jsonnet