-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add Dictionary support to @Generable macro #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ public struct GenerationSchema: Sendable, Codable, CustomDebugStringConvertible | |
| indirect enum Node: Sendable, Codable { | ||
| case object(ObjectNode) | ||
| case array(ArrayNode) | ||
| case dictionary(DictionaryNode) | ||
| case string(StringNode) | ||
| case number(NumberNode) | ||
| case boolean | ||
|
|
@@ -89,6 +90,13 @@ public struct GenerationSchema: Sendable, Codable, CustomDebugStringConvertible | |
| try container.encode(max, forKey: .maxItems) | ||
| } | ||
|
|
||
| case .dictionary(let dict): | ||
| try container.encode("object", forKey: .type) | ||
| if let desc = dict.description { | ||
| try container.encode(desc, forKey: .description) | ||
| } | ||
| try container.encode(dict.values, forKey: .additionalProperties) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This new encoding writes Useful? React with 👍 / 👎. |
||
|
|
||
| case .string(let str): | ||
| try container.encode("string", forKey: .type) | ||
| if let desc = str.description { | ||
|
|
@@ -145,14 +153,22 @@ public struct GenerationSchema: Sendable, Codable, CustomDebugStringConvertible | |
|
|
||
| switch type { | ||
| case "object": | ||
| let propsContainer = try container.nestedContainer(keyedBy: DynamicCodingKey.self, forKey: .properties) | ||
| var properties: [String: Node] = [:] | ||
| for key in propsContainer.allKeys { | ||
| properties[key.stringValue] = try propsContainer.decode(Node.self, forKey: key) | ||
| // Distinguish between a regular object (has "properties") and a dictionary (has "additionalProperties" as a schema) | ||
| if container.contains(.properties) { | ||
| let propsContainer = try container.nestedContainer(keyedBy: DynamicCodingKey.self, forKey: .properties) | ||
| var properties: [String: Node] = [:] | ||
| for key in propsContainer.allKeys { | ||
| properties[key.stringValue] = try propsContainer.decode(Node.self, forKey: key) | ||
| } | ||
| let requiredArray = try container.decodeIfPresent([String].self, forKey: .required) ?? [] | ||
| let required = Set(requiredArray) | ||
| self = .object(ObjectNode(description: description, properties: properties, required: required)) | ||
| } else if let valuesNode = try? container.decode(Node.self, forKey: .additionalProperties) { | ||
| self = .dictionary(DictionaryNode(description: description, values: valuesNode)) | ||
| } else { | ||
| // Empty object with no properties | ||
| self = .object(ObjectNode(description: description, properties: [:], required: [])) | ||
| } | ||
| let requiredArray = try container.decodeIfPresent([String].self, forKey: .required) ?? [] | ||
| let required = Set(requiredArray) | ||
| self = .object(ObjectNode(description: description, properties: properties, required: required)) | ||
|
|
||
| case "array": | ||
| let items = try container.decode(Node.self, forKey: .items) | ||
|
|
@@ -203,6 +219,11 @@ public struct GenerationSchema: Sendable, Codable, CustomDebugStringConvertible | |
| var maxItems: Int? | ||
| } | ||
|
|
||
| struct DictionaryNode: Sendable, Codable { | ||
| var description: String? | ||
| var values: Node | ||
| } | ||
|
|
||
| struct StringNode: Sendable, Codable { | ||
| var description: String? | ||
| var pattern: String? | ||
|
|
@@ -256,6 +277,8 @@ public struct GenerationSchema: Sendable, Codable, CustomDebugStringConvertible | |
| return "object(\(obj.properties.count) properties)" | ||
| case .array(let arr): | ||
| return "array(items: \(debugString(for: arr.items, indent: 0)))" | ||
| case .dictionary(let dict): | ||
| return "dictionary(values: \(debugString(for: dict.values, indent: 0)))" | ||
| case .string(let str): | ||
| if let choices = str.enumChoices { | ||
| return "string(enum: \(choices))" | ||
|
|
@@ -526,6 +549,8 @@ public struct GenerationSchema: Sendable, Codable, CustomDebugStringConvertible | |
| } | ||
| case .array(let arr): | ||
| try validateRefs(arr.items, defs: defs, undefinedRefs: &undefinedRefs) | ||
| case .dictionary(let dict): | ||
| try validateRefs(dict.values, defs: defs, undefinedRefs: &undefinedRefs) | ||
| case .anyOf(let nodes): | ||
| for n in nodes { | ||
| try validateRefs(n, defs: defs, undefinedRefs: &undefinedRefs) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
Value.generationSchema.rootis a$ref(for example,Valueis another@Generabletype), this code stores that ref inadditionalPropertiesbut dropsValue.generationSchema.defsby returningGenerationSchema.primitive(...).GenerationSchema.Property.buildNodelater only mergesschema.defs, so schemas for[String: Value]can emit dangling$refs with missing definitions, which breaks nested dictionary value typing in structured generation/tool schemas.Useful? React with 👍 / 👎.