⚙️ JSONNET RUNE DIFFICULTY: Adept Structural

The Composite of Jsonnet

Treating individual objects and compositions uniformly.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Composite
ARCANUM SCHOOL: Illusion // Fractal Reality
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// JSONNET PATTERN //
local File(name, size) = { type: "file", name: name, size: size };
local Directory(name, children) = {
  type: "directory",
  name: name,
  children: children,
  size: std.foldl(function(acc, c) acc + c.size, children, 0)
};

local fs = Directory("root", [
  File("spell1.txt", 10),
  Directory("docs", [
    File("grimoire.pdf", 500)
  ])
]);

{
  filesystem: fs
}

The Composite pattern is naturally expressed as recursive object and array structures, where calculating a property like size elegantly cascades down the tree.

Runic Tags: #structural #composite #jsonnet