Repository Public License
1import Foundation2import PrismCore34/// A typed input or output of an object (PUOP "typed ports").5public struct Port: Sendable, Hashable {6 public var name: String7 public var type: String8 public var required: Bool9 public init(name: String, type: String, required: Bool = false) {10 self.name = name; self.type = type; self.required = required11 }1213 public static let knownTypes: Set<String> = ["any", "string", "number", "boolean", "date", "record", "table", "list", "action"]1415 init(json: JSONValue) throws {16 guard let name = json["name"]?.stringValue, let type = json["type"]?.stringValue else {17 throw BlueprintError.malformed("A port needs a string `name` and `type`.")18 }19 self.init(name: name, type: type, required: json["required"]?.boolValue ?? false)20 }2122 var json: JSONValue { ["name": .string(name), "type": .string(type), "required": .bool(required)] }23}2425public enum BlueprintError: Error, LocalizedError, Sendable, Equatable {26 case malformed(String)27 case unsupportedVersion(found: Int, supported: Int)28 case patch(String)2930 public var errorDescription: String? {31 switch self {32 case .malformed(let m): "Malformed blueprint: \(m)"33 case .unsupportedVersion(let found, let supported):34 "This blueprint uses format version \(found); this build of PRISM reads up to version \(supported)."35 case .patch(let m): "Change could not be applied: \(m)"36 }37 }38}3940/// One PUOP object (spec §6, ADR 0002).41public struct PrismObject: Sendable, Hashable, Identifiable {42 public static let currentSchemaVersion = 14344 public var id: String45 public var type: String46 public var schemaVersion: Int47 public var name: String48 public var metadata: [String: JSONValue]49 public var dataRef: String?50 public var inputs: [Port]51 public var outputs: [Port]52 public var capabilities: [String]53 public var permissions: [String]54 public var dependencies: [String]55 public var ui: [String: JSONValue]56 public var runtime: [String: JSONValue]57 public var version: Int58 public var provenance: [String: JSONValue]59 public var tests: [JSONValue]60 /// Component configuration. Values may be literals, `{"$expr": …}` or `{"$bind": …}`.61 public var props: [String: JSONValue]62 /// Ordered IDs of child objects.63 public var children: [String]64 /// Event name → ordered list of actions.65 public var events: [String: [JSONValue]]6667 public init(68 id: String, type: String, name: String = "", props: [String: JSONValue] = [:], children: [String] = [],69 events: [String: [JSONValue]] = [:], dataRef: String? = nil, dependencies: [String] = [],70 permissions: [String] = [], provenance: [String: JSONValue] = [:]71 ) {72 self.id = id; self.type = type; self.schemaVersion = Self.currentSchemaVersion; self.name = name73 self.metadata = [:]; self.dataRef = dataRef; self.inputs = []; self.outputs = []; self.capabilities = []74 self.permissions = permissions; self.dependencies = dependencies; self.ui = [:]; self.runtime = [:]75 self.version = 1; self.provenance = provenance; self.tests = []76 self.props = props; self.children = children; self.events = events77 }7879 public static let fieldNames: [String] = [80 "id", "type", "schemaVersion", "name", "metadata", "dataRef", "inputs", "outputs", "capabilities",81 "permissions", "dependencies", "ui", "runtime", "version", "provenance", "tests", "props", "children", "events",82 ]8384 public init(json: JSONValue) throws {85 guard let o = json.objectValue else { throw BlueprintError.malformed("An object must be a JSON object.") }86 guard let id = o["id"]?.stringValue, !id.isEmpty else { throw BlueprintError.malformed("An object is missing its `id`.") }87 guard let type = o["type"]?.stringValue, !type.isEmpty else {88 throw BlueprintError.malformed("Object `\(id)` is missing its `type`.")89 }90 if let unknown = o.keys.first(where: { !Self.fieldNames.contains($0) }) {91 throw BlueprintError.malformed("Object `\(id)` has an unknown field `\(unknown)`.")92 }93 func strings(_ key: String) throws -> [String] {94 guard let v = o[key] else { return [] }95 guard let a = v.arrayValue else { throw BlueprintError.malformed("`\(key)` of `\(id)` must be an array of strings.") }96 return try a.map {97 guard let s = $0.stringValue else { throw BlueprintError.malformed("`\(key)` of `\(id)` must contain strings.") }98 return s99 }100 }101 func dict(_ key: String) throws -> [String: JSONValue] {102 guard let v = o[key], !v.isNull else { return [:] }103 guard let d = v.objectValue else { throw BlueprintError.malformed("`\(key)` of `\(id)` must be an object.") }104 return d105 }106 self.id = id107 self.type = type108 self.schemaVersion = o["schemaVersion"]?.intValue ?? Self.currentSchemaVersion109 self.name = o["name"]?.stringValue ?? ""110 self.metadata = try dict("metadata")111 self.dataRef = o["dataRef"]?.stringValue112 self.inputs = try (o["inputs"]?.arrayValue ?? []).map(Port.init(json:))113 self.outputs = try (o["outputs"]?.arrayValue ?? []).map(Port.init(json:))114 self.capabilities = try strings("capabilities")115 self.permissions = try strings("permissions")116 self.dependencies = try strings("dependencies")117 self.ui = try dict("ui")118 self.runtime = try dict("runtime")119 self.version = o["version"]?.intValue ?? 1120 self.provenance = try dict("provenance")121 self.tests = o["tests"]?.arrayValue ?? []122 self.props = try dict("props")123 self.children = try strings("children")124 var events: [String: [JSONValue]] = [:]125 for (name, value) in try dict("events") {126 guard let list = value.arrayValue else {127 throw BlueprintError.malformed("Event `\(name)` of `\(id)` must be an array of actions.")128 }129 events[name] = list130 }131 self.events = events132 }133134 /// Full JSON form. Empty optional collections are omitted so files stay readable.135 public var json: JSONValue {136 var o: [String: JSONValue] = [137 "id": .string(id), "type": .string(type), "schemaVersion": .int(schemaVersion), "version": .int(version),138 ]139 if !name.isEmpty { o["name"] = .string(name) }140 if !metadata.isEmpty { o["metadata"] = .object(metadata) }141 if let dataRef { o["dataRef"] = .string(dataRef) }142 if !inputs.isEmpty { o["inputs"] = .array(inputs.map(\.json)) }143 if !outputs.isEmpty { o["outputs"] = .array(outputs.map(\.json)) }144 if !capabilities.isEmpty { o["capabilities"] = .array(capabilities.map(JSONValue.string)) }145 if !permissions.isEmpty { o["permissions"] = .array(permissions.map(JSONValue.string)) }146 if !dependencies.isEmpty { o["dependencies"] = .array(dependencies.map(JSONValue.string)) }147 if !ui.isEmpty { o["ui"] = .object(ui) }148 if !runtime.isEmpty { o["runtime"] = .object(runtime) }149 if !provenance.isEmpty { o["provenance"] = .object(provenance) }150 if !tests.isEmpty { o["tests"] = .array(tests) }151 if !props.isEmpty { o["props"] = .object(props) }152 if !children.isEmpty { o["children"] = .array(children.map(JSONValue.string)) }153 if !events.isEmpty { o["events"] = .object(events.mapValues(JSONValue.array)) }154 return .object(o)155 }156157 public var isPage: Bool { type == ObjectType.page }158 public var isTable: Bool { type == ObjectType.dataTable }159 public var isQuery: Bool { type == ObjectType.dataQuery }160 /// A global script: functions that actions and expressions call by name.161 public var isScript: Bool { type == ObjectType.script }162 /// The definition of an on-demand component; instances have the type `custom.<id>`.163 public var isComponentDefinition: Bool { type == ObjectType.componentDefinition }164 public var isCustomInstance: Bool { type.hasPrefix(ObjectType.customPrefix) }165 public var isComponent: Bool { !isPage && !isTable && !isQuery && !isScript && !isComponentDefinition }166}167168public enum ObjectType {169 public static let page = "prism.page"170 public static let dataTable = "prism.data.table"171 public static let dataQuery = "prism.data.query"172 public static let script = "prism.script"173 public static let componentDefinition = "prism.component"174 public static let customPrefix = "custom."175}176